How can I find the Window Handle from Process ID?
Question
Monday, March 14, 2016 7:30 AM
Hi All,
I wish to find the Window Handle from Process ID, and wish to check that particular window is Active or not. But "FindWindowEx" is not working properly. My test exe is running on "System" level, and the process I am trying to find the window handle is running on "User" level. Below are the sample code I used:
BOOL CheckWindowVisible(DWORD dwProcessId)
{
HWND hwnd = GetWindowHandle(dwProcessId);
BOOL bVisible = IsWindowVisible(hwnd);
if(bVisible)
{
return TRUE;
}
return FALSE;
}
HWND GetWindowHandle(DWORD dwProcessId)
{
try
{
HWND prevWindow = 0;
while(true)
{
HWND desktopWindow = GetDesktopWindow();
if (!desktopWindow)
break;
HWND nextWindow = FindWindowEx(desktopWindow, prevWindow, NULL, NULL);
if (!nextWindow)
break;
DWORD procId = 0;
GetWindowThreadProcessId(nextWindow, &procId);
if (procId == dwProcessId)
{
wchar_t windowText[1024];
if (IsWindowVisible(nextWindow) && !IsIconic(nextWindow) && GetWindowText(nextWindow, (LPSTR)windowText, sizeof(windowText) / sizeof(wchar_t))
&& !GetParent(nextWindow))
return nextWindow;
}
prevWindow = nextWindow;
}
}
catch(...)
{
return 0;
}
return 0;
}
Tried with "EnumWindows" also, but no luck.
http://stackoverflow.com/questions/1888863/how-to-get-main-window-handle-from-process-id#
Kindly help me to solve this issue.
R-VR
All replies (7)
Wednesday, March 16, 2016 7:47 AM ✅Answered
Hi,
Thank you come back.
Please see the article https://blogs.technet.microsoft.com/askperf/2007/07/24/sessions-desktops-and-windows-stations/
From the article what I know. A session consists of all of the processes and other system objects that represent a single user’s logon session.
a session may contain more than one Windows Station and each windows station can have multiple desktops.
Only one windows station is permitted to interact with the user at the console; this is called Winsta0.
So you want to check whether the window is focused or minimized. You should use the Winsta0.
Hope my reply to help you.
Best Regards,
Hart
Wednesday, March 16, 2016 10:48 AM ✅Answered
Hi Hart,
Many thanks for your reply.
I will give you some additional information about my test application. My test application is a Windows Service application developed with VS 2008 (OS- XP), contains one exe file (with respect to Windows service- in session zero). And one driver which is running on Windows kernel level and collecting the information about all the processes (information includes- process id, parent process id, created or destroyed. The driver is passing those information into the exe file (session zero), and the exe file can successfully call "OpenProcess" with respect to the process id. But we can't check the window (related to the process id) is focused or minimized.
So how can I check the window associated with process id is focused or minimized in session zero without using user privileged exe.
R-VR
In Windows XP services and applications were not isolated from each other. This was changed beginning with Vista to improve security. If your solution is reliant on the obsolete and unsupported configuration present in XP it will not function on any version of windows that is currently supported, beginning with Vista through Windows 10.
Monday, March 14, 2016 10:57 AM | 1 vote
What do you mean by saying that your test program runs at "System" level and that the target process is at "User" level?
Window's services run in a different session and have a different desktop than user GUI or console mode applications.
Monday, March 14, 2016 12:41 PM
What do you mean by saying that your test program runs at "System" level and that the target process is at "User" level?
Window's services run in a different session and have a different desktop than user GUI or console mode applications.
Hi RLWA32,
Thanks for your reply. My test program runs at "System" level means it is a Windows service. And I am trying to capture the process which running under local user privilege. So kindly help me to solve this issue.
R-VR
Monday, March 14, 2016 12:48 PM
Thanks for your reply. My test program runs at "System" level means it is Windows service. And I am trying to capture the process which running under local user privilege. So kindly help me to solve this issue.
R-VR
What you want to do cannot be done directly by a service due to the session 0 isolation. See https://blogs.technet.microsoft.com/askperf/2007/04/27/application-compatibility-session-0-isolation/
You could use a helper application that runs in the user's session and communicates with your service using IPC.
Tuesday, March 15, 2016 3:02 AM
Hi R-VR,
About the session communicate with session 0 by desktop services, you can see the link:
Hope the article can help you.
If you have anything. please feel free to contact me.
Best Regards,
Hart
Wednesday, March 16, 2016 6:00 AM
Hi Hart,
Many thanks for your reply.
I will give you some additional information about my test application. My test application is a Windows Service application developed with VS 2008 (OS- XP), contains one exe file (with respect to Windows service- in session zero). And one driver which is running on Windows kernel level and collecting the information about all the processes (information includes- process id, parent process id, created or destroyed. The driver is passing those information into the exe file (session zero), and the exe file can successfully call "OpenProcess" with respect to the process id. But we can't check the window (related to the process id) is focused or minimized.
So how can I check the window associated with process id is focused or minimized in session zero without using user privileged exe.
R-VR