'Why does setting a PropertyChangedEventHandler on the Desktop not work when TreeScope_Children is used?

I'm working in C++ using Microsoft's UIAutomation framework. I have a need to be notified when any top-level windows on the desktop have their title changed. In my understanding, I should just be able to register a PropertyChangedEventHandler on the desktop to watch for UIA_NamePropertyID changes. The scope of that handler should be TreeScope_Children, because I don't care about the lower levels of the windows.

The code should look like this:

IUIAutomationPropertyChangedEventHandler* eHandler = <create a handler>;
IUIAutomationElement* deskTopElement = <retrieve the desktop element>;
CComSafeArray<PROPERTYID> propArray(1);
propArray[0] = UIA_NamePropertyId;

uiaInterface->AddPropertyChangedEventHandler(deskTopElement, TreeScope_Children, NULL, eHandler, propArray);

I get an HRESULT of S_OK, and things look all right. However, I never get any events.

If I change the scope to TreeScope_Descendants, then I start getting all the desired events, those of the top-level window title changes, but also ALL the lower-level ones. This means, to me, that I'm setting the event handler up correctly, it's just unable to process the TreeScope_Children scope correctly.

I'm rather confused about this, because if I use AccEvent.exe to look for those events, it works as I expect: I select Desktop as the entity, and "Immediate children" from which to include events. I select the Name PropertyID and let it go to town. I get all the events I expect, and none of what I don't want.

So, does anyone have any ideas as to what I'm doing wrong?

Note that I can work around this by gathering all the immediate children of the desktop, then setting a similar handler on each of them with scope TreeScope_Element, then watching for new windows and setting a handler on each new one. But that seems rather overkill for such a (seemingly) simple task.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source