🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Mutex not working

#7743
From: Alan Bellingham
Date: Thu, 23 Dec 2004 21:17
58 lines
2419 bytes
"Jarkko Halme" <jjhalme@kotiposti.net> wrote:

>I am trying to prevent several instances of my program running. I am using C++ Builder v5 and OS is XP. My main looks like below:
>
>-----------------------------------------
>WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
>{
>  HANDLE mutex;
>
>  try
>  {
>    const char mutexname[]="program";
>    mutex=OpenMutex(0,false,mutexname);
>
>    //If mutex does not exist = program is not running
>    if (mutex==NULL)
>    {
>      mutex=CreateMutex(NULL, true,mutexname);
>    }
>    //mutex exists = program is aleady running
>    else
>    {
>      ShowMessage("Program is already running");
>      return 0;
>    }
>
>    Application->Initialize();
>    Application->Title = "program";
>    Application->CreateForm(__classid(TForm1), &Form1);
>    Application->Run();
>  }
>  catch (Exception &exception)
>  {
>     Application->ShowException(&exception);
>  }
>  ReleaseMutex(mutex);
>  return 0;
>}
>--------------------------------------------------

I use the CreateMutex to attempt to create one. If it already exists,
then some other program has already created it.

This is the relevant part of the class - the d'tor merely cleans up:

  bool
  SingleInstanceMutex::Acquire()
  {
    SetLastError(0) ;
    m_Handle = CreateMutex(NULL, false, m_Name) ;
    return (GetLastError() != ERROR_ALREADY_EXISTS) ;
  }

I don't actually bother calling OpenMutex.

Alan Bellingham
-- 
ACCU Conference 2005 - 20-23 April, Randolph Hotel, Oxford, UK
Technical Details
Message-ID: <i6dms0dqr8046tcv1p0h06tq1dr1qjk5r4@4ax.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!gatel-ffm!gatel-ffm!194.25.134.126.MISMATCH!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <41cb32b8$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,5146b811ec878b88
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: Alan Bellingham <alanb@episys.com>
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Re: Mutex not working
Date: Thu, 23 Dec 2004 21:17:20 +0000
Reply-To: alanb@episys.com
Message-ID: <i6dms0dqr8046tcv1p0h06tq1dr1qjk5r4@4ax.com>
References: <41cb32b8$1@newsgroups.borland.com>
X-Newsreader: Forte Agent 2.0/32.652
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 193.129.251.130
X-Trace: newsgroups.borland.com 1103836637 193.129.251.130 (23 Dec 2004 13:17:17 -0800)
Lines: 58
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!gatel-ffm!gatel-ffm!194.25.134.126.MISMATCH!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail