?
Solved

Process ID --> Main window HWND, how?

Posted on 1997-07-24
2
Medium Priority
?
898 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 400 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

771 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