Click here to Skip to main content
15,913,773 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: How do we window associated with a process using the process id Pin
Paolo Messina6-Jul-00 22:31
professionalPaolo Messina6-Jul-00 22:31 
You can enumerate all the top level windows, then call GetWindowThreadProcessId on each one until you find the same process id. However you can't say if it's the only top level window created by that process.
Try code like this:
<br />
#define TIMEOUT 20*1000<br />
<br />
struct ProcessWindow<br />
{<br />
	DWORD processID;<br />
	DWORD threadID;<br />
	HWND hwnd;<br />
};<br />
<br />
static BOOL CALLBACK EnumWindowsProc(<br />
  HWND hwnd,					// handle to the window<br />
  ProcessWindow *lParam   // application-defined value<br />
)<br />
{<br />
	DWORD processID = NULL;<br />
	DWORD threadID = GetWindowThreadProcessId(hwnd, &processID);<br />
<br />
	if (processID == lParam->processID && threadID == lParam->threadID)<br />
	{<br />
		lParam->hwnd = hwnd;<br />
		return FALSE;	// stop enum<br />
	}<br />
<br />
	return TRUE;<br />
}<br />
<br />
HWND StartApp(LPCTSTR path)<br />
{<br />
	STARTUPINFO si;<br />
	ZeroMemory(&si, sizeof(STARTUPINFO));<br />
<br />
	si.cb = sizeof(STARTUPINFO);<br />
	si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;<br />
	si.wShowWindow = SW_HIDE;<br />
<br />
	PROCESS_INFORMATION pi;<br />
<br />
	TCHAR buf[MAX_PATH];<br />
	lstrcpyn(buf, path, MAX_PATH);<br />
<br />
	if (!CreateProcess(NULL, buf, NULL, NULL, FALSE, CREATE_DEFAULT_ERROR_MODE |<br />
		/*CREATE_NEW_CONSOLE | CREATE_NEW_PROCESS_GROUP | CREATE_SEPARATE_WOW_VDM<br />
		| DETACHED_PROCESS |*/ NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi))<br />
		return NULL;<br />
<br />
	if(WaitForInputIdle(pi.hProcess, TIMEOUT) != 0)<br />
		return NULL;<br />
<br />
	// get the main window<br />
<br />
	ProcessWindow pw;<br />
	pw.hwnd = NULL;<br />
	pw.processID = pi.dwProcessId;<br />
	pw.threadID = pi.dwThreadId;<br />
<br />
	if (EnumWindows((WNDENUMPROC)EnumWindowsProc, (LPARAM)(LPVOID)&pw))<br />
		return NULL;<br />
<br />
	return pw.hwnd;<br />
}<br />


I've used it with success. I hope it will work also for you.
Cheers,
Paolo.
QuestionSDI with an MDI style close button? Pin
kumaichi6-Jul-00 13:50
kumaichi6-Jul-00 13:50 
GeneralChanging the background color of a read-only CEdit Pin
Joan Murt6-Jul-00 10:11
sussJoan Murt6-Jul-00 10:11 
GeneralRe: Changing the background color of a read-only CEdit Pin
Sam Hobbs7-Jul-00 6:07
Sam Hobbs7-Jul-00 6:07 
GeneralRe: Changing the background color of a read-only CEdit Pin
Philip Nicoletti7-Jul-00 6:46
Philip Nicoletti7-Jul-00 6:46 
GeneralThank you for your help, but there is still a little problem Pin
Joan Murt11-Jul-00 9:27
sussJoan Murt11-Jul-00 9:27 
GeneralRe: Changing the background color of a read-only CEdit Pin
Joan Murt12-Jul-00 8:02
sussJoan Murt12-Jul-00 8:02 
GeneralCMonthCalCtrl -> Need help on getting Date... Pin
MaverickTC6-Jul-00 9:55
MaverickTC6-Jul-00 9:55 
GeneralGet active view from modeless dialog Pin
Christoph Rupp6-Jul-00 8:33
sussChristoph Rupp6-Jul-00 8:33 
GeneralRe: Get active view from modeless dialog Pin
Mike Dunn6-Jul-00 8:50
Mike Dunn6-Jul-00 8:50 
GeneralRe: Get active view from modeless dialog Pin
Christoph Rupp6-Jul-00 21:45
sussChristoph Rupp6-Jul-00 21:45 
GeneralRe: Get active view from modeless dialog Pin
Paolo Messina6-Jul-00 23:07
professionalPaolo Messina6-Jul-00 23:07 
GeneralWriting VxDs in VC Pin
Sajid Siraj6-Jul-00 7:25
Sajid Siraj6-Jul-00 7:25 
QuestionHow to scroll to a row in a CListCtrl Pin
Tony10-Jul-00 3:43
Tony10-Jul-00 3:43 
AnswerRe: How to scroll to a row in a CListCtrl Pin
Mike Dunn10-Jul-00 6:26
Mike Dunn10-Jul-00 6:26 
Generalneed help with PJNaughter's CSerialPort class Pin
acidmarco9-Jul-00 23:13
acidmarco9-Jul-00 23:13 
GeneralRe: need help with PJNaughter's CSerialPort class Pin
Remus Lazar10-Jul-00 22:23
Remus Lazar10-Jul-00 22:23 
GeneralWindows Address Book Pin
Cirmu Nicolae9-Jul-00 22:53
sussCirmu Nicolae9-Jul-00 22:53 
GeneralFederated Databases vs. Distributed Access Pin
Member 11569-Jul-00 11:50
Member 11569-Jul-00 11:50 
GeneralFederated Databases vs. Distributed Access Pin
Member 11569-Jul-00 11:49
Member 11569-Jul-00 11:49 
Questioncan any body help me Pin
David9-Jul-00 10:11
David9-Jul-00 10:11 
AnswerRe: can any body help me Pin
phoenix11-Jul-00 0:17
phoenix11-Jul-00 0:17 
GeneralWith external resourcefile dialog with Custom Control not loaded Pin
M.Vo.9-Jul-00 3:24
M.Vo.9-Jul-00 3:24 
GeneralRe: With external resourcefile dialog with Custom Control not loaded Pin
Paolo Messina9-Jul-00 7:13
professionalPaolo Messina9-Jul-00 7:13 
GeneralRe: With external resourcefile dialog with Custom Control not loaded Pin
M.Vo.9-Jul-00 10:12
M.Vo.9-Jul-00 10:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.