c++ - IPersistFile 加载未定义的返回值

标签 c++ qt winapi mingw

我目前正在尝试移植示例 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 失败,返回值 0x80070002its 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/

相关文章:

qt - qt Creator 中的提升功能是什么?它是如何工作的?

c++ - 我可以编写 OpenGL 应用程序而不将其绑定(bind)到某个窗口库吗?

c++ - 在已编译的 exe 中编辑字符串变量? C++ win32

c++ - 如何检查我的窗口是否隐藏/可见?

c++ - 我的机器出问题 : how to debug outside a dev environment?

c++ - (->) 箭头运算符和 (.) 点运算符,类指针

c++ - 在 C++ 中转换为 void* 和 typedef

c++ - 使用QT C++从json中获取多个数据

c++ - 如何在 Windows 7 64 位下使用 AddMonitor() 添加 redmonnt.dll

c++ - 覆盖 QTextEdit 子类中的 keyPressEvent