Click to See Complete Forum and Search --> : Getting an HWND from a process started with CreateProcess
Bob Davis
February 6th, 2003, 04:32 PM
I'm working on a simple console application. It takes some command line parameters containing a path of a Win32 executable. What I need to do is execute that program, maximize the window, then capture its contents and output it to a file. My question is, after creating the process, how should I go about getting the HWND for the process' main window? After that, maximizing the window and capturing the contents shouldn't be a problem, but I'm not sure how to go out there and get the window handle. I'd appreciate any input you guys have on this. Thanks.
Bengi
February 6th, 2003, 05:16 PM
try:
HWND FindWindow(
LPCTSTR lpClassName, // pointer to class name
LPCTSTR lpWindowName // pointer to window name
);
If the function succeeds, the return value is the handle to the window that has the specified class name and window name.
thpsthc
February 6th, 2003, 05:25 PM
I'm trying to do the same sort of thing you are(although I'm not capituring the program I'm starting), but here's what I try(using enumwindows):
PROCESS_INFORMATION pi;
CreateProcess(...., pi, ...);
::EnumWindows(&EnumWindowsProc, pi.threadid);
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM param)
{
DWORD id = GetWindowThreadProcessId(hwnd, NULL)
if (id == (DWORD)param)
{
//do whatever we want to do
return false;
}
return true;
}
I found this code for on another thread(sorry can't find the link for it, but I found it by doing a search of enumwindows). The idea going here is that after you use createprocess, you make a call to enumwindows( by passing the address of the proper callback funciton, and the threadid variable of the PROCESS_INFORMATION stucture.
Then EnumWindowsProc checks the id of the HWND that was passed to it and checks to see if it matches the program you started with CreateProcess(since you passed the threadid). I don't thinked I typed everything out correctly(going off of memory), but that should give you an idea. I'll try to post the link I found on this site that showed the example.
templesfw
March 10th, 2003, 11:20 AM
I cannot see the window that belongs to MyApp.exe, actually i include on the EnumWindowsProc the GetWindowsText function to see what windows is getting the enumwindows.
ByThe way : When i execute my app from windows explorer I can see it on my Program that use the EnumWindows.....
Am i missing something on the CreateProcess ??
I call CreateProcess in this way :
CreateProcess( "MyApp.exe",
NULL, // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
NULL, // No creation flags.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi ) // Pointer to PROCESS_INFORMATION structure.
)
and this on my Enum Stuff :
bool CALLBACK EnumWindowsProc(HWND hwnd, LPARAM param)
{DWORD pID;
char szBuffer[MAX_PATH];
GetWindowText(hwnd,szBuffer,200);
DWORD TpID = GetWindowThreadProcessId(hwnd, &pID);
ShowMessage(szBuffer);
if (TpID == (DWORD)param)
{
Application->MessageBox(sMsg.c_str(),"Entro a ID Iguales",MB_OK);
FormMain->TargetHWND = hwnd;
return false;
}
return true;
}
james108
March 10th, 2003, 02:57 PM
You have probably already found a solution, but be sure and wait a bit before you start your enumwindows process - the process you start will take some time before the window is created. Try something like:
Sleep(1000);
to give the window time to appear...
thpsthc
March 10th, 2003, 04:30 PM
He's right, it may take some time so you may want to use the ::sleep function and ::waitforinputidle to make sure the window has been creating and is ready to respond to windows messages, functions...etc.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.