2

我正在使用这个基于 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;
}
4

1 回答 1

3

路径通常只是一个 exe;您似乎正在尝试将路径设置为带有一些命令行参数的可执行文件。尝试使用 SetPath 仅对 exe - 没有额外的引号 - 并使用IShellLink::SetArguments()作为命令行参数。SetPath 可能试图通过检查是否存在具有您传递的完整字符串名称的 exe 来验证参数,这可能会导致错误。

于 2012-02-13T12:22:01.763 回答