Article View: borland.public.cppbuilder.nativeapi
Article #4374Mutex troubles?
From: Nate Lockwood
Date: Fri, 24 Feb 2006 20:54
Date: Fri, 24 Feb 2006 20:54
62 lines
2884 bytes
2884 bytes
I'm trying to have my program detect that another instance is running,
send a "Show" message to the already running instance, and then return.
I'm using Builder 6.
It appears that I always get a non null mutex so the message is never
sent. Here's some code.
Thanks
UINT WM_MYMSG;
HANDLE CheckForInstance(const UINT WM_MYMSG);
<snip>
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
HANDLE mutex;
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)) == NULL)
return 0 ; // second instance
Application->Title = sTitle;
Application->Initialize();
Application->CreateForm(__classid(TFormMain), &FormMain);
Application->CreateForm(__classid(TAboutBox), &AboutBox);
Application->Run();
}
</snip>
//---------------------------------------------------------------------------
HANDLE CheckForInstance(const UINT WM_MYMSG)
{
SetLastError(0) ;
char * m_Name = "USDA.FS.FireScience.GooleEarthTool";
HANDLE m_Handle = CreateMutex(NULL, false, m_Name);
if(m_Handle != NULL)
return m_Handle; // this is the first instance
if(GetLastError() == ERROR_ALREADY_EXISTS)
PostMessage(HWND_BROADCAST, WM_MYMSG, NULL, NULL);
return NULL; // NULL handle
}
<snip>
if (mutex != NULL)
ReleaseMutex(mutex);
</snip>
-- In TFormMain
//----------------------------------------------------------------------------
void __fastcall TFormMain::WndProc(TMessage& message)
{
if(message.Msg == WM_MYMSG)
FormMain->Show();
else
TForm::WndProc(message); //pass on to parent
}
Technical Details
Message-ID:
<43ff726e$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!news.tele.dk!feed118.news.tele.dk!news.tele.dk!small.news.tele.dk!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,7b6bdf1fac3d7d96 X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit Date: Fri, 24 Feb 2006 12:54:06 -0800 From: Nate Lockwood <RNLockwoodREMOVE@attglobal.net> User-Agent: Thunderbird 1.5 (Windows/20051201) MIME-Version: 1.0 Newsgroups: borland.public.cppbuilder.nativeapi Subject: Mutex troubles? Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 69.234.59.217 Message-ID: <43ff726e$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1140814446 69.234.59.217 (24 Feb 2006 12:54:06 -0700) Lines: 63 Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!news.tele.dk!feed118.news.tele.dk!news.tele.dk!small.news.tele.dk!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail