Re: Limit running instances of a program between console sessions

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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



.



Relevant Pages

  • Re: Mutex not working as expected
    ... Are you sure that the mutex isn't getting GC'd? ... I get the impression it is true on server side for server ... User session), The admin session. ... >> Sub Main ...
    (microsoft.public.dotnet.languages.vb)
  • Re: [Info-Ingres] Mutex: DCB iidbdb
    ... What does the logstat show for that session? ... Subject: Mutex: DCB iidbdb ... Enterprise Linux Server release 5. ...
    (comp.databases.ingres)
  • Re: [Info-Ingres] Mutex: DCB iidbdb
    ... Subject: [Info-Ingres] Mutex: DCB iidbdb ... What does the logstat show for that session? ...
    (comp.databases.ingres)
  • Re: Query Process on a terminal server
    ... restricts the Mutex object to be 'visible' in the current session only. ... |> // prevent multiple instances accross TS sessions by using a Global ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Need help with named mutex shared between asp.net and .net win app.
    ... Seems odd that this functionality was not included in the Mutex ... and use the Win32 calls directory in my managed C++ code. ... >> I know that in MFC you can set a security descriptor for the CMutex object>> so that it is created with a set security descriptor level. ...
    (microsoft.public.dotnet.framework)