本文介绍了使用 C++ 显示(.lnk)快捷方式文件的真实路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
限时送ChatGPT账号..我正在尝试显示快捷方式(.lnk)文件的真实路径(目标).所以我将(test.lnk)复制并粘贴到项目的相关文件夹中,我正在尝试显示其目标
I'm trying to display real Path (target ) of shortcut (.lnk) file .So i copy and paste (test.lnk)in corrent folder of project and I'm trying to display its target
#include <windows.h>
#include <string>
#include <iostream>
#include <objidl.h> /* For IPersistFile */
#include <shlobj.h> /* For IShellLink */
#include "objbase.h"
using namespace std;
/*********************************************************************
* Function......: ResolveShortcut
* Parameters....: lpszShortcutPath - string that specifies a path
and file name of a shortcut
* lpszFilePath - string that will contain a file name
* Returns.......: S_OK on success, error code on failure
* Description...: Resolves a Shell link object (shortcut)
*********************************************************************/
HRESULT ResolveShortcut(/*in*/ LPCTSTR lpszShortcutPath,
/*out*/ LPTSTR lpszFilePath)
{
HRESULT hRes = E_FAIL;
IShellLink* psl = NULL;
// buffer that receives the null-terminated string
// for the drive and path
TCHAR szPath[MAX_PATH];
// buffer that receives the null-terminated
// string for the description
TCHAR szDesc[MAX_PATH];
// structure that receives the information about the shortcut
WIN32_FIND_DATA wfd;
WCHAR wszTemp[MAX_PATH];
lpszFilePath[0] = '\0';
// Get a pointer to the IShellLink interface
hRes = CoCreateInstance(CLSID_ShellLink,
NULL,
CLSCTX_INPROC_SERVER,
IID_IShellLink,
(void**)&psl);
if (SUCCEEDED(hRes))
{
// Get a pointer to the IPersistFile interface
IPersistFile* ppf = NULL;
psl->QueryInterface(IID_IPersistFile, (void **) &ppf);
// IPersistFile is using LPCOLESTR,
// so make sure that the string is Unicode
#if !defined _UNICODE
MultiByteToWideChar(CP_ACP, 0, lpszShortcutPath,
-1, wszTemp, MAX_PATH);
#else
wcsncpy(wszTemp, lpszShortcutPath, MAX_PATH);
#endif
// Open the shortcut file and initialize it from its contents
hRes = ppf->Load(wszTemp, STGM_READ);
if (SUCCEEDED(hRes))
{
// Try to find the target of a shortcut,
// even if it has been moved or renamed
hRes = psl->Resolve(NULL, SLR_UPDATE);
if (SUCCEEDED(hRes))
{
// Get the path to the shortcut target
hRes = psl->GetPath(szPath,
MAX_PATH, &wfd, SLGP_RAWPATH);
if (FAILED(hRes))
return hRes;
// Get the description of the target
hRes = psl->GetDescription(szDesc,
MAX_PATH);
if (FAILED(hRes))
return hRes;
lstrcpyn(lpszFilePath, szPath, MAX_PATH);
}
}
}
return hRes;
}
int main(void)
{
LPCTSTR lpszShortcutPath =("test.lnk");
TCHAR szFilePath[MAX_PATH];
HRESULT hRes =ResolveShortcut(lpszShortcutPath, szFilePath);
cout << TEXT("Succeeded: path = ") <<hRes ;
return 0;
}
显示成功:path = -2134343
it displays Succeeded: path = -2134343
谁能帮我显示 .lnk 文件的真实目标及其描述.
Can someone help me to display real target of .lnk file and its Description.
推荐答案
您可能忘记调用 CoInitialize
You probably forgot to call CoInitialize
开始你的主要功能:
int main(void)
{
CoInitialize(NULL); //<< add
LPCTSTR lpszShortcutPath....
CoUninitialize(); //<< add
}
这篇关于使用 C++ 显示(.lnk)快捷方式文件的真实路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论