Solved

Process ID --> Main window HWND, how?

Posted on 1997-07-24
2
891 Views
Last Modified: 2013-11-20
(win32)
What I want to do: Run an application the user selects and close it later after some event occurs.
What I learned so far: I can use CreateToolhelp32Snapshot and Process32First(etc) to freeze the current processes/modules data of the running applications. I know from the modules data I can compare the EXE file name to the one I know I executed, so now I
got the application's PROCESS ID, now I want to use that process ID to get the HWND for that application main window (or vice versa, get a process id according to a HWND)
I dont want to use the TerminateProcess because I want to send the program a WM_CLOSE, not unconditional termination.

In short, I want the way to get an application's main HWND from its process ID. I see it happening in Spy++ when I choose a window and can go to its Process information.

Thanks.
0
Comment
Question by:d3dman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 3

Accepted Solution

by:
jaba earned 100 total points
ID: 1302793
You can enumerate all toplevel windows , call GetWindowThreadProcessId for each and find window with known process ID .

static HWND ProcHwnd;
BOOL CALLBACK EnumProc(HWND hwnd , LPARAM lParam)
{
HANDLE hProc = (HANDLE) lParam;
DWORD tmpID;
GetWindowThreadProcessId(hwnd , &tmpID);
if ( tmpID == hProc ) {
ProcHwnd = hwnd;
return false ;
}
return TRUE;
}


{
CreateProcess(...)
HANDLE hProc = .. ( get information from LPPROCESS_INFORMATION  lpProcessInformation parameter)
EnumWindows( EnumProc , (LPARAM)hProc );

0
 

Author Comment

by:d3dman
ID: 1302794
I'll try it, it looks like what I need. Thanks!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question