Re: Limit running instances of a program between console sessions
- From: dc2000 <dc2000@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Sun, 12 Nov 2006 13:44:01 -0800
Thank you for your input guys. This is not that simple though. The mutex
should be declared as Global, but that is not the end. The access to it will
be denied from any other terminal session, thus we need to initialize
security descriptor for it.
Here's how I was able to do it:
When the app initializes:
//Declare security attributes
SECURITY_ATTRIBUTES sa = {0};
sa.nLength = sizeof(sa);
sa.bInheritHandle = FALSE;
//Initialize security descriptor
BOOL bGotSA = FALSE;
SECURITY_DESCRIPTOR sd;
if(InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION))
{
//Set security descriptor (Null DACL)
if(SetSecurityDescriptorDacl(&sd, TRUE, (PACL)NULL, FALSE))
{
bGotSA = TRUE;
sa.lpSecurityDescriptor = &sd;
}
}
//Create named mutex
BOOL bSecondInstance;
HANDLE hMutex;
LPCTSTR pMutexName = "Global\\Logon_Session_Mutex_1";
int nErr;
if(bGotSA)
{
//Got security descriptor
hMutex = CreateMutex(&sa, FALSE, pMutexName);
nErr = ::GetLastError();
bSecondInstance = hMutex && nErr == ERROR_ALREADY_EXISTS;
}
else
{
//No security descriptor
hMutex = CreateMutex(NULL, FALSE, pMutexName);
nErr = ::GetLastError();
bSecondInstance = (hMutex && nErr == ERROR_ALREADY_EXISTS) || nErr ==
ERROR_ACCESS_DENIED;
}
if(bSecondInstance)
{
//More than one instance of this app is running
//Show message and exit
}
And then release the mutex when the app exits:
//Close mutex
if(hMutex)
CloseHandle(hMutex);
"William DePalo [MVP VC++]" wrote:
"dc2000" <dc2000@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message.
news:5C7D5A1B-6330-4C21-B72C-711919584E59@xxxxxxxxxxxxxxxx
It is pretty simple to limit number of running instances of a program to
one
in a single user logon (i.e. in the same console session) but what if I
need
to do the same between several users that might be logged on -- in other
words how to stop another instance of the program to be run from another
logon session on Windows XP?
I see others have pointed out that one option is to use a named object. If
you intend to limit the number of instances across all terminal server
sessions, you should name the object appropriately:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/termserv/termserv/kernel_object_namespaces.asp
Regards,
Will
- References:
- Re: Limit running instances of a program between console sessions
- From: William DePalo [MVP VC++]
- Re: Limit running instances of a program between console sessions
- Prev by Date: Re: Limit running instances of a program between console sessions
- Next by Date: Re: Temporary folders
- Previous by thread: Re: Limit running instances of a program between console sessions
- Next by thread: Precision issue of CreateTimerQueueTimer()
- Index(es):
Relevant Pages
|