Jump to content

Checking if your game has been started multiple times


Laurens
 Share

Recommended Posts

Thought I'd share this bit of code with you I found quite useful. It basically checks if your game has already been started to prevent players from going bananas on your executable. If your game has already been started, it brings it to the front.

 

const bool Application::IsOnlyInstance(const std::string title) const {
   HANDLE h = CreateMutex(0, true, title.c_str());

   if (GetLastError() != ERROR_SUCCESS) {
       HWND hWnd = FindWindow(title.c_str(), 0);

       if (hWnd != 0) {
           ShowWindow(hWnd, SW_SHOWNORMAL);
           SetFocus(hWnd);
           SetForegroundWindow(hWnd);
           SetActiveWindow(hWnd);
       }

       return false;
   }

   return true;
}

 

Title refers to the caption in the window's title bar so you do need to call this after having called SetAppTitle(...).

 

From GameCode: http://code.google.com/p/gamecode3/

 

Cheers!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...