Solved

Process ID --> Main window HWND, how?

Posted on 1997-07-24
2
887 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

Technology Partners: 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

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

749 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