How can I get the HWND of application, if I know the process ID? Anyone could post a sample please? I'm using MSV C++ 2010. I found Process::MainWindowHandle but I don't know how to use it.
Asked
Active
Viewed 5.9k times
14
-
2A process isn't limited to 1 window. – chris Jul 29 '12 at 17:37
-
1A process can have several windows, and this have several hwnds. – yuri kilochek Jul 29 '12 at 17:37
-
so, maby is it possible to get all HWNDs and select these, wich have this process ID? – Luke Jul 29 '12 at 17:41
-
What's wrong with `Process::MainWindowHandle`? isn't it what you're looking for? – Mohammad Jul 29 '12 at 17:46
-
1Possible duplicate of [How to get main window handle from process id?](http://stackoverflow.com/questions/1888863/how-to-get-main-window-handle-from-process-id) – Frédéric Hamidi Jul 29 '12 at 17:50
-
I don't understand how to do that in .net c++ – Luke Jul 29 '12 at 18:24
-
http://stackoverflow.com/questions/3269390/how-to-get-hwnd-of-window-opened-by-shellexecuteex-hprocess has some very good code doing exactly this – john ktejik Jan 07 '13 at 23:18
-
A process can have also zero windows. – Michael Chourdakis Nov 15 '19 at 11:17
4 Answers
25
HWND g_HWND=NULL;
BOOL CALLBACK EnumWindowsProcMy(HWND hwnd,LPARAM lParam)
{
DWORD lpdwProcessId;
GetWindowThreadProcessId(hwnd,&lpdwProcessId);
if(lpdwProcessId==lParam)
{
g_HWND=hwnd;
return FALSE;
}
return TRUE;
}
EnumWindows(EnumWindowsProcMy,m_ProcessId);
Andre Kirpitch
- 997
- 9
- 18
-
4One needs to point out that `PID` must belong to the same desktop (or interactive user/logon session) as the process you call this method from. Otherwise `EnumWindows` will not enumerate its window handle. For instance, you cannot use this method from a service, or if the process in question runs under a different logged on user account, or to retrieve the HWND for a screensaver if the workstation is locked, etc. – c00000fd Jun 10 '15 at 20:40
-
Just converted this code in free pascal and works like cheese for me too:) – Jako Nov 10 '15 at 15:41
4
A single PID (Process ID) can be associated with more than one window (HWND). For example if the application is using several windows.
The following code locates the handles of all windows per a given PID.
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);
DWORD dwProcID = 0;
GetWindowThreadProcessId(hCurWnd, &dwProcID);
if (dwProcID == dwProcessID)
{
vhWnds.push_back(hCurWnd); // add the found hCurWnd to the vector
wprintf(L"Found hWnd %d\n", hCurWnd);
}
}
while (hCurWnd != NULL);
}
Michael Haephrati
- 3,381
- 1
- 29
- 53
3
You can use EnumWindows and GetWindowThreadProcessId() functions as mentioned in this MSDN article.
PermanentGuest
- 5,119
- 2
- 26
- 36
2
Thanks to Michael Haephrati, I slightly corrected your code for modern Qt C++ 11:
#include <iostream>
#include "windows.h"
#include "tlhelp32.h"
#include "tchar.h"
#include "vector"
#include "string"
using namespace std;
void GetAllWindowsFromProcessID(DWORD dwProcessID, std::vector <HWND> &vhWnds)
{
// find all hWnds (vhWnds) associated with a process id (dwProcessID)
HWND hCurWnd = nullptr;
do
{
hCurWnd = FindWindowEx(nullptr, hCurWnd, nullptr, nullptr);
DWORD checkProcessID = 0;
GetWindowThreadProcessId(hCurWnd, &checkProcessID);
if (checkProcessID == dwProcessID)
{
vhWnds.push_back(hCurWnd); // add the found hCurWnd to the vector
//wprintf(L"Found hWnd %d\n", hCurWnd);
}
}
while (hCurWnd != nullptr);
}