#include <iostream> #include <ShObjIdl.h> #include <strsafe.h>
using namespace std;
#define TARGET TEXT("C:\\mylog.log") #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(IID_IPersistFile, (LPVOID *)&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(IID_IPersistFile, (LPVOID *)&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; }
void Office2010Desc(void) { TCHAR szFolder[MAX_PATH] = TEXT("C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Microsoft Office\\"); TCHAR szPath[MAX_PATH];
int flag; for (flag = 0; flag <= 1; flag++) { if (flag == 1) { StringCchCat(szFolder, MAX_PATH, TEXT("Microsoft Office 2010 Tools\\")); cout << endl << endl << endl; }
WIN32_FIND_DATA data; StringCchCopy(szPath, MAX_PATH, szFolder); StringCchCat(szPath, MAX_PATH, TEXT("*.lnk")); HANDLE hFind = FindFirstFile(szPath, &data); 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(IID_IPersistFile, (LPVOID *)&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);
Office2010Desc();
CoUninitialize(); return 0; }
|