c++ - Unicode filename generated when creating a fileshortcut with IShellLinkA and IPersistFile in Win32
问题描述
I have this function to create a file-shortcut in Win32
:
// prototype
HRESULT CreateLink(LPCSTR lpszPathObj, LPCSTR lpszPathLink, LPCSTR lpszDesc);
When I call it as bellow :
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);
It generate the specific short-cut file And then filename is as entered filename in argv[4]
like this :
e.g. : file_shortcut.exe -src 1.bmp -dst 123
But the real filename in file properties is a unicode name :
⸱浢p
Even I used MultiByteToWideChar()
to convert dest filename to WCHAR[]
:
INT wch_size = MultiByteToWideChar(CP_UTF8, 0, lpszPathLink, -1, NULL, 0);
MultiByteToWideChar(CP_UTF8, 0, lpszPathLink, -1, wsz, wch_size);
In this function I'm used of IShellLink
and IPersistFile
interfaces as fllowing :
// 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;
}
Any suggested?
解决方案
我知道了 :
因为我将IShellLinkA* psl;
接口定义为ANSI
并且我必须在下面的函数中使用 ofIID_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);
推荐阅读
- node.js - 使用 cmd 在 React Js App 中启动时出现 npm start 错误
- html - 如何将图像放置在 div 内,使其完全适合 div 内?
- tensorflow - 我们可以使用在 tensorflow 1.12 上运行的 tensorflow 对象检测 API 在单独的 GPU 上运行训练和验证吗?
- android - 在 Android Studio 3 中调试 so 文件
- java - 分布式 Hazelcast 缓存中的异步值计算
- php - 语法问题限制使用 find() MongoDB PHP 返回的文档数量
- r - R ggplot:geom_ribbon 使导出非常缓慢
- matlab - 将多个 .cvs 文件导入 MATLAB 而不将标头导入到单独的矩阵中
- flutter - 手势检测器在 3 指滑动时抛出异常
- c++ - 我编译的 exe 链接的哪个静态库需要 msvcr100d.dll?