Solved

Process ID --> Main window HWND, how?

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
This video discusses moving either the default database or any database to a new volume.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now