Sign in to follow this  
Impz0r

Only one application instance allowed?

Recommended Posts

Impz0r    166
Heya, as the topic says, i'm trying to restrict my application to only run once. Meaning i only want one instance running at a time. To achieve this, i'm using a Mutex as the following:
HANDLE hMutex = ::CreateMutex (NULL, True, "Global\\MyVeryOwnInstanceName");
if (hMutex)
 {
  if (::GetLastError () == ERROR_ALREADY_EXISTS)
   {
    ::CloseHandle (hMutex);
    throw "Only one instance allowed.";
   }
  else
   ::CloseHandle (hMutex);
 }

But this wont work for me, GetLastError () never returns ERROR_ALREADY_EXISTS regardless how many instances allready running. Any ideas ? Impz0r

Share this post


Link to post
Share on other sites
DaBono    1496
By closing the handle, you release the mutex immediately after checking if there is another instance running. However, you need to hold on to that handle during the entire course of the program.
(Also, I think you need to lose the 'if(hMutex)'-check.)

Share this post


Link to post
Share on other sites
Impz0r    166
I'm with stupid!

You're absolutely right DaBono, i need to hold the Mutex handle and not to destroy it right after i created it ...

Thanks DaBono!


Impz0r

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this