英文:
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] = "\"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe\"";
it doesn't work if I use char newTargetPath[200] = "\"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe\" --load-extension=C:\\path\\to\\extension";
How can I successfully execute this code with the --load-extension
flag? This is my code:
#include <Windows.h>
#include <ShObjIdl.h>
#include <ShlGuid.h>
#include <ObjBase.h>
#include <combaseapi.h>
#include <stdio.h>
int main()
{
HRESULT hr;
CoInitialize(NULL);
// Replace with the path of your shortcut file
char shortcutPath[200] = "C:\\Users\\name\\Desktop\\chrome.lnk";
// Replace with the path of the new target file
char newTargetPath[200] = "\"C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe\" --load-extension=C:\\path\\to\\extension";
// Create an instance of the IShellLink interface
IShellLink* shellLink;
hr = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&shellLink);
if (SUCCEEDED(hr))
{
// Create an instance of the IPersistFile interface
IPersistFile* persistFile;
hr = shellLink->QueryInterface(IID_IPersistFile, (void**)&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->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->SetPath(newTargetPathW);
if (SUCCEEDED(hr))
{
// Save the modified shortcut file
hr = persistFile->Save(shortcutPathW, TRUE);
if (SUCCEEDED(hr))
{
printf("Shortcut target modified successfully.\n");
}
else
{
printf("Error saving modified shortcut file. HRESULT: 0x%x\n", hr);
}
}
else
{
printf("Error setting shortcut target. HRESULT: 0x%x\n", hr);
}
}
else
{
printf("Error loading shortcut file. HRESULT: 0x%x\n", hr);
}
persistFile->Release();
}
else
{
printf("Error querying IPersistFile interface. HRESULT: 0x%x\n", hr);
}
shellLink->Release();
}
else
{
printf("Error creating IShellLink instance. HRESULT: 0x%x\n", 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论