-3

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);
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
user3742860
  • 100
  • 2
  • 13
  • What happens if you have two windows with the same name? Stupid question I'm sorry. – Jonathan Potter Oct 21 '15 at 08:49
  • ... That is my question... what does the HWND store ? it does not store a string so what does it store then ?? – user3742860 Oct 21 '15 at 08:52
  • A `HWND` doesn't store anything. It is a (hashed) index into a kernel controlled container of `WND` structures. Each `WND` structure stores the respective window's position, size, parent and owner window, window text, and so on. With that information you can probably guess, how the window manager implements `FindWindow`. – IInspectable Oct 21 '15 at 13:05

1 Answers1

1

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.

Community
  • 1
  • 1
Werner Henze
  • 16,404
  • 12
  • 44
  • 69
  • so the HWND stores a pointer to the structure, and the proces id is offset from the main entry point in that structure ? – user3742860 Oct 21 '15 at 08:54
  • @user3742860 Might be something like that. If you want to know it exactly, then you'll have to ask Microsoft. – Werner Henze Oct 21 '15 at 09:06