🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Stopping multiple instances of an application

#12017
From: Alan Bellingham
Date: Tue, 06 Jan 2004 16:57
37 lines
2109 bytes
"Andrew" <alamb@tynetec.co.uk> wrote:

>I am trying to stop more than one instance of my app running at any one
>time, normally I use a mutex and this works fine, most of the applications
>we have developed have used Win 9x but now we have moved to win 2k and the
>mutex doesn't seem to work any more, I have also tried semaphores and the
>API function FindWindow, please see the following
>
>Using a mutex:
>
>        HANDLE mutex;
>        String mutexname = "Concierge.Server.mutex";
>        mutex = OpenMutex(NULL,false,mutexname.c_str());
>
>        if (mutex == NULL)
>            mutex = CreateMutex(NULL,true,mutexname.c_str());
>        else
>        {
>            MessageBox(0,"This application is already running.\n"
>                  "You may only run one instance at any one
>time","Error",MB_OK);
>            return 0;
>        }

Hmmm. That's not how my code does it. Basically, I go straight into
CreateMutex() and on return check GetLastError() to see if it's
ERROR_ALREADY_EXISTS.

In other words:

	SetLastError(0) ;
	m_Handle = CreateMutex(NULL, false, m_Name) ;
	return (GetLastError() != ERROR_ALREADY_EXISTS) ;

Alan Bellingham
-- 
alanb@episys.com
Technical Details
Message-ID: <u0qlvvc1n1epgh3cf8plpup49s7ug7fpeh@4ax.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!fu-berlin.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <3ffae3bc@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,557b0e7a5128b725
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2004-01-06 09:10:04 PST
From: Alan Bellingham <alanb@episys.com>
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Re: Stopping multiple instances of an application
Date: Tue, 06 Jan 2004 16:57:38 +0000
Reply-To: alanb@episys.com
Message-ID: <u0qlvvc1n1epgh3cf8plpup49s7ug7fpeh@4ax.com>
References: <3ffae3bc@newsgroups.borland.com>
X-Newsreader: Forte Agent 1.92/32.572
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 1073408257 193.129.251.130 (6 Jan 2004 08:57:37 -0700)
Lines: 37
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!fu-berlin.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail