Article View: borland.public.cppbuilder.nativeapi
Article #11753Re: SHExecInfo.hInstApp != GetWindowLong(hwnd,GWL_HINSTANCE);
From: Remy Lebeau (TeamB)
Date: Fri, 23 Jan 2004 19:17
Date: Fri, 23 Jan 2004 19:17
88 lines
3918 bytes
3918 bytes
"Rodrigo" <rmorteo@eudoramail.com> wrote in message
news:401157db$2@newsgroups.borland.com...
> 1.- When watching the values of SHELLEXECUTEINFO,
> the instance retrieved by GetWindowLong appears to be more
> similar to the hProcess value than the hInstApp value (see the
> GetWinHandle function below).
Like its brother ShellExecute(), the returned HINSTANCE is not always a true
handle. You would be better off working with process handles instead.
Since ShellExecuteEx() does not return a process ID, only a process handle,
your callback function will need to use GetWindowThreadProcessId() to get a
process ID and then use OpenProcess() to get a process handle that you can
compare with. Otherwise, change to use CreateProcess() instead so that you
can compare IDs instead of handles.
> 2.- I couldn't make the callback function belong to the TMainForm
> class because compiler showed a message telling that the function
> must be called or its address taken...
That is correct. The callback needs to be a standalone function. It cannot
be a member of a class at all, unless it is declared as 'static'. The
signature of a class method vs. a standalone function is very different.
> 4.-For what I understood from the documentation of both API functions,
> EnumWindows should loop until callback returns false
You should be using TRUE and FALSE, not 'true' and 'false'. BOOL is not a
'bool'.
> HWND TMainForm::LookForWindowHandle(HANDLE Hinstance)
> {
> unsigned long temp;
> temp=(long)Hinstance;
> if (!EnumWindows((WNDENUMPROC)GetWinHandle,temp))
> return swProcess; //global variable
> else
> return 0;
> }
You don't need to use a separate variable, you could pass the HInstance to
EnumWindows() directly:
HWND TMainForm::LookForWindowHandle(HANDLE Hinstance)
{
if( !EnumWindows((WNDENUMPROC)GetWinHandle, (LPARAM)Hinstance) )
return swProcess; //global variable
else
return 0;
}
I would also suggest that you not name your variable as "HInstance". The
VCL already has a global variable by that same name.
Also, I would suggest avvoiding the use of a global variable to return the
found window handle. You can use a structure instead in order to keep
everything local:
struct TWindowInfo
{
HINSTANCE hInst;
HWND hWnd;
};
HWND TMainForm::LookForWindowHandle(HANDLE Hinst)
{
TWindowInfo info;
info.hInst = Hinst;
info.hWnd = NULL;
EnumWindows((WNDENUMPROC)GetWinHandle, (LPARAM)&info);
return info.hWnd;
}
BOOL CALLBACK GetWinHandle(HWND hwnd, LPARAM lParam)
{
TWindowInfo *pInfo = (TWindowInfo*)lParam;
if( GetWindowLong(hwnd, GWL_HINSTANCE) == pInfo->hInst )
{
pInfo->hWnd = hwnd;
return FALSE;
}
else
return TRUE;
}
Gambit
Technical Details
Message-ID:
<401171bf@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!fu-berlin.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<401157db$2@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10cc67,b936e6c0846d144e X-Google-Attributes: gid10cc67,public X-Google-ArrivalTime: 2004-01-23 11:40:11 PST From: "Remy Lebeau \(TeamB\)" <gambit47.no.spam@no.spam.yahoo.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <401157db$2@newsgroups.borland.com> Subject: Re: SHExecInfo.hInstApp != GetWindowLong(hwnd,GWL_HINSTANCE); Date: Fri, 23 Jan 2004 11:17:34 -0800 Lines: 90 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: 67.22.138.253 Message-ID: <401171bf@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1074885055 67.22.138.253 (23 Jan 2004 11:10:55 -0700) Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!fu-berlin.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail