gpt4 book ai didi

c++ - 在 Win32 中使用 IShellLinkA 和 IPersistFile 创建文件快捷方式时生成的 Unicode 文件名

转载 作者:行者123 更新时间:2023-11-28 04:35:25 25 4
gpt4 key购买 nike

我有在 Win32 中创建文件快捷方式的功能:

// prototype
HRESULT CreateLink(LPCSTR lpszPathObj, LPCSTR lpszPathLink, LPCSTR lpszDesc);

当我这样调用它时:

StrCpyA(dst_file, argv[2]);
StrCpyA(src_file, argv[4]);

// concat ".lnk" to destination path
StrCatA(dst_file, ".lnk");

HRESULT res = CreateLink(src_file, dst_file, LINK_DESC);

它生成特定的快捷方式文件,然后文件名是在 argv[4] 中输入的文件名,如下所示:

e.g. : file_shortcut.exe -src 1.bmp -dst 123

文件属性中的真实文件名是一个 unicode 名称:

⸱浢p

即使我使用 MultiByteToWideChar() 将目标文件名转换为 WCHAR[] :

INT wch_size = MultiByteToWideChar(CP_UTF8, 0, lpszPathLink, -1, NULL, 0);
MultiByteToWideChar(CP_UTF8, 0, lpszPathLink, -1, wsz, wch_size);

在这个函数中,我使用了 IShellLinkIPersistFile 接口(interface)作为流:

// function implementation
HRESULT CreateLink(LPCSTR lpszPathObj, LPCSTR lpszPathLink, LPCSTR lpszDesc)
{
HRESULT hres;
IShellLinkA* psl;

// Get a pointer to the IShellLink interface. It is assumed that CoInitialize
// has already been called.
CoInitialize(NULL);

hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl);
if (SUCCEEDED(hres))
{
IPersistFile* ppf;

// Set the path to the shortcut target and add the description.
psl->SetPath(lpszPathObj);
psl->SetDescription(lpszDesc);

// Query IShellLink for the IPersistFile interface, used for saving the
// shortcut in persistent storage.
hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf);

if (SUCCEEDED(hres))
{
WCHAR wsz[MAX_PATH] = {0};

// Ensure that the string is Unicode.
INT wch_size = MultiByteToWideChar(CP_UTF8, 0, lpszPathLink, -1, NULL, 0);
MultiByteToWideChar(CP_UTF8, 0, lpszPathLink, -1, wsz, wch_size);

// Save the link by calling IPersistFile::Save.
hres = ppf->Save(wsz, TRUE);
ppf->Release();
}
psl->Release();
}
return hres;
}

有什么建议吗?

最佳答案

我明白了:

因为我将 IShellLinkA* psl; 接口(interface)定义为 ANSI 而且我必须使用 IID_IShellLinkA 而不是 IID_IShellLink在波纹管函数中:

// mismatch with IShellLinkA
hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl);

// correct way used of IID_IShellLinkA instead of IID_IShellLink
hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLinkA, (LPVOID*)&psl);

关于c++ - 在 Win32 中使用 IShellLinkA 和 IPersistFile 创建文件快捷方式时生成的 Unicode 文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51607825/

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