Win32控制台獲取可執行程序的快捷方式的目標位置、起始位置、快捷鍵、備注等,示例如下圖:
#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:\\Users\\Public\\Desktop\\騰訊QQ.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; }