为什么在 Windows API 中对 CoCreateInstance 函数的调用没有类型不匹配?

Why no type mismatch in call to CoCreateInstance function in the Windows API?

我正在尝试将 this 使用 winapi 调用 WMI/COM 的示例从 C++ 翻译成 Rust,但在原始代码中有些东西我不明白。这是 C++ 代码中的函数调用:

hres = CoCreateInstance(
        CLSID_WbemLocator,             
        0, 
        CLSCTX_INPROC_SERVER, 
        IID_IWbemLocator, (LPVOID *) &pLoc);

让我疑惑的是第一个和第四个参数的类型。以第四个参数为例。如果我们看the definition of CoCreateInstance, the fourth argument is of type REFIID which in turn is defined as a *IID where an IID is defined as aGUID。所以第四个参数应该是一个指向 GUID 对象的指针。

但是如果您查看 IID_IWbemLocatorwhat I think is the definition,它被定义为 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 headers 中定义为 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