Click to See Complete Forum and Search --> : How to get HWND of external window?
gecka
July 22nd, 2006, 02:45 PM
Hello.
I'd like to know how to execute an external program and get it's window handle.
I tried using ShellExecute() and WinExec() to launch the program, but I can only get the instance handle of the newly created window.
Please help.
Thank you.
Calculator
July 22nd, 2006, 03:43 PM
I would not really know a good way to do it with shellexecute or winexec, it would end up being kind of hackish. But, using shellexecuteex, you can retrieve the process handle, and then enumerate windows (find/getwindow or enumwindows) until you find it.
SHELLEXECUTEINFO eI = { 0 };
DWORD pIdExec, pTId;
HWND hWnd;
eI.cbSize = sizeof(eI);
eI.lpVerb = "open";
eI.lpFile = "iexplore";
eI.nShow = SW_SHOW;
eI.fMask = SEE_MASK_NOCLOSEPROCESS;
if(ShellExecuteEx(&eI) != FALSE)
{
WaitForSingleObject(eI.hProcess, 1000);
hWnd = FindWindow(NULL, 0);
while(hWnd != NULL)
{
GetWindowThreadProcessId(hWnd, &pTId);
pIdExec = GetProcessId(eI.hProcess);
if(pTId == pIdExec)
{
PostMessage(hWnd, WM_SYSCOMMAND, SC_CLOSE, 0);
}
hWnd = GetWindow(hWnd, GW_HWNDNEXT);
}
}
gecka
July 22nd, 2006, 04:20 PM
Thanks alot for your response. It works good for me.
Bornish
July 23rd, 2006, 02:58 AM
If fact, your request might not have a solution, since you can have a process (application) that has no windows! Same, you can have 1000 windows created every second, from about 10 threads belonging to that process, so, like Calculator said, you can only enumerate all windows and see which belong to the process of interest.
Regards,
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.