【问题标题】:Create an UiAutomation with C用 C 创建一个 UiAutomation
【发布时间】:2020-10-09 21:47:15
【问题描述】:

我在 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 winapi ui-automation


    【解决方案1】:

    发布的代码在 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'd 取决于目标语言:

    #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
    

    【讨论】:

    • 非常感谢您的全面解释。谢谢你的时间。
    猜你喜欢
    • 2014-08-12
    • 2014-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-09
    • 2014-07-20
    • 1970-01-01
    相关资源
    最近更新 更多