gpt4 book ai didi

C++:在 Windows 中创建快捷方式——使用 MinGW/g++ 的实际工作示例

转载 作者:行者123 更新时间:2023-11-28 02:52:52 27 4
gpt4 key购买 nike

经过一些研究,我找到了如何使用 C++/MinGW/g++ 在 Windows 中创建快捷方式的解决方案:

#include <stdio.h>
#include <windows.h>
#include <Shlobj.h>
#include <objidl.h>
#include <iostream>

using namespace std;

HRESULT CreateLink(
LPCWSTR FilePath,
LPCWSTR LnkPath,
LPCWSTR LnkDesc,
LPCWSTR WorkDir) {


CoInitialize(NULL);
IShellLinkW* psl;

HRESULT hres = CoCreateInstance(
CLSID_ShellLink,
NULL,
CLSCTX_INPROC_SERVER,
IID_IShellLink,
(LPVOID *) & psl);

if (SUCCEEDED(hres)) {
IPersistFile* ppf;

wstring file(L"C:\\jarlauncher.exe");
psl->SetPath((LPWSTR)file.c_str());//Not working - only first letter is visible in created shortcut properties

psl->SetWorkingDirectory(WorkDir);
psl->SetDescription(LnkDesc);

hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*) & ppf);

if (SUCCEEDED(hres)) {
hres = ppf->Save(LnkPath, TRUE);
ppf->Release();
}

psl->Release();
}

CoUninitialize();
return hres;
}

仍在测试中。问题是 psl->SetDescription(LnkDesc); - 在创建的快捷方式中只有一个符号可见。其他领域也一样。这可能是什么问题?

但我可以用 Unicode 名称创建快捷方式(添加了一些日文字符):

...
hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*) &ppf);

wstring shortCutName(L"C:\\猫が好き?.lnk");

if (SUCCEEDED(hres)) {
hres = ppf->Save((LPWSTR)shortCutName.c_str(), TRUE);
ppf->Release();
}
...

编辑:

现在的例子:

#include <Shlobj.h>
#include <tchar.h>

using namespace std;

HRESULT CreateLink(
LPCWSTR filePath,
LPCWSTR workDir,
LPCWSTR desc,
LPCWSTR iconPath,
LPCWSTR linkPath) {

CoInitialize(NULL);
IShellLinkW* psl;

HRESULT hres = CoCreateInstance(
CLSID_ShellLink,
NULL,
CLSCTX_INPROC_SERVER,
IID_IShellLinkW,
(LPVOID*) &psl);

if (SUCCEEDED(hres)) {

IPersistFile* ppf;

psl->SetPath(filePath);
psl->SetWorkingDirectory(workDir);
psl->SetDescription(desc);
psl->SetIconLocation(iconPath, 0);

hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*) &ppf);

if (SUCCEEDED(hres)) {
hres = ppf->Save(linkPath, TRUE);
ppf->Release();
}

psl->Release();
}

CoUninitialize();
return hres;
}

int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) {

CreateLink(
L"C:\\Program Files\\Appl\\Appl.exe",
L"C:\\Program Files\\Appl",
L"Location: C:\\Program Files\\Appl",
L"C:\\Program Files\\Appl\\someIcon.ico",
L"C:\\Appl.lnk");//Shortcut will be created at C:/

return 0;
}

您还需要在项目属性中添加Ole32.libUuid.lib 库文件。如果您不需要终端窗口 - 只需将 -mwindows 添加到 C++ 编译器选项。仅此而已 - 不再有“ secret ”信息。

此文件在 Windows + NetBeans IDE 8.0 上测试,带有 C/C++ 插件,以及安装在 C:\的 MinGW 和 msys,并在 NetBeans IDE 当前项目属性中正确配置。

最佳答案

听起来您没有明确定义 UNICODE,如果您定义了,那么它应该调用条件代码:

#ifdef UNICODE
#define IID_IShellLink IID_IShellLinkW
#else
#define IID_IShellLink IID_IShellLinkA
#endif

当您请求通用版本的 IShellLink 时,它会为您提供宽版本的 IShellLink 接口(interface)。在您的情况下,您必须将 IID_IShellLink 显式替换为 IID_IShellLinkW 并且它会在您调用 CoCreateInstance 时实例化宽版本,因此你得到了所有广泛的例程。

The headers look somewhat different between the official MS SDK and the mingw conversion (I think it's accomplished using the __MINGW_NAME_AW() macro in those headers.

关于C++:在 Windows 中创建快捷方式——使用 MinGW/g++ 的实际工作示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22647661/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com