🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #4255

Re: Continuing problem with SendMessage and WM_CCOPYDATA

#4255
From: Dennis Jones
Date: Mon, 27 Feb 2006 00:04
63 lines
3929 bytes
"Nate Lockwood" <RNLockwoodREMOVE@attglobal.net> wrote in message
news:44021981$1@newsgroups.borland.com...
> I've tried this a couple of ways and have now have the most basic code
> for testing.  I'm trying to use SendMessage from an initializing
> application when it discovers that there is another instance already
> running.  SendMessage returns 0 (I think that means success) but the
> function in my main form is never called.

First of all, the return value of SendMessages depends on the message type
that gets sent.  A return value of 0 may be success, or it may be failure --
it all depends on how the message is processed by the receiver.  SendMessage
works kind of like a function call...when the receiver finishes processing
the message, the value it returns becomes SendMessage's return value.

Now, forgive me if I've missed something, but if all you want to do is
prevent a second instance from running and instead just bring the first
instance to the foreground, you don't need to use SendMessage to do that.
The way I do it is use a mutex (like you're doing) and then tell the first
application to display itself using ShowWindow (no need to send a custom
message):

      // create an application-specific, named mutex
      hMutex = CreateMutex( NULL, FALSE, "MyApplicationMutex" );

      // if the mutex already exists, it means that another instance
      // is already running -- display it
      if ( GetLastError() == ERROR_ALREADY_EXISTS )
      {
         // The main "form" in a C++Builder/Delphi application is not the
         // application's main window (as far as Windows is concerned).
         // TApplication creates a hidden window that acts as the true main
         // window for the application.  Thus, whenever we want to refer to
the
         // application's main window, we must use the handle of the window
         // created by TApplication, not the handle of the main "form".  The
         // TApplication's window handle is the parent window handle for the
         // main form, so it is easy to find:

         // get the window handle of the other instance's main form
         HWND MainFormHWnd = FindWindow( "TMyApplicationMainForm", NULL );

         // get the main form's parent window handle (we'll assume
MainFormHWnd
         // is not NULL, since we wouldn't even be here unless another
instance
         // of the application were actually running)
         HWND hAppWnd = (HWND)GetWindowLong( MainFormHWnd, GWL_HWNDPARENT );

         if ( IsIconic( hAppWnd ) )
         {
            ShowWindow( hAppWnd, SW_RESTORE );
         }
         SetForegroundWindow( hAppWnd );
         return 0;
      }

Does this help at all?  Now, if you still want to send a message to the
other instance, *maybe* (I don't know for sure -- I've never done it myself)
you need to send the message to the application's main window as opposed to
the main form (see the comments in my code above).

- Dennis
Technical Details
Message-ID: <44024219$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.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
References: <44021981$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: "Dennis Jones" <nospam@nospam.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <44021981$1@newsgroups.borland.com>
Subject: Re: Continuing problem with SendMessage and WM_CCOPYDATA
Date: Sun, 26 Feb 2006 16:04:43 -0800
Lines: 65
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
NNTP-Posting-Host: 204.200.27.7
Message-ID: <44024219$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1140998681 204.200.27.7 (26 Feb 2006 16:04:41 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.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