Solved

Process ID --> Main window HWND, how?

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How do you programatically show and hide the Windows 10 On-Screen-Keyboard? 3 650
ORA-01403: no data found 43 98
Detect CR LF to each line 12 158
sumHeights  challenge 17 70
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
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.

813 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

14 Experts available now in Live!

Get 1:1 Help Now