Article View: borland.public.cppbuilder.nativeapi
Article #10930Finding a Spawned Window
From: Mortz
Date: Fri, 05 Mar 2004 18:30
Date: Fri, 05 Mar 2004 18:30
72 lines
3687 bytes
3687 bytes
Hi, does anyone know how to get a window handle (HWND) after being spawned
with "ShellExecuteEx".
I've tried several ways but none seems to work; the lastone was trying to
get the HWND trought the "ShExecInfo.hProcess" enumerating windows but seems
like I'm missing something (See code below).
May be If I can get the Window Text form the spawned windows trought one
member of the ShExecInfo retrieved by the call to ShellExecuteEx I could use
"FindWindow" to get the actual window handle.
Am I going in the right direction?
Any help would be appreciated...
Regards...
MortZ.
Here is the code:
Button1Click(TObject *Sender){
//...
// Variable declarations
//...
//spawn application to open the document
shExecInfo.cbSize=sizeof(SHELLEXECUTEINFO);
memset(&shExecInfo,0,sizeof(shExecInfo));
shExecInfo.cbSize=sizeof(shExecInfo);
shExecInfo.hwnd=Handle;
shExecInfo.lpVerb="open";
shExecInfo.lpFile=dataPtr->SystemString().c_str(); //dataPtr
is a pointer tu a structure (NodeInfo) that holds some info about the
spawned app, see the if(b_res) section.
shExecInfo.nShow=SW_NORMAL;
shExecInfo.fMask=SEE_MASK_NOCLOSEPROCESS;
b_res=ShellExecuteEx(&shExecInfo);
if(b_res)
{
dataPtr->HProcess(shExecInfo.hProcess);
dataPtr->HInstance(shExecInfo.hInstApp);
dataPtr->WindowHandle(LookForWindowHandle(dataPtr));
//this function returns nothing 'cause window enumeration finishes without
getting the handle
dataPtr->Active(true);
}
}
//--------------------------------------------------------------------------
-
HWND TMainForm::LookForWindowHandle(NodeInfo *node)
{
if (!EnumWindows((WNDENUMPROC)GetWinHandle,(LPARAM)node))
return node->WindowHandle();
else
return 0;
}
//--------------------------------------------------------------------------
-
BOOL CALLBACK GetWinHandle(HWND hwnd,LPARAM lParam)
{
NodeInfo* pInfo=(NodeInfo *)lParam;
DWORD dwProcessId=0;
GetWindowThreadProcessId(hwnd,&dwProcessId);
HANDLE hProcess=OpenProcess(0,FALSE,dwProcessId);
if (hProcess)
{
if(hProcess==pInfo->HProcess())
pInfo->WindowHandle(hwnd);
CloseHandle(hProcess);
}
return (pInfo->WindowHandle()==NULL);
}
Technical Details
Message-ID:
<4048c64f$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!fu-berlin.de!border1.nntp.ash.giganews.com!border2.nntp.ash.giganews.com!nntp.giganews.com!elnk-atl-nf1!newsfeed.earthlink.net!bigfeed2.bellsouth.net!bigfeed.bellsouth.net!news.bellsouth.net!peer01.cox.net!cox.net!newsfeeds.ihug.co.nz!ihug.co.nz!ken-transit.news.telstra.net!lon-in.news.telstra.net!news.telstra.net!202.12.144.25.MISMATCH!mws-stat-syd.cdn.telstra.com.au!newsx!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10cc67,6b6fbcbb8d2db5cb,start X-Google-Attributes: gid10cc67,public X-Google-ArrivalTime: 2004-03-05 10:40:15 PST Reply-To: "Mortz" <rmorteo@hotmail.com> From: "Mortz" <rmorteo@hotmail.com> Newsgroups: borland.public.cppbuilder.nativeapi Subject: Finding a Spawned Window Date: Fri, 5 Mar 2004 10:30:24 -0800 Lines: 74 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 NNTP-Posting-Host: 200.38.16.98 Message-ID: <4048c64f$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1078511183 200.38.16.98 (5 Mar 2004 10:26:23 -0800) Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!fu-berlin.de!border1.nntp.ash.giganews.com!border2.nntp.ash.giganews.com!nntp.giganews.com!elnk-atl-nf1!newsfeed.earthlink.net!bigfeed2.bellsouth.net!bigfeed.bellsouth.net!news.bellsouth.net!peer01.cox.net!cox.net!newsfeeds.ihug.co.nz!ihug.co.nz!ken-transit.news.telstra.net!lon-in.news.telstra.net!news.telstra.net!202.12.144.25.MISMATCH!mws-stat-syd.cdn.telstra.com.au!newsx!newsgroups.borland.com!not-for-mail