尝试用C更改快捷方式目标属性

huangapple go评论60阅读模式
英文:

Trying to change shortcut target property with C

问题

我创建了一个C程序,用于修改Chrome快捷方式的目标属性,以便加载我制作的扩展。如果我使用char newTargetPath[200] = "\"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe\"";,代码可以正常运行,但如果我使用char newTargetPath[200] = "\"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe\" --load-extension=C:\\path\\to\\extension";则无法正常工作。如何成功执行带有--load-extension标志的代码?这是我的代码:

#include <Windows.h>
#include <ShObjIdl.h>
#include <ShlGuid.h>
#include <ObjBase.h>
#include <combaseapi.h>
#include <stdio.h>

int main()
{
    HRESULT hr;
    CoInitialize(NULL);

    // 用您的快捷方式文件路径替换
    char shortcutPath[200] = "C:\\Users\\name\\Desktop\\chrome.lnk";
    // 用新目标文件的路径替换
    char newTargetPath[200] = "\"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe\" --load-extension=C:\\path\\to\\extension";

    // 创建IShellLink接口的实例
    IShellLink* shellLink;
    hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&shellLink);

    if (SUCCEEDED(hr))
    {
        // 创建IPersistFile接口的实例
        IPersistFile* persistFile;
        hr = shellLink->QueryInterface(IID_IPersistFile, (void**)&persistFile);

        if (SUCCEEDED(hr))
        {
            // 将快捷方式路径转换为Unicode
            wchar_t shortcutPathW[MAX_PATH];
            MultiByteToWideChar(CP_ACP, 0, shortcutPath, -1, shortcutPathW, MAX_PATH);

            // 加载现有的快捷方式文件
            hr = persistFile->Load(shortcutPathW, STGM_READ);

            if (SUCCEEDED(hr))
            {
                // 设置新的目标文件路径
                wchar_t newTargetPathW[MAX_PATH];
                MultiByteToWideChar(CP_ACP, 0, newTargetPath, -1, newTargetPathW, MAX_PATH);
                hr = shellLink->SetPath(newTargetPathW);

                if (SUCCEEDED(hr))
                {
                    // 保存修改后的快捷方式文件
                    hr = persistFile->Save(shortcutPathW, TRUE);

                    if (SUCCEEDED(hr))
                    {
                        printf("快捷方式目标成功修改。\n");
                    }
                    else
                    {
                        printf("保存修改后的快捷方式文件时出错。HRESULT: 0x%x\n", hr);
                    }
                }
                else
                {
                    printf("设置快捷方式目标时出错。HRESULT: 0x%x\n", hr);
                }
            }
            else
            {
                printf("加载快捷方式文件时出错。HRESULT: 0x%x\n", hr);
            }

            persistFile->Release();
        }
        else
        {
            printf("查询IPersistFile接口时出错。HRESULT: 0x%x\n", hr);
        }

        shellLink->Release();
    }
    else
    {
        printf("创建IShellLink实例时出错。HRESULT: 0x%x\n", hr);
    }

    CoUninitialize();
    return 0;
}
英文:

I made a C program to change the target property of Chrome shortcut to load it with a extension that I made. The code works if I use char newTargetPath[200] = &quot;\&quot;C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe\&quot;&quot;; it doesn't work if I use char newTargetPath[200] = &quot;\&quot;C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe\&quot; --load-extension=C:\\path\\to\\extension&quot;; How can I successfully execute this code with the --load-extension flag? This is my code:

#include &lt;Windows.h&gt;
#include &lt;ShObjIdl.h&gt;
#include &lt;ShlGuid.h&gt;
#include &lt;ObjBase.h&gt;
#include &lt;combaseapi.h&gt;
#include &lt;stdio.h&gt;
int main()
{
HRESULT hr;
CoInitialize(NULL);
// Replace with the path of your shortcut file
char shortcutPath[200] = &quot;C:\\Users\\name\\Desktop\\chrome.lnk&quot;;
// Replace with the path of the new target file
char newTargetPath[200] = &quot;\&quot;C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe\&quot; --load-extension=C:\\path\\to\\extension&quot;;
// Create an instance of the IShellLink interface
IShellLink* shellLink;
hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&amp;shellLink);
if (SUCCEEDED(hr))
{
// Create an instance of the IPersistFile interface
IPersistFile* persistFile;
hr = shellLink-&gt;QueryInterface(IID_IPersistFile, (void**)&amp;persistFile);
if (SUCCEEDED(hr))
{
// Convert the shortcut path to Unicode
wchar_t shortcutPathW[MAX_PATH];
MultiByteToWideChar(CP_ACP, 0, shortcutPath, -1, shortcutPathW, MAX_PATH);
// Load the existing shortcut file
hr = persistFile-&gt;Load(shortcutPathW, STGM_READ);
if (SUCCEEDED(hr))
{
// Set the new target file path
wchar_t newTargetPathW[MAX_PATH];
MultiByteToWideChar(CP_ACP, 0, newTargetPath, -1, newTargetPathW, MAX_PATH);
hr = shellLink-&gt;SetPath(newTargetPathW);
if (SUCCEEDED(hr))
{
// Save the modified shortcut file
hr = persistFile-&gt;Save(shortcutPathW, TRUE);
if (SUCCEEDED(hr))
{
printf(&quot;Shortcut target modified successfully.\n&quot;);
}
else
{
printf(&quot;Error saving modified shortcut file. HRESULT: 0x%x\n&quot;, hr);
}
}
else
{
printf(&quot;Error setting shortcut target. HRESULT: 0x%x\n&quot;, hr);
}
}
else
{
printf(&quot;Error loading shortcut file. HRESULT: 0x%x\n&quot;, hr);
}
persistFile-&gt;Release();
}
else
{
printf(&quot;Error querying IPersistFile interface. HRESULT: 0x%x\n&quot;, hr);
}
shellLink-&gt;Release();
}
else
{
printf(&quot;Error creating IShellLink instance. HRESULT: 0x%x\n&quot;, hr);
}
CoUninitialize();
return 0;
}

答案1

得分: 0

你不能在 SetPath 中使用程序参数。路径就是路径,不是命令。使用 SetArguments 方法来设置程序参数。

英文:

You cannot use program arguments in SetPath. A path is a path, not a command. Use SetArguments method to set program arguments.

huangapple
  • 本文由 发表于 2023年4月10日 21:51:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75977713.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定