🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.os.ms-windows.programmer.misc
Article #2935

Re: How to avoid multiple instances of a program in NT and 95?

#2935
From: Pete MacLiesh
Date: Thu, 06 Feb 1997 00:00
56 lines
2623 bytes

The following appears to work - it also restricts to a single instance
system-wide
on machines running citrix derived systems like WinDD.

Return is true if it succeeded in creating the mutex, FALSE if failed
because it's
already running

PMac

--

BOOL	CheckandCreateMutex(const char * mutexName)
{
	BOOL	ret = FALSE;

	// The nastiness here is because of multi-user NT derivatives ala Citrix.
	// To make a global resource on these, \SYSTEM must be appended -- but
this
	// fails on vanilla NT, so I try both (\SYSTEM first) ... because I cannot
	// determine portably if the system is multi-user !!!
	HANDLE hMutex1 = OpenMutex(MUTEX_ALL_ACCESS, FALSE, mutexName);
	HANDLE hMutex2 = OpenMutex(MUTEX_ALL_ACCESS, FALSE, CString(mutexName) +
"\\SYSTEM");
	if (hMutex1 == NULL && hMutex2 == NULL)
	{
		HANDLE hMutex = CreateMutex(0, TRUE, CString(mutexName) + "\\SYSTEM");
		if (hMutex == NULL)
			hMutex = CreateMutex(0, TRUE, mutexName);

		DWORD err = GetLastError();
		ret = !(hMutex == NULL || err == ERROR_ALREADY_EXISTS);
		ASSERT(ret);
	}
	else
	{
		if (hMutex1)
			CloseHandle(hMutex1);
		if (hMutex2)
			CloseHandle(hMutex2);
	}

	return ret;
}


-- 
________________________________________________________________
All Opinions are my own ... not necessarily those of my employer
________________________________________________________________
Peter MacLiesh                *      Email:  pmac@synopsys.com
Synopsys Inc.                 *      Phone:  (415)694-4423
700A E. Middlefield Rd.       *      Fax:    (415)694-1626
Mountain View, CA 94043       *
Technical Details
Message-ID: <01bc13cd$971217a0$5754e192@zaphod95.synopsys.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <5d4eeb$n6u$1@euler.space.net> <01bc11ec$dfe21120$b8bfabc7@pongo> <3Kq8KAA+ax9yEwPr@ormesci.demon.co.uk> <Pine.SGI.3.95.970204134313.16218D-100000@sbustd.stud.uni-sb.de>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 1124cf,3403ef0501dd273
X-Google-Attributes: gid1124cf,public
X-Google-Thread: f6ef4,3403ef0501dd273
X-Google-Attributes: gidf6ef4,public
X-Google-Thread: 113dfb,3403ef0501dd273
X-Google-Attributes: gid113dfb,public
X-Google-Thread: 105df1,3403ef0501dd273
X-Google-Attributes: gid105df1,public
From: "Pete MacLiesh" <pmac@synopsys.com>
Subject: Re: How to avoid multiple instances of a program in NT and 95?
Date: 1997/02/06
Message-ID: <01bc13cd$971217a0$5754e192@zaphod95.synopsys.com>
X-Deja-AN: 214741525
references: <5d4eeb$n6u$1@euler.space.net> <01bc11ec$dfe21120$b8bfabc7@pongo> <3Kq8KAA+ax9yEwPr@ormesci.demon.co.uk> <Pine.SGI.3.95.970204134313.16218D-100000@sbustd.stud.uni-sb.de>
content-type: text/plain; charset=ISO-8859-1
organization: Synopsys, Inc.
mime-version: 1.0
newsgroups: comp.os.ms-windows.programmer.misc,comp.os.ms-windows.programmer.win32,de.comp.os.ms-windows.programmer,fido.ger.windows.prog