Try this fix: EnumWindows( EnumWindowsProc, (LPARAM)&hWnd )
.
C++ FindWindow() does not work on Windows 11
I am trying to catch HWND by FindWindow or FindWindowA or FindWindowW but not successful, I wonder to have ideas how to fix them. Please help
My code:
#include <windows.h>
#include <iostream>
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
char buffer[128];
int written = GetWindowTextA(hwnd, buffer, 128);
if (written && strstr(buffer, "Mozilla Firefox") != NULL) {
*(HWND*)lParam = hwnd;
return FALSE;
}
return TRUE;
}
HWND GetFirefoxHwnd()
{
HWND hWnd = NULL;
EnumWindows(EnumWindowsProc, (LPARAM)hWnd);
return hWnd;
}
int main()
{
HWND hwndFirefox = GetFirefoxHwnd();
if (hwndFirefox != NULL)
MessageBox(NULL, L"Firefox found", L"Successful", MB_OK);
else
MessageBox(NULL, L"Failed!", L"Error", MB_OK);
return 0;
}
2 answers
Sort by: Most helpful
-
Viorel 106.4K Reputation points
2023-10-27T16:04:30.3533333+00:00 -
RLWA32 36,881 Reputation points
2023-10-27T16:36:05.8633333+00:00 In addition to the coding correction posted by @Viorel, consider the following:
Any window caption could contain the substring "Mozilla Firefox". You would have better luck if you searched for the window class instead. In my copy of Firefox 119.0 the class of the window on the desktop is "MozillaWindowClass".
Also, a user can have multiple Firefox windows open on the desktop.
For example, Spy++ shows the following for 3 open Firefox windows on the desktop.