COM UIA事件仅在注销后调用



我通过C++/CLI接口使用MS UIA(COM),而我的C#应用程序正在使用该C++/CLI界面(让我们将此接口.dll称为uiacpp)

我在uiacpp中创建了事件处理机制,主要遵循中的示例https://msdn.microsoft.com/en-us/library/windows/desktop/ff625914(v=vs.85).aspx

我面临的问题是,我注册到UIA的事件处理程序只有在我注销同一事件(每次都相同/不同的事件/事件类型和测试)后才会被调用。当我注册事件时,我可以看到我的事件类的QueryInterface方法被调用了两次,显然是从UIA调用的,所以UIA对它做了一些事情。然后我在测试中触发了事件,但什么也没发生。在我注销事件的那一刻,QueryInterface被调用了几次,然后事件处理程序被调用,然后发布方法被调用,用于UIA进行的剩余引用(此时大约还有6个),以清理内容。

这是代码:

C++/CLI类:

class CppUIAutomationEventHandler :
public ::IUIAutomationEventHandler
{
private:
LONG _refCount;
public:
int _eventCount;
gcroot<UIAMan::IUIAutomationEventHandler^> myHandler;
static std::list<IUIAutomationEventHandler*> *EventRegister;
// Constructor.
CppUIAutomationEventHandler() : _refCount(1), _eventCount(0)
{
}
// Constructor.
CppUIAutomationEventHandler(
UIAMan::IUIAutomationEventHandler^ aHandler)
: _refCount(1)
, _eventCount(0)
, myHandler(aHandler)
{
}
// IUnknown methods.
ULONG STDMETHODCALLTYPE AddRef()
{
ULONG ret = InterlockedIncrement(&_refCount);
return ret;
}
ULONG STDMETHODCALLTYPE Release()
{
ULONG ret = InterlockedDecrement(&_refCount);
if (ret == 0)
{
delete this;
return 0;
}
return ret;
}
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** ppInterface)
{
if (riid == __uuidof(IUnknown) || riid == __uuidof(IUIAutomationEventHandler))
*ppInterface = static_cast<IUIAutomationEventHandler*>(this);
else
{
*ppInterface = NULL;
return E_NOINTERFACE;
}
this->AddRef();
return S_OK;
}
// IUIAutomationEventHandler methods
HRESULT STDMETHODCALLTYPE HandleAutomationEvent(::IUIAutomationElement * pSender, EVENTID eventID)
{
_eventCount++;
myHandler->HandleAutomationEvent(gcnew CUIAutomationElement(pSender, false), eventID);
return S_OK;
}
};

这里有一个ref(托管c++)类方法,c#调用它来注册事件(通过使用末尾的最后一个代码):

void CUIAutomation::AddAutomationEventHandler(
int eventId
, IUIAutomationElement^ element
, TreeScope scope
, IUIAutomationCacheRequest^ cacheRequest
, IUIAutomationEventHandler^ handler)
{
::IUIAutomationElement* el = safe_cast<CUIAutomationElement^>(element)->getElement();
::IUIAutomationEventHandler* _handler = new CppUIAutomationEventHandler(handler);
LastHResult = puia->AddAutomationEventHandler(
eventId
, el
, (::TreeScope)(int)scope
, (cacheRequest != nullptr) ? ((CUIAutomationCacheRequest^)cacheRequest)->getElement() : NULL
, _handler);
CppUIAutomationEventHandler::EventRegister->push_back(_handler);
};

我正在使用一个处理程序列表来注销它们。puia也是一个COM指针,它是由以下人员创建的:

CUIAutomation::CUIAutomation()
{
CoInitializeEx(NULL, COINIT_MULTITHREADED);
::IUIAutomation* _puia;
HRESULT hr = CoCreateInstance(CLSID_CUIAutomation, NULL,
CLSCTX_INPROC_SERVER, IID_IUIAutomation,
(void**)&_puia);
if (SUCCEEDED(hr))
puia = _puia;
}

最后,这是C#调用:

使用uiacpp:实现automationhandler类

class AutomationHandler : IUIAutomationEventHandler
{
public AutomationHandler()
{
}
public void HandleAutomationEvent(IUIAutomationElement sender, int eventId)
{
Console.WriteLine("IUIAutomationEventHandler called");
}
}

和C#注册/注销行:

var aHandler = new AutomationHandler();
uia.AddAutomationEventHandler(UIA_EventIds.UIA_Window_WindowOpenedEventId, uia.GetRootElement(), TreeScope.TreeScope_Subtree, null, aHandler);
// for debugging
bool loop = true;
while(loop)
{
Thread.Sleep(500);
}
uia.RemoveAutomationEventHandler(UIA_EventIds.UIA_Window_WindowOpenedEventId, uia.GetRootElement(), aHandler);

这些COM事件是通过windows消息循环调度的。

再加上您没有在注册和注销之间泵送消息,导致事件延迟,直到您注销并返回主消息循环。

一种解决方案是使用await Task.Delay而不是阻塞睡眠。

最新更新