Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# c++;com如何继承IUIAutomationPropertyChangedEventHandler接口_C#_C++_Com_Ui Automation_Microsoft Ui Automation - Fatal编程技术网

C# c++;com如何继承IUIAutomationPropertyChangedEventHandler接口

C# c++;com如何继承IUIAutomationPropertyChangedEventHandler接口,c#,c++,com,ui-automation,microsoft-ui-automation,C#,C++,Com,Ui Automation,Microsoft Ui Automation,c#代码: AddPropertyChangedEventHandler中使用的处理程序 var nativeAutomation = new UIAutomationClient.CUIAutomation8(); nativeAutomation.AddPropertyChangedEventHandler(ele, UIA.TreeScope.TreeScope_Element, null, new handler(), pidarray); 它工作得很好 但是当我使用c++时: cla

c#代码:

AddPropertyChangedEventHandler中使用的处理程序

var nativeAutomation = new UIAutomationClient.CUIAutomation8();
nativeAutomation.AddPropertyChangedEventHandler(ele, UIA.TreeScope.TreeScope_Element, null, new handler(), pidarray);
它工作得很好

但是当我使用c++时:

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);
    }
}
看起来我应该在QueryInterface()中返回一个IMarshal对象

我认为它需要另一个线程来循环事件

伙计们,这个IMarshal对象怎么编码?

好的,我在

我的网络最近不稳定。没有看到页面

#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;
}
am->AddPropertyChangedEventHandlerNativeArray(ele, TreeScope_Element, NULL, a, pids, 4);