C++ FindWindow() does not work on Windows 11

Hoang-Dao Trinh 0 Reputation points

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);
        MessageBox(NULL, L"Failed!", L"Error", MB_OK);

    return 0;
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,316 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 106.4K Reputation points

    Try this fix: EnumWindows( EnumWindowsProc, (LPARAM)&hWnd ).

    0 comments No comments

  2. RLWA32 36,881 Reputation points

    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.


    0 comments No comments