【发布时间】:2021-08-26 01:05:34
【问题描述】:
我有 C++ 代码,我试图用组件对象模型的 IFileDialog 打开一个文件选择对话框。该代码可以在 Visual Studio 中运行,但是当我在 VS Code 中键入完全相同的代码时,会出现 2 个错误:
IID_IFileOpenDialog' was not declared in this scope
和
invalid use of incomplete type 'IFileDialog' {aka 'struct IFileDialog'}
这是在 Visual Studio 中成功加载文件选择对话框的代码:
#include <iostream>
#include <objbase.h>
#include <ShObjIdl.h>
int main()
{
CoInitializeEx(nullptr, COINIT::COINIT_MULTITHREADED);
IFileDialog *fileDialog;
HRESULT hr = CoCreateInstance(CLSID_FileOpenDialog, nullptr, CLSCTX_ALL, IID_IFileOpenDialog, reinterpret_cast<void**>(&fileDialog));
if (SUCCEEDED(hr)) {
fileDialog->AddRef();
fileDialog->Show(NULL);
}
else {
std::cout << "no" << std::endl;
}
CoUninitialize();
}
在 VS Code 中,我使用的是 MinGW/gcc/g++ 编译器(抱歉,我对编译器了解不多,但我认为这三个编译器都能让我的代码运行),而在 Visual Studio 中我不是当然,在绿色播放按钮旁边显示的是本地 Windows 调试器。
是什么导致这个错误只出现在 VS Code 中?
【问题讨论】:
-
与您的问题无关,但您的
fileDialog变量需要声明为IFileOpenDialog*,因为这是您要求CoCreateInstance()输出的内容。您应该使用IID_PPV_ARGS()宏来避免这种不匹配,例如:IFileOpenDialog *fileDialog; HRESULT hr = CoCreateInstance(CLSID_FileOpenDialog, nullptr, CLSCTX_ALL, IID_PPV_ARGS(&fileDialog)); -
gcc 的哪个版本和发行版? (mingw 和 mingw-64 是不同的项目;并且是具有各种不同二进制包的源代码分发)
-
您的程序在 mingw-w64 目标 x64_64-w64-mingw32,gcc 版本 10.3.0 - MSYS2 中为我正确构建和运行
-
我确信可以将 VS Code 与 microsoft 编译器一起使用
-
@M.M 我想我用的是mingw-64,我的gcc版本是6.3.0
标签: c++ gcc visual-studio-code com ifiledialog