🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.nativeapi
1 messages
1 total messages Started by anon5 Mon, 03 Sep 2007 01:50
WM_COPYDATA problem
#1354
Author: anon5
Date: Mon, 03 Sep 2007 01:50
93 lines
3764 bytes
Hey guys

I am trying to create a 'once-only' app (only one instance can run).
When the user tries to run a second instance, it should pass its command
line to the first instance. I have been following the code at
http://bcbjournal.org/articles/vol3/9911/Single-instance_applications.htm
(and also
http://www.bcbjournal.org/articles/vol3/9911/Using_file_associations.htm)
but I can't get it to work. I'm using C++Builder 6.

Here is the workings of my code (slightly different from Kent's):


//---------------------------------------------------------------------------
// TFORMMAIN.CPP
//---------------------------------------------------------------------------
__fastcall TFormMain::TFormMain(TComponent* Owner)
     : TForm(Owner)
{
     WindowProc = MyWndProc;
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::MyWndProc(Messages::TMessage &Message)

{
     if (Message.Msg == WM_COPYDATA) {
         Caption = "WM_COPYDATA";
     }
     else {
         WndProc(Message);
     }
}
//---------------------------------------------------------------------------


//---------------------------------------------------------------------------
// MUTEXTEST.CPP
//---------------------------------------------------------------------------
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR cmdLine, int)
{
     try
     {
         CreateMutex(NULL, NULL, "the mutex of doom");

         if (GetLastError())
         {
             Application->Title = "Something Else";
             HWND hWnd = FindWindow("TApplication", "MutexTest");

             if (IsIconic(hWnd)) {
                 ShowWindow(hWnd, SW_SHOWDEFAULT);
             }
             SetForegroundWindow(hWnd);

             if (strlen(cmdLine) != 0)
             {
                 COPYDATASTRUCT cds;
                 cds.cbData = strlen(cmdLine) + 1;
                 cds.lpData = cmdLine;
                 SendMessage(hWnd, WM_COPYDATA, 0, (LPARAM)&cds);
             }
         }
         else
         {
             Application->Initialize();
             Application->Title = "MutexTest";
             Application->CreateForm(__classid(TFormMain), &FormMain);
             Application->Run();
         }
     }
     catch (Exception &exception)
     {
         Application->ShowException(&exception);
     }
     catch (...)
     {
         try
         {
             throw Exception("");
         }
         catch (Exception &exception)
         {
             Application->ShowException(&exception);
         }
     }
     return 0;
}
//---------------------------------------------------------------------------


Now, the once-only aspect of the app works great. But my WM_COPYDATA
message isn't being picked up at all. If anyone can help at all it would
be fantastic. Thanks!
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads