C++ 代码:使用 ShellLink 对象修改 LNK 文件目标路径
#include <Windows.h> #include <ShlObj.h> #include <Objbase.h>
int main() { // 初始化 COM 库 HRESULT hr = CoInitialize(NULL); if (FAILED(hr)) { // COM 库初始化失败,处理错误 return -1; }
// 创建 ShellLink 对象
IShellLink* pShellLink = NULL;
hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&pShellLink);
if (SUCCEEDED(hr)) {
// 创建 IPersistFile 对象
IPersistFile* pPersistFile = NULL;
hr = pShellLink->QueryInterface(IID_IPersistFile, (LPVOID*)&pPersistFile);
if (SUCCEEDED(hr)) {
// 加载 LNK 文件
hr = pPersistFile->Load(L"C:\\Path\\to\\your\\file.lnk", STGM_READ);
if (SUCCEEDED(hr)) {
// 获取 LNK 文件中的目标路径
WCHAR targetPath[MAX_PATH];
WIN32_FIND_DATA findData;
hr = pShellLink->GetPath(targetPath, MAX_PATH, &findData, SLGP_UNCPRIORITY);
if (SUCCEEDED(hr)) {
// 在这里可以对目标路径进行编辑或其他操作
// ...
// 保存 LNK 文件
hr = pPersistFile->Save(NULL, TRUE);
if (FAILED(hr)) {
// 保存 LNK 文件失败,处理错误
}
} else {
// 获取目标路径失败,处理错误
}
} else {
// 加载 LNK 文件失败,处理错误
}
// 释放 IPersistFile 对象
pPersistFile->Release();
} else {
// 创建 IPersistFile 对象失败,处理错误
}
// 释放 ShellLink 对象
pShellLink->Release();
} else {
// 创建 ShellLink 对象失败,处理错误
}
// 释放 COM 库
CoUninitialize();
return 0;
}
原文地址: https://www.cveoy.top/t/topic/p0zi 著作权归作者所有。请勿转载和采集!