我正在尝试翻译 this使用 winapi 从 C++ 调用 WMI/COM 到 Rust 的示例,但在原始代码中有一些我不理解的地方。这是 C++ 代码中的函数调用:
hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &pLoc);
令我困惑的是第一个和第四个参数的类型。以第四个参数为例。如果我们看the definition of CoCreateInstance , 第四个参数是 REFIID
类型,它又是 is defined as a *IID
其中 IID
is defined as a GUID
。所以第四个参数应该是一个指向 GUID
对象的指针。
但是如果你看what I think is the definition IID_IWbemLocator
,它被定义为 GUID
(使用 DEFINE_GUID
宏)。所以看起来我们将 GUID
作为参数传递,而实际上我们应该传递指向 GUID
的指针。 为什么这段代码可以编译?
相比之下,在 Rust 中,我必须传递对 IID_IWbemLocator
的引用。
抱歉,如果答案真的很简单,我对 C++ 和 Winapi 都不熟悉。
最佳答案
the third argument is of type REFIID which in turn is defined as a *IID where an IID is defined as a GUID. So the third argument should be a pointer to a GUID object.
您链接的文档不正确。我怀疑它是为 C 编写的。
在 C++ 中,REFIID 和 REFCLSID 被定义为 Windows header 中的 IID 引用,如下所示:
#ifdef __cplusplus
#define REFIID const IID &
#else
#define REFIID const IID * __MIDL_CONST
#endif
#ifdef __cplusplus
#define REFCLSID const IID &
#else
#define REFCLSID const IID * __MIDL_CONST
#endif
关于c++ - 为什么在 Windows API 中调用 CoCreateInstance 函数时没有类型不匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59362354/