C++ Handle as HWND?

2019-08-13 18:56发布


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); 
}

2条回答
The star\"
2楼-- · 2019-08-13 19:44

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.

查看更多
贪生不怕死
3楼-- · 2019-08-13 20:01

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.

查看更多
登录 后发表回答