sergioMoreno
sergioMoreno

Reputation: 354

Create an UiAutomation with C

I have the following code in C to create the UiAutomation object:

#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);

However, I am getting the following error:

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

I do not know what I am doing incorrectly. Thanks for your help in advance

Upvotes: 1

Views: 441

Answers (1)

dxiv
dxiv

Reputation: 17638

The posted code compiles in C++, but in C the function expects pointers instead of references.

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

This is because CoCreateInstance is declared as:

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

But REFCLSID and REFIID are conditionally #define'd depending on the target language:

#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

Upvotes: 2

Related Questions