如何使用 IShellLink::SetPath 函数传递参数

发布于 2025-01-05 00:14:31 字数 2329 浏览 3 评论 0原文

我正在使用这个基于 win32 的 C 程序来更改快捷方式的目标
[“C:\Program Files\YP\YPseries\Bin\myexe.exe”] 到
[“C:\Program Files\YP\YPseries\Bin\myexe.exe”-启动 UDCDevicePage]
不包括方括号。

但是当我使用
WCHAR newTargetPath[] = L"\"C:\Program Files\YP\YP 系列\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;
}

I am using this win32 based C program to change the target of a shortcut from
["C:\Program Files\YP\YPseries\Bin\myexe.exe"] to
["C:\Program Files\YP\YPseries\Bin\myexe.exe" -Start UDCDevicePage]
excluding the square braces.

However when i use
WCHAR newTargetPath[] = L"\"C:\Program Files\YP\YP series\Bin\myexe.exe\" -Start UDCDevicePage";
in main, SetPath returns an E_INVALIDARG error code.

How can i pass an argument to myexe using IShellLink::SetPath function?

The program is given below:

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

友欢 2025-01-12 00:14:32

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

Paths are usually just an exe; you seem to be trying to set the path to be an executable with some command-line args. Try instead using SetPath to just the exe - without the extra quotes - and use IShellLink::SetArguments() for the command-line parameters. SetPath is perhaps attempting to validate the param by checking that an exe with the name of the full string you pass it exists, which is likely failing causing the error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文