我正在使用这个基于 win32 的 C 程序将快捷方式的目标从
["C:\Program Files\YP\YPseries\Bin\myexe.exe"] 更改为
["C:\Program Files\YP\YPseries\Bin\ myexe.exe" -Start UDCDevicePage]
不包括方括号。
但是,当我使用
WCHAR newTargetPath[] = L"\"C:\Program Files\YP\YP series\Bin\myexe.exe\" -Start UDCDevicePage";
在 main 中,SetPath 返回 E_INVALIDARG 错误代码。
如何使用 IShellLink::SetPath 函数将参数传递给 myexe?
程序如下:
HRESULT changeLinkTarget(LPCSTR pathLink, LPWSTR newTargetPath)
{
HRESULT hres;
IShellLink* psl;
WCHAR szGotPath[MAX_PATH];
WIN32_FIND_DATA wfd;
// Get a pointer to the IShellLink interface.
hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl);
if (SUCCEEDED(hres))
{
IPersistFile* ppf;
// Get a pointer to the IPersistFile interface.
hres = psl->QueryInterface(IID_IPersistFile, (void**)&ppf);
if (SUCCEEDED(hres))
{
WCHAR wsz[MAX_PATH];
// Ensure that the string is Unicode.
MultiByteToWideChar(CP_ACP, 0, pathLink, -1, wsz, MAX_PATH);
// Load the shortcut.
hres = ppf->Load(wsz, STGM_READ);
if (SUCCEEDED(hres))
{
// Get the path to the link target.
hres = psl->GetPath(szGotPath, MAX_PATH, (WIN32_FIND_DATA*)&wfd, SLGP_SHORTPATH);
if (SUCCEEDED(hres))
{
hres = psl->SetPath(newTargetPath);
hres = ppf->Save(wsz, TRUE); //save changes
}
else
{
// Handle the error
}
}
// Release the pointer to the IPersistFile interface.
ppf->Release();
}
// Release the pointer to the IShellLink interface.
psl->Release();
}
return hres;
}
int _tmain(int argc, _TCHAR* argv[])
{
char linkPath[128] = "C:\\Users\\Public\\Desktop\\YP series.lnk";
WCHAR newTargetPath[] = L"\"C:\\Program Files\\YP\\YP series\\Bin\\myexe.exe\" -Start UDCDevicePage";
CoInitialize(NULL); // initialize the COM subsystem
HRESULT ret = changeLinkTarget(linkPath, newTargetPath);
return 0;
}