由于在开发中,使用Qt中的QFileInfo::symLinkTarget不能获取到正确的文件路径。
C:/Windows/Installer/{8F158BE6-D2F5-40CF-A51F-5C658A2A7CC5}/NewShortcut1_409C464E55B64E9C8A394B22BC19BCFB.exe
所以找到一种在window下查找快捷方式的属性的方法。
编译环境vs2019 C++
转载:https://www.cnblogs.com/rcg714786690/p/14231103.htmlWin32控制台获取可执行程序的快捷方式的目标位置、起始位置、快捷键、备注等 - 任小七 - 博客园 (cnblogs.com)
#include <iostream>
#include <atlstr.h>
#include <ShObjIdl.h>
#include <assert.h>
#include <ShlGuid.h>
using namespace std;
struct ShortcutAttribute
{
CString Path_;//目标位置
CString WorkingDirectory_;//起始位置
CString Description_;//备注
WORD HotKey_;//快捷键
ShortcutAttribute()
{
Path_ = _T("");
WorkingDirectory_ = _T("");
Description_ = _T("");
}
};
bool GetShortcutAttribute(IN CString shortcutPath, OUT ShortcutAttribute& shortattr)
{
assert(shortcutPath != _T(""));
LPTSTR lpsz = shortcutPath.GetBuffer(MAX_PATH);
IShellLink* psl = NULL;
// Initialize the COM library.
HRESULT hr = CoInitialize(NULL);
if (FAILED(hr))
{
printf("ERROR - Could not initialize COM library");
return false;
}
HRESULT hres = ::CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl);
if (SUCCEEDED(hres))
{
IPersistFile* ppf = NULL;
hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf);
if (SUCCEEDED(hres))
{
#ifndef _UNICODE
wchar_t wsz[MAX_PATH];
::MultiByteToWideChar(CP_ACP, 0, lpsz, -1, wsz, MAX_PATH);
hres = ppf->Load(wsz, STGM_READ);
#else
hres = ppf->Load(lpsz, STGM_READ);
#endif
if (SUCCEEDED(hres))
{
//1.目标位置
WIN32_FIND_DATA wfd;
hres = psl->GetPath(shortattr.Path_.GetBuffer(MAX_PATH), MAX_PATH, &wfd, SLGP_UNCPRIORITY);
//2.起始位置
hres = psl->GetWorkingDirectory(shortattr.WorkingDirectory_.GetBuffer(MAX_PATH), MAX_PATH);
//3.备注
hres = psl->GetDescription(shortattr.Description_.GetBuffer(MAX_PATH), MAX_PATH);
//4.快捷键
hres = psl->GetHotkey(&shortattr.HotKey_);
shortattr.Path_.ReleaseBuffer();
shortattr.WorkingDirectory_.ReleaseBuffer();
shortattr.Description_.ReleaseBuffer();
}
ppf->Release();
}
psl->Release();
}
shortcutPath.ReleaseBuffer();
CoUninitialize();
return true;
}
int main(int argc, char* argv[])
{
ShortcutAttribute sa;
bool flag = GetShortcutAttribute(CString(L"C:/ProgramData/Microsoft/Windows/Start Menu/Programs/Git/Git GUI.lnk"), sa);
if (flag)
{
//cout << CT2A(sa.Path_.GetBuffer()) << endl;
cout << CT2A(sa.WorkingDirectory_.GetBuffer()) << endl;
//cout << CT2A(sa.Description_.GetBuffer()) << endl;
//cout << sa.HotKey_ << endl;
}
//getchar();
return 0;
}