我目前正在尝试移植示例 Resolving a Shortcut在 MSDN 上使用 MinGW 4.8.1 构建的 QT 应用程序。
我的代码(为了简短而去除了错误检查)目前看起来像这样:
QFileInfo shortcut("C:\\Users\\MyUserName\\ShortCut.lnk");
HRESULT apiResult;
IShellLink *shellLink;
apiResult = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
IID_IShellLink, (LPVOID*) &shellLink);
IPersistFile *persistFile;
apiResult = shellLink->QueryInterface(IID_IPersistFile, (void**) &persistFile);
WCHAR shortcutLocationWchar[MAX_PATH];
QString shortcutLocation = QDir::toNativeSeparators(shortcut.absoluteFilePath());
shortcutLocation.toWCharArray(shortcutLocationWchar);
apiResult = persistFile->Load(shortcutLocationWchar, STGM_READ);
apiResult = shellLink->Resolve(NULL, SLR_NO_UI);
WCHAR shortcutTargetWchar[MAX_PATH];
WIN32_FIND_DATA winFindData;
apiResult = shellLink->GetPath(shortcutTargetWchar, MAX_PATH, &winFindData, 0);
QString shortcutTarget = QString::fromWCharArray(shortcutTargetWchar);
此时 IPersistFile::Load
失败,返回值 0x80070002
,its API Document 中均未定义,winerr.h
header 和 Google 似乎都没有得出任何有用的结果。
这里出了什么问题有什么建议吗?
最佳答案
我错过了 API documentation for QString::toWcharArrar() 中的重要一行:
Note: This function does not append a null character to the array.
因此将快捷方式文件名转换为 WCHAR
数组的正确方法是
WCHAR shortcutLocationWchar[MAX_PATH];
QString shortcutLocation = QDir::toNativeSeparators(shortcut.absoluteFilePath());
int l = shortcutLocation.toWCharArray(shortcutLocationWchar);
shortcutLocationWchar[l] = L'\0';
关于c++ - IPersistFile 加载未定义的返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25038362/