Maybe the automation element does not have a native window handle. Have you used inspect.exe to verify that the focus window does have an HWND?
Why does get_CurrentNativeWindowHandle return 0 in some cases?
I am using a focus change handler to track change of focus across all applications on Windows. When I focus into the following textbox in Chrome on an empty HTML page:
<textarea></textarea>
The value of window handle I obtain is 0. I am using the following code to implement the focus change handler: https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src/WinAuto/uiauto-howto-implement-event-handlers.md#handling-focus-changed-events
I made the following change to get the window handle and print it:
HRESULT STDMETHODCALLTYPE
HandleFocusChangedEvent(IUIAutomationElement *pSender) {
_eventCount++;
UIA_HWND windowHandle;
HRESULT hr = pSender->get_CurrentNativeWindowHandle(&windowHandle);
if (SUCCEEDED(hr)) {
HWND hwnd = (HWND)windowHandle;
// this value is printed as 0
}
return S_OK;
}
Any suggestion on how to get the correct window handle in this case? The documentation (https://learn.microsoft.com/en-us/windows/win32/api/uiautomationclient/nf-uiautomationclient-iuiautomationelement-get_currentnativewindowhandle) does not specify any reasons as to why this could be 0.
Windows development | Windows API - Win32
Answer accepted by question author