c++ com how to inherit IUIAutomationPropertyChangedEventHandler interface

-1

c# code:

var nativeAutomation = new UIAutomationClient.CUIAutomation8();
nativeAutomation.AddPropertyChangedEventHandler(ele, UIA.TreeScope.TreeScope_Element, null, new handler(), pidarray);

the handler used in AddPropertyChangedEventHandler

class handler : UIAutomationClient.IUIAutomationPropertyChangedEventHandler
{
    public void HandlePropertyChangedEvent(UIA.IUIAutomationElement src, int propertyId, object newValue)
    {
        UIA.IUIAutomationElement sourceElement = src as UIA.IUIAutomationElement;
        Console.WriteLine(propertyId + ":" + newValue);
    }
}

it works very well

but when i using c++:

#include "stdafx.h"
#include <Windows.h>
#include "test.h"
#include "cbt.h"
#include <UIAutomationClient.h>
#include <iostream>
#include <string>
#pragma comment(lib,"testDll.lib")

class A :public IUIAutomationPropertyChangedEventHandler {
    ULONG m_ref;
public:
    A() :m_ref(0)
    {}
    virtual HRESULT STDMETHODCALLTYPE QueryInterface(const IID &id, void** p) override {
        //  return IUnknown::QueryInterface(id,p);
        REFIID d = { 0x0000001b,0x0000,0x0000, {0xC0,00,00,00,00,00,00,0x46} };// IdentityUnmarshal.
        if (id == d) {
            return E_NOINTERFACE;
        }
        *p = this;
        return S_OK;
        //return E_NOINTERFACE;
    }
    virtual ULONG STDMETHODCALLTYPE AddRef() override {
        ++m_ref;
        //return IUnknown::AddRef();
        return m_ref;
    }
    virtual ULONG STDMETHODCALLTYPE Release() override {
        //  return IUnknown::Release();
        --m_ref;
        if (!m_ref)
            delete this;
        return m_ref;
    }
    virtual HRESULT STDMETHODCALLTYPE HandlePropertyChangedEvent(
        /* [in] */ __RPC__in_opt IUIAutomationElement *sender,
        /* [in] */ PROPERTYID propertyId,
        /* [in] */ VARIANT newValue) {
        printf("dsdsdsdsddsd\n");
        return S_OK;
    };
};
int main()
{
//  cbt::Instance();
    CoInitializeEx(NULL, COINIT_MULTITHREADED);
    IUIAutomation* am = NULL;
    HRESULT hr = CoCreateInstance(__uuidof(CUIAutomation8), NULL, CLSCTX_INPROC_SERVER,
        __uuidof(IUIAutomation), (void**)&am);
    if (S_OK != hr)
        hr = CoCreateInstance(__uuidof(CUIAutomation), NULL, CLSCTX_INPROC_SERVER,
            __uuidof(IUIAutomation), (void**)&am);

    A* a = new A;
    std::string hx;
    std::getline(std::cin, hx);
    char* s = NULL;
    HWND h = (HWND)strtol(hx.c_str(), &s, 16);
    IUIAutomationElement* ele = NULL;
    am->ElementFromHandle(h, &ele);

    /*SAFEARRAY* sa = SafeArrayCreateVector(VT_I4, 0, 4);
    LONG i = 0;
    long pid = UIA_AutomationIdPropertyId;
    SafeArrayPutElement(sa, &i, &pid);
    i = 1;
    pid = UIA_BoundingRectanglePropertyId;
    SafeArrayPutElement(sa, &i, &pid);
    i = 2;
    pid = UIA_ClassNamePropertyId;
    SafeArrayPutElement(sa, &i, &pid);
    i = 3;
    pid = UIA_NamePropertyId;
    SafeArrayPutElement(sa, &i, &pid);
    am->AddPropertyChangedEventHandler(ele, TreeScope_Element, NULL, p,sa );
    SafeArrayDestroy(sa);*/

    PROPERTYID *pids = new PROPERTYID[4];
    pids[0] = UIA_AutomationIdPropertyId;
    pids[1] = UIA_BoundingRectanglePropertyId;
    pids[2] = UIA_ClassNamePropertyId;
    pids[3] = UIA_NamePropertyId;
    am->AddPropertyChangedEventHandlerNativeArray(ele, TreeScope_Element, NULL, a, pids, 4);

    getchar();
    CoUninitialize();
    return 0;
}

so,it is very easy in c#.

but,with c++,i need to override Addref(), Release(), QueryInterface().

error occurs when call

am->AddPropertyChangedEventHandlerNativeArray(ele, TreeScope_Element, NULL, a, pids, 4);

looks like i should return a IMarshal object in the QueryInterface().

i think it needs a otherThread to loop events.

guys , how to code this IMarshal object?

c#
c++
com
ui-automation
microsoft-ui-automation
asked on Stack Overflow May 22, 2020 by rookie

1 Answer

0

ok,i got the answer at https://docs.microsoft.com/en-us/windows/win32/winauto/uiauto-howto-implement-event-handlers

my internet unstable these days.didn't see the page

answered on Stack Overflow May 22, 2020 by rookie

User contributions licensed under CC BY-SA 3.0