Click here to Skip to main content
15,849,829 members
Please Sign up or sign in to vote.
2.89/5 (7 votes)
See more:
How can one find and enumerate all window handles associated with a single PID (Process ID), for example when a program uses multiple windows from a single process.

What I have tried:

I published what I think to be the answer for this question (for the benefit of the Code Project community). Please feel free to publish better answers.
Posted
Updated 27-Feb-20 11:05am
v2

The following code locates the handles of all windows per a given PID.

#include <vector>
#include <Windows.h>

void GetAllWindowsFromProcessID(DWORD dwProcessID, std::vector<HWND>& vhWnds)
{
    // find all hWnds (vhWnds) associated with a process id (dwProcessID)
    HWND hCurWnd = NULL;
    do
    {
        hCurWnd = FindWindowEx(NULL, hCurWnd, NULL, NULL);
        if (hCurWnd != NULL)
        {
            DWORD curProcessID = 0;
            GetWindowThreadProcessId(hCurWnd, &curProcessID);
            if (curProcessID == dwProcessID)
            {
                vhWnds.push_back(hCurWnd);  // add the found hCurWnd to the vector
                
                wprintf(L"Found hWnd %d\n", (int)hCurWnd);
            }
        }
    } while (hCurWnd != NULL);
}
 
Share this answer
 
v3
Comments
Richard Deeming 11-Jan-18 12:25pm    
Why did you post this question if you already knew the answer?
Michael Haephrati 11-Jan-18 12:44pm    
There is nothing wrong with that. The purpose of the Q&A is to publish questions, as long as they aren't already published, for the benefit of the entire community. Next time someone will look for this question he will find it and even though I published my own solution, I didn't mark the question is solved and awaiting other solutions (maybe better than mine).
Richard Deeming 11-Jan-18 13:11pm    
No. The purpose of QA is to ask for help to answer a question which you don't already know the answer to.

If you've come up with a solution that you think would benefit the community, then publish it as a tip[^].
KulaGGin 23-Jul-23 4:38am    
This is absolutely a wrong answer. First of all, you're redefining your dwProcessID argument with the local variable dwProcessID that you define later and overwrite it with 0, so your local variable hides the argument and the argument isn't even used anywhere. So it's already broken. But even if you remove your local variable and use the argument, the function GetWindowThreadProcessId doesn't locate handle of a window by process id, the function takes in a valid window handle and the identifier of the process that created the window.
This function is completely broken and doesn't answer the question.
Michael Haephrati 23-Jul-23 6:59am    
Thank you for pointing out these bugs. I changed the code
The above code has the same name for both parameter passed to the function from main and local variable inside the function.
i.e. DWORD dwProcessID = 0;

which will cause it to enumerate all the windows from all the processes.

change the local variable name to something else and use that in GetWindowThreadProcessId() function and comparison too.
 
Share this answer
 
Comments
KulaGGin 23-Jul-23 4:42am    
"which will cause it to enumerate all the windows from all the processes"
It won't do that. GetWindowThreadProcessId doesn't locate handle of a window by process id, the function takes in a valid window handle and the identifier of the process that created the window.
Also, he tries to get a window using FindWindowEx and passes a null hCurWnd, which will return him null.
Then the do-while will run once and exit. The GetWindowThreadProcessId won't return anything meaningful, will probably leave dwProcessID at 0, then it will push null hCurWnd to the vector, print, exit the loop and then the function.
There's like everything wrong with that function, feels like it was a troll answer or something.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900