🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Continuing problem with SendMessage and WM_CCOPYDATA

#4254
From: Nate Lockwood
Date: Sun, 26 Feb 2006 21:11
108 lines
4794 bytes
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.

Nate
Here is some code

UINT WM_MYMSG;   Global
HANDLE CheckForInstance(const UINT WM_MYMSG, const String sTitle, 
TStringList *);

WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
         HANDLE mutex;
         TStringList * FileList = NULL;
         try
         {
                  String sTitle = "Google Earth (C) Tool";
                  String sMessage = 
"USDA.FS.FireScience.GooleEarthTool.Awake";
                  Application->Title = sTitle + " Temporary";
                  WM_MYMSG = RegisterWindowMessage(sMessage.c_str());
                  if((mutex = CheckForInstance(WM_MYMSG, sTitle, 
FileList)) == NULL)
                     return 0 ;    //  second instance
                  Application->Title = sTitle;
                  Application->Initialize();
                  Application->CreateForm(__classid(TFormMain), &FormMain);
                  Application->CreateForm(__classid(TAboutBox), &AboutBox);
                  Application->Run();
         }
         catch (Exception &exception)
         {
                  Application->ShowException(&exception);
         }
         catch (...)
         {
                  try
                  {
                          throw Exception("");
                  }
                  catch (Exception &exception)
                  {
                          Application->ShowException(&exception);
                  }
         }
        if (mutex != NULL)
            ::ReleaseMutex(mutex);
        return 0;
//---------------------------------------------------------------------------
HANDLE CheckForInstance(const UINT WM_MYMSG,    // unique
                         const String sTitle,    // of running ap
                         TStringList * FileList)
{
   SetLastError(0) ;
   char * m_Name = "USDA.FS.FireScience.GooleEarthTool.mutex";
   HANDLE m_Handle = ::CreateMutex(NULL, true, m_Name);

   if(m_Handle == NULL)      // Didn't exist and couldn't create
     return m_Handle;
                           // got a mutex -
   if(::GetLastError() != ERROR_ALREADY_EXISTS) //  already set?
     return m_Handle;                           // no, first instance

                    // code for the second instance
   HWND hReceiver  = ::FindWindow("TApplication", sTitle.c_str());
   if( hReceiver  )                // send a message to other
     {
     WPARAM wparam;
     char * pBuf = "Second instance";

     COPYDATASTRUCT MyCDS;
     MyCDS.dwData = WM_MYMSG;      // unique identifier
     MyCDS.cbData = strlen(pBuf) + 1;    // buffer size 1 -> no data
     MyCDS.lpData = &pBuf;         // address of the data
     HANDLE hThisAp = Application->Handle;
     LRESULT result = ::SendMessage(hReceiver , WM_COPYDATA,
                                    (WPARAM)hThisAp, (LPARAM)&MyCDS);
     int SyntheticBreak;
     SyntheticBreak = 0;
                      //::PostMessage(hWnd, WM_MYMSG, wparam, NULL);
         // cleanup
     }
   return NULL;     //  NULL handle
}

//  in my main form .h

public:         // public user declarations
   void __fastcall WMCopyData(TWMCopyData &message);
BEGIN_MESSAGE_MAP
     MESSAGE_HANDLER(WM_COPYDATA, TWMCopyData, WMCopyData)
END_MESSAGE_MAP(TForm)

// in my main form .cpp
extern const UINT WM_MYMSG;

     // This function is never called...
void __fastcall TFormMain::WMCopyData(TWMCopyData &message)
{
   if(message.CopyDataStruct->dwData == WM_MYMSG)
     {
     Application->Restore();
     Show();
     }
}
Technical Details
Message-ID: <44021981$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!news-out1.kabelfoon.nl!newsfeed.kabelfoon.nl!bandi.nntp.kabelfoon.nl!newsfeed.freenet.de!newsfeed.inode.at!reader.uucp.at!newsfeed.uucp.at!news.uucp.at!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,6afbc9bf5041d787,start
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
Date: Sun, 26 Feb 2006 13:11:36 -0800
From: Nate Lockwood <RNLockwoodREMOVE@attglobal.net>
User-Agent: Thunderbird 1.5 (Windows/20051201)
MIME-Version: 1.0
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Continuing problem with SendMessage and WM_CCOPYDATA
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 69.234.59.217
Message-ID: <44021981$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1140988289 69.234.59.217 (26 Feb 2006 13:11:29 -0700)
Lines: 108
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!news-out1.kabelfoon.nl!newsfeed.kabelfoon.nl!bandi.nntp.kabelfoon.nl!newsfeed.freenet.de!newsfeed.inode.at!reader.uucp.at!newsfeed.uucp.at!news.uucp.at!newsgroups.borland.com!not-for-mail