View Full Version : No more than 1 instance of the game.
jaggu
01-27-2004, 04:16 PM
Hi all,
How do I ensure that I dont let the user start more than one instance of my game in Windows? There should probably be an API function somewhere that lets me check that but I cant seem to find it.
One method I thought was to write to a file that the game is running and the subsequent instance would open the file and find the game is already running and quit. I suspect this is not The Right Thing. Any alternatives?
Thanks as always.
Lizardsoft
01-27-2004, 04:46 PM
This is actually a problem that isn't trivial at all, and has sparked a LOT of debate. Microsoft themselves have goofed on this many times in documentation. Here's a really really good article on doing it. I'm using the author's solution in CustomBar, and it has yet to fail:
http://www.codeproject.com/cpp/avoidmultinstance.asp
Everyone should read this article and take it to heart!
SorrowMan
01-27-2004, 09:12 PM
use CreateMutex that should do the trick.
Roulette
01-28-2004, 12:42 AM
Here's the basic code...
int WINAPI WinMain(HINSTANCE hinstance,HINSTANCE hprevinstance,LPSTR lpcmdline,int ncmdshow)
{
HANDLE hMutex=CreateMutex(NULL,TRUE,"Fancy Pants");
if (hMutex==NULL || GetLastError()==ERROR_ALREADY_EXISTS)
return 0;
// At this point, you can rest assured that another instance
// of this application is not already executing.
return 0;
}
princec
01-28-2004, 01:23 AM
A cross-platform trick could be to attempt to get exclusive write-access to a lock file.
Cas :)
Punchey
01-28-2004, 05:32 AM
I use a semaphore in similar fashion to the mutex technique described above.
tentons
01-28-2004, 11:16 AM
Originally posted by Roulette
Here's the basic code...
I popped this into my code and it did the trick. And seems a lot simpler than the article cited above. :) Thanks!!
Jake Stine
01-28-2004, 02:06 PM
For the record, the code snippet and the article do exactly the same thing in terms of preventing multiple instances from running. The article merely adds additional code to bring the pre-existing instance into the foreground (using EnumWindows and some Message handling). This is a useful feature because many people will try to re-run a program to get back to it, especially if they have a shortcut for it in their taskbar's QuickLaunch menu.
.. useful for things like Winamp or Windows Media Player, but probably isn't needed for games. :)
Actually, it IS important to make this process as smooth as possible if your potential customer is the impatient type. I have an excellent anecdote about this from sometime earlier this year(or perhaps it was last year). My roommate saw me playing the Mutant Storm demo and was quite curious about it. It wasn't long before he downloaded a copy himself and started playing. I observed, wanting to know how this potential customer, the most archetypical, generic "gamer" type you could ever hope to encounter(he really, quite seriously, has no life beyond his games and reading the Something Awful Forums - it's very sad), would respond.
But then when it came time for the "please register" banner, he missed the "skip" buttons(which I have to admit are quite hidden in that game), and so opened up a browser window. Grumbling, he closed it....and started a second instance of the game. When it crashed the system cold some time later, probably because of his running two instances, he wasted no time wiping it from the face of his hard drive. That was the end of that.
He certainly seems to enjoy Crimsonland, though :P
VaderSB
01-29-2004, 06:46 AM
I've already heard of mutexes and currently planning to implement this trick, but I have one question - do I need to call ReleaseMutex() at the end of program execution or it will be released automatically?
Roulette
01-29-2004, 09:30 AM
The mutex object will be destroyed when its last handle is closed. You can use CloseHandle() to close a retrieved handle explicitly. When your process terminates, the mutex handle is automatically released and, if no other handles are in existence for that mutex, the mutex will be destroyed.
So...if you are simply using this functionality to prevent multiple instances of your application from running, you don't need to worry about releasing the mutex.
- Roulette
VaderSB
01-29-2004, 10:25 AM
Thanks for the answer, Roulette!
jaggu
01-29-2004, 03:45 PM
Been listening and as always grateful for the answers.