Article View: borland.public.cppbuilder.nativeapi
Article #1353WM_COPYDATA problem
From: anon5
Date: Mon, 03 Sep 2007 01:37
Date: Mon, 03 Sep 2007 01:37
93 lines
3775 bytes
3775 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 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!
Technical Details
Message-ID:
<46db6544$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!feeder1-2.proxad.net!proxad.net!feeder1-1.proxad.net!club-internet.fr!feedme-small.clubint.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,ae849346afde0161,start X-Google-Attributes: gid10cc67,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Date: Mon, 03 Sep 2007 02:37:13 +0100 From: anon5 <bogus@mail.com> User-Agent: Thunderbird 2.0.0.6 (Windows/20070728) MIME-Version: 1.0 Newsgroups: borland.public.cppbuilder.nativeapi Subject: WM_COPYDATA problem Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 82.32.100.249 Message-ID: <46db6544$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1188783428 82.32.100.249 (2 Sep 2007 18:37:08 -0700) Lines: 93 Path: arc1-ca.pugleaf.net!g2news2.google.com!news4.google.com!feeder1-2.proxad.net!proxad.net!feeder1-1.proxad.net!club-internet.fr!feedme-small.clubint.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail