加载未定义的返回值
IPersistFile Load undefined return value
我目前正在尝试将示例解析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
,什么既不是在其API文档中定义,也不是winerr.h
头也不是谷歌似乎提出了任何有用的结果。
有什么建议吗?
我错过了API文档中QString::toWcharArrar():
的重要一行注意:此函数不向数组添加空字符。
所以将快捷文件名转换为WCHAR
数组的正确方法是
WCHAR shortcutLocationWchar[MAX_PATH];
QString shortcutLocation = QDir::toNativeSeparators(shortcut.absoluteFilePath());
int l = shortcutLocation.toWCharArray(shortcutLocationWchar);
shortcutLocationWchar[l] = L' ';
返回值0x80070002
表示系统找不到指定的文件。因此你的文件路径是不正确的。我认为你应该这样写:
QFileInfo shortcut("C:\Users\MyUserName\ShortCut.lnk");
我还会以以下方式简化您的代码:
QString shortcutLocation = shortcut.absoluteFilePath();
apiResult = persistFile->Load((LPCWSTR)shortcutLocation.constData(), STGM_READ);
最后,为什么你需要使用Windows API,并将其与Qt混合,当你可以使用完全基于Qt的更简单和更短的解决方案时。例如,我将这样做:
QFileInfo shortcut(QFileInfo shortcut("D:\downloads\sk.lnk.lnk");
QString shortcutTarget ;
if (shortcut.isSymLink()) {
shortcutTarget = shortcut.symLinkTarget();
}
相关文章:
- 编译时的 CImg 库返回对"__imp_SetDIBitsToDevice"的未定义引用
- C++ 中的编译错误:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- 编译问题:在函数"_start"中:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- collect2:错误:ld 返回 1 个退出状态未定义的引用
- boost::any 如何检查空值/未定义的值
- G++ 编译器是否在未使用返回值的情况下将 constexpr 函数视为常规函数?
- 是否未定义将对函数范围变量的引用作为值返回
- C++:私有类指针成员返回未定义的值
- 返回对未定义指针的引用
- 正在使用放置-new,复制存储,然后访问值未定义的行为
- std::is_arithmetic 为通用 lambda 中的 int 类型返回 false:未定义的行为?
- 完美的返回值,未定义的行为
- 函数的返回值未返回
- 在函数返回值中定义一个新类型
- 如果返回值未用于特定类型,则编译失败
- 未分配返回未定义对象类型引用的 C++ 函数的返回值时会发生什么情况
- C++函数返回值未分配
- 为什么我的静态方法的返回值与定义的构造函数(在 c++ 中)之间存在类型不匹配?
- 加载未定义的返回值
- 如何在函数返回值未使用时触发编译器错误