#include <iostream>
#include <ShObjIdl.h>
#include <strsafe.h>
using namespace std;
#define TARGET TEXT("C:\\WINDOWS\\system32\\shell32.dll") // 快捷方式链接目标
#define LNKFILE L"example.lnk"
// 给cout添加输出Unicode字符串的功能
#ifdef _UNICODE
ostream &operator << (ostream &os, LPCWSTR lpwstr)
{
if (os == cout)
WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), lpwstr, wcslen(lpwstr), NULL, NULL);
return os;
}
#endif
// 创建快捷方式
void CreateShortcut(void)
{
IShellLink *psl;
HRESULT hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&psl));
if (SUCCEEDED(hr))
{
psl->SetPath(TARGET);
psl->SetDescription(TEXT("This is just an example."));
IPersistFile *ppf;
hr = psl->QueryInterface(&ppf);
if (SUCCEEDED(hr))
{
hr = ppf->Save(LNKFILE, TRUE);
if (SUCCEEDED(hr))
cout << TEXT("创建快捷方式成功") << endl;
else
cout << TEXT("创建快捷方式失败") << endl;
ppf->Release();
}
else
cout << TEXT("创建快捷方式时遇到系统错误") << endl;
psl->Release();
}
else
cout << TEXT("操作失败") << endl;
}
// 解析快捷方式
void ResolveShortcut(void)
{
IShellLink *psl;
HRESULT hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&psl));
if (SUCCEEDED(hr))
{
IPersistFile *ppf;
hr = psl->QueryInterface(&ppf);
if (SUCCEEDED(hr))
{
hr = ppf->Load(LNKFILE, STGM_READ);
if (SUCCEEDED(hr))
{
TCHAR szPath[MAX_PATH];
TCHAR szDesc[INFOTIPSIZE];
hr = psl->GetPath(szPath, MAX_PATH, NULL, SLGP_RAWPATH);
if (SUCCEEDED(hr))
cout << TEXT("快捷方式指向的文件: ") << szPath << endl;
else
cout << TEXT("获取快捷方式指向的文件失败!") << endl;
hr = psl->GetDescription(szDesc, INFOTIPSIZE);
if (SUCCEEDED(hr))
cout << TEXT("描述: ") << szDesc << endl;
else
cout << TEXT("获取快捷方式描述失败!") << endl;
}
else
cout << TEXT("打开文件失败") << endl;
ppf->Release();
}
else
cout << TEXT("读取文件时遇到系统错误") << endl;
psl->Release();
}
else
cout << TEXT("操作失败") << endl;
}
// 解析开始菜单中Office2010的所有快捷方式
void Office2010Desc(void)
{
TCHAR szFolder[MAX_PATH] = TEXT("C:\\Documents and Settings\\All Users\\「开始」菜单\\程序\\Microsoft Office\\");
TCHAR szPath[MAX_PATH];
HANDLE hFind;
int flag;
for (flag = 0; flag <= 1; flag++)
{
if (flag == 1)
{
StringCchCat(szFolder, MAX_PATH, TEXT("Microsoft Office 2010 工具\\"));
if (hFind != INVALID_HANDLE_VALUE)
cout << endl << endl << endl;
}
WIN32_FIND_DATA data;
StringCchCopy(szPath, MAX_PATH, szFolder);
StringCchCat(szPath, MAX_PATH, TEXT("*.lnk"));
hFind = FindFirstFile(szPath, &data);
if (hFind == INVALID_HANDLE_VALUE)
{
cout << szPath << TEXT(" doesn't exist!") << endl;
continue;
}
do
{
TCHAR szDesc[INFOTIPSIZE + 2] = TEXT(": ");
IShellLink *psl;
HRESULT hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&psl));
if (SUCCEEDED(hr))
{
IPersistFile *ppf;
hr = psl->QueryInterface(&ppf);
if (SUCCEEDED(hr))
{
StringCchCopy(szPath, MAX_PATH, szFolder);
StringCchCat(szPath, MAX_PATH, data.cFileName);
#ifdef _UNICODE
hr = ppf->Load(szPath, STGM_READ);
#else
WCHAR szwPath[MAX_PATH];
MultiByteToWideChar(CP_ACP, NULL, szPath, -1, szwPath, MAX_PATH);
hr = ppf->Load(szwPath, STGM_READ);
#endif
if (SUCCEEDED(hr))
{
hr = psl->GetDescription(szDesc + 2, INFOTIPSIZE);
if (FAILED(hr))
szDesc[0] = '\0';
}
ppf->Release();
}
psl->Release();
}
int len = lstrlen(data.cFileName);
data.cFileName[len - 4] = '\0';
cout << data.cFileName << szDesc << endl;
} while (FindNextFile(hFind, &data));
FindClose(hFind);
}
}
int main(void)
{
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
CreateShortcut();
ResolveShortcut();
Office2010Desc();
CoUninitialize();
system("pause");
return 0;
}
C++ 创建、解析快捷方式
原文链接:https://blog.****.net/zlk1214/article/details/79269524