Skip to main content
GameDev.net gamedev.net
🔒 Locked

Only one application instance allowed?

Started by Impz0r Aug 17, 2006 at 5:45 AM 1 replies 500+ views
Original Post
Impz0r
Impz0r
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
Stay Evil & Ugly!
DaBono
DaBono
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.)
Impz0r
Impz0r
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
Stay Evil & Ugly!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.