用 C 创建一个 UiAutomation

Create an UiAutomation with C

我在 C 中有以下代码来创建 UiAutomation 对象:

#include "UiAutomationclient.h"

IUIAutomation *pAutomation = NULL;
IUIAutomationElement *element = NULL;

CoInitialize(NULL);
      
EXTERN_C const CLSID CLSID_CUIAutomation;
EXTERN_C const IID IID_IUIAutomation;

HRESULT hr = CoCreateInstance(CLSID_CUIAutomation,NULL, CLSCTX_INPROC_SERVER,IID_IUIAutomation,(void**)&pAutomation);

但是,我收到以下错误:

'function': cannot convert from 'const CLSID' to 'const IID *const '
'function': cannot convert from 'const IID' to 'const IID *const '

我不知道我做错了什么。预先感谢您的帮助

发布的代码在 C++ 中编译,但在 C 中,函数需要 指针 而不是引用。

HRESULT hr = CoCreateInstance(&CLSID_CUIAutomation, NULL, CLSCTX_INPROC_SERVER, &IID_IUIAutomation, (void**)&pAutomation);

这是因为 CoCreateInstancedeclared 为:

HRESULT CoCreateInstance(
  REFCLSID  rclsid,
  LPUNKNOWN pUnkOuter,
  DWORD     dwClsContext,
  REFIID    riid,
  LPVOID    *ppv
);

但是 REFCLSIDREFIIDconditionally #define,具体取决于目标语言:

#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