1


I was wondering whether you can convert a handle to a window "HWND". I need to call the "PostMessage" function using the "FindWindow" method.

I currently have to source

HANDLE mainProcess;
BOOL APIENTRY ATTACH_PROCESS(int ProcessID)
{

    mainProcess = OpenProcess(PROCESS_ALL_ACCESS, true, ProcessID);

    return TRUE;
}
BOOL APIENTRY SEND_INPUT(/*NOT USED FOR THIS SAMPLE*/ const char* String, bool Keydown)
{

    int ToDo = WM_KEYUP;
    if (Keydown)
        ToDo = WM_KEYDOWN;
    return PostMessage((HWND)mainProcess, ToDo, VK_TAB, NULL); 
}
Rasmus Søborg
  • 3,597
  • 4
  • 29
  • 46
  • 5
    No. There's a reason why there are two different data types (`HANDLE` vs. `HWND`) for process handles and window handles. Not to mention that a process can have more than one window, so there's definitely not a one-to-one correspondance. What are you actually trying to do? – In silico May 03 '12 at 18:04
  • I am trying to make a "Easy to use" API for the making of a "World of Warcraft" bot. The window HWND I am trying to find is the HWND of the Main window. I do not know if I am able to find that :S However; It is not a big issue, I am just educate myself at C++ing. – Rasmus Søborg May 03 '12 at 18:08

2 Answers2

4

No. A process can create multiple windows. Since there does not exist a 1-to-1 mapping, such a function would not make sense.

On the other hand, it is certainly possible to have a function which returns a list of windows created by a process.

Mike Kwan
  • 24,123
  • 12
  • 63
  • 96
2

call GetProcessId() using the mainProcess handle to get the ProcessID.

call EnumWindows()

For Each Window, call GetWindowThreadProcessId() to get the ProcessId of the process associated with the window.

Compare the ProcessID's, if they match -- you've found the HWND you want.

This is a somewhat expensive task, so best to find the hwnd you want upfront and just store it.

MessyHack
  • 133
  • 3
  • 1
    Warning: As mentioned previously -- a process can have multiple windows, so you may need additional checking on the window (classname, caption, etc.) to determine if it's the one you want. – MessyHack May 03 '12 at 18:46
  • Thanks to both of you, I have gotten an idea for overcomming this issue :]. – Rasmus Søborg May 03 '12 at 19:34