I'm trying to get windows' names when they are created and went with the route of using accessibility hooks. However, it seems that I'm getting invalid window handles from the WINEVENTPROC struct. I'm requesting EVENT_OBJECT_SHOW in my SetWinEventHook function and checking if the idObject is OBJID_WINDOW, but I still seem to get invalid handles when using GetWindowTextW and GetWindowTextLengthW. I also already checked to see if it's a valid window handle using IsWindow but still got the same result. One instance when this happens is when hovering over the icon of an opened window in the taskbar.
My hook function
void CALLBACK WinEventProc(
HWINEVENTHOOK hWinEventHook,
DWORD event,
HWND hwnd,
LONG idObject,
LONG idChild,
DWORD dwEventThread,
DWORD dwmsEventTime
)
{
if (hwnd && idObject == OBJID_WINDOW && idChild == CHILDID_SELF) {
switch (event) {
case EVENT_OBJECT_SHOW:
int l = GetWindowTextLengthW(hwnd);
auto* txt = new wchar_t[l + 1];
if (!GetWindowTextW(hwnd, txt, l + 1)) {
std::cout << "error " << GetLastError() << "\n";
} else {
auto text = std::wstring(txt);
std::wcout << text << "\n";
}
break;
}
}
}
This is my hook creation
HWINEVENTHOOK hWinEventHook = SetWinEventHook(
EVENT_OBJECT_SHOW, EVENT_OBJECT_SHOW,
NULL, WinEventProc, 0, 0, WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS
);
I believe this is because not all handles provided may be valid window handles, if that is the case, how can I ignore these window handles?