c++winapifindwindow

FindWindow in c++


Why cannot I just write:

GetWindowThreadProcessId("Name of Window", &PID);

instead of:

HWND Name = FindWindow(("Name of Window", NULL));

GetWindowThreadProcessId(Name, &PID);

What does the Handle function do ? Like, if there wasn't something special with HWND, if it just stores a string, why not just use "string", so what does it store ?

because if I do this:

cout << Name << endl;

it gives a string ???

I was thinking about if it stores a function:

GetWindowThreadProcessId(FindWindow(("Name"), NULL)), &PID);

Solution

  • From application view a window handle is an abstract value that uniquely identifies a window, see also What is a Windows Handle? or Handle in Wikipedia. The operating system might see it differently and see a window handle as a pointer to a struct with Information about the window. Or it might see the window handle as an index into an array, or as something totally different. But that is up to the operating system.

    A window title is not unique, multiple windows can exist with the same title.

    GetWindowThreadProcessId needs to know exactly on which window to work, so you cannot pass a window title to the function, but you need to pass a window handle.

    Besides, cout << hwnd_value; will not work, it will just print a pointer value, not a string.