Article View: borland.public.cppbuilder.nativeapi
Article #4283Re: Continuing problem with SendMessage and WM_CCOPYDATA
From: Remy Lebeau (TeamB)
Date: Tue, 28 Feb 2006 19:17
Date: Tue, 28 Feb 2006 19:17
94 lines
4592 bytes
4592 bytes
"JD" <nospam@nospam.com> wrote in message
news:44046dac$1@newsgroups.borland.com...
> > HWND hAppWnd = (HWND)GetWindowLong( MainFormHWnd,
GWL_HWNDPARENT );
>
> With this line, I was able to solve my problem with knowing
> the state and reduce the component code and complexity:
The HWND parent of a TForm is the TApplication::Handle (unless the form
overrides its CreateParams() method to specify something else)
The application can subclass its TApplication window by using the
TApplication::HookMainWindow() method.
> ::GetClassName( FParentForm->Handle, ThisClassName, 255 );
> hMutex = ::CreateMutex( NULL, TRUE, ThisClassName );
That will only be reliable if the TForm class name is unique across all
applications. A better approach would be to append some kind of
process-specific data to the mutex name to ensure its uniqueness, such as
the process ID. Then the TForm class name becomes irrelevant and is not
needed at all:
char MutexName[32] = {0};
::wsprintf(MutexName, "TSingleInstance.%0.10u", GetCurrentProcessId());
hMutex = ::CreateMutex(NULL, TRUE, MutexName);
> bool __stdcall TSingleInstance::EnumWindowsCallBack( HWND hWnd, LPARAM
lParam )
> {
> char WindowClassName[256] = { 0 };
> if( ::GetClassName(hWnd, WindowClassName, 255) )
> {
> if( stricmp(WindowClassName, (char*)lParam) == 0 )
> {
> if( ThisSingleInstance->hFirstInstance )
> {
> ThisSingleInstance->hSecondInstance = hWnd;
> return FALSE;
> }
> else ThisSingleInstance->hFirstInstance = hWnd;
> }
> }
> return TRUE;
> }
That is not a very accurate way to find previous instances. The order in
which the enumeration can return windows is not predictable ahead of time.
The new application instance could be found before the previous instance, in
which case your HWND variables will be swapped incorrectly - hFirstInstace
will point to the second instance, and hSecondInstace will point to the
first instace. I do notice that you are swapping the variables after
EnumWindows() returns, but a better approach would be to not track two
variables at all. You can check the HWND provided by the callback to see if
it matches the TForm::Handle before assuming whether it is the first or
second instance. Then you only have to to use one variable instead of two:
private:
HWND hPreviousInstance;
bool __stdcall TSingleInstance::EnumWindowsCallBack( HWND hWnd, LPARAM
lParam )
{
char WindowClassName[256] = { 0 };
if( ::GetClassName(hWnd, WindowClassName, 255) )
{
if( stricmp(WindowClassName, reinterpret_cast<char*>(lParam)) ==
0 )
{
if( hWnd != ThisSingleInstance->FParentForm->Handle )
{
ThisSingleInstance->hPreviousInstance = hWnd;
return FALSE;
}
}
}
return TRUE;
}
> Alot of people looked at this, including Gambit and at least
> 2 other TeamB members commented and even though we all
> knew that TApplication has a hidden window, none of us thought of
> it being important.
When did I ever tell you that the TApplication window was not important? If
you hook the TApplication window, it could greatly simplify your code, since
you won't have to hook into any particular TForm objects anymore.
Gambit
Technical Details
Message-ID:
<4404a191$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<44021981$1@newsgroups.borland.com> <44024219$1@newsgroups.borland.com> <44046dac$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,6afbc9bf5041d787 X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <44021981$1@newsgroups.borland.com> <44024219$1@newsgroups.borland.com> <44046dac$1@newsgroups.borland.com> Subject: Re: Continuing problem with SendMessage and WM_CCOPYDATA Date: Tue, 28 Feb 2006 11:17:27 -0800 Lines: 96 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1409 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409 NNTP-Posting-Host: 68.69.17.41 Message-ID: <4404a191$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1141154193 68.69.17.41 (28 Feb 2006 11:16:33 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail