Jump to content
  • Advertisement
Sign in to follow this  
Codorke

C++ How to be sure my appl can only run 1 instance at a time ?

This topic is 4840 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Like the subject : How can i be sure my application can only be started when no other instance off my application is already started ? (C++)

Share this post


Link to post
Share on other sites
Advertisement
What operating system are you targeting? Standard C++ has no built-in features to do this.

Oh, and moved to General Programming.

Share this post


Link to post
Share on other sites
At the very start of your application, try to create a mutex. If this succeeds, continue execution. If it fails, another instance must be running, so exit. Be sure to release the mutex when the application is finished. Some Win32 source (there should be a way to create a mutex with most operating systems):


//just after the program entry point
HANDLE mutex = CreateMutex(NULL, FALSE, "PreventMultipleAppsMutex");
if(!mutex || GetLastError() == ERROR_ALREADY_EXISTS)
return -1;//exit the program

//insert the rest of your code here
//...

//just before the program ends
ReleaseMutex(mutex);

Share this post


Link to post
Share on other sites
i think the "file lock" feature should work on all platforms.
if you open a file for writing, and leave it opened while your app is running, another "open for write"-try should fail...
and if it does, the app is running already. but this is somewhat lame, since the app would seem "damaged" if someone write-protected the file, for example.

Share this post


Link to post
Share on other sites
The application is focused to run under windows XP.
I'm already using different functions who are only supported by windows XP so windows is the targetted platform.

I'm using the NeHe's standerd code to run my openGL window. So i'll need too create the mutex in the InitGL or better in the CreateGLWindow function ?

Share this post


Link to post
Share on other sites
As CodeMunkie said, you should try to create the mutex at the start of your program, so that you can determine right away whether or not another instance is already running. (creating a mutex has nothing to do with the OpenGL API or your game engine; it's a mechanism that your application can use to check for previous instances.

If you need to execute some code (for whatever reason) before checking for another instance, then do that. But remember, if you already have an instance running, it would be best to exit as soon as possible. Initialising subsystems before exiting due to an existing mutex is unnecessary (and may negate the value of the mutex).

As well, destroy the mutex at the very end of your program, so that you can be sure that any code that needs it can have the gaurantee it's the only instance.

Share this post


Link to post
Share on other sites
The first thing my program is doing now is creating the mutex. And like you all said the last thing is releasing it. I know how to create a mutex but didn't knew wherefor they where used.

Thanks a lot to help me out !

Share this post


Link to post
Share on other sites
Just put:

HANDLE hMtx = CreateMutex( 0, TRUE, "ThereCanBeOnlyOne");
if( GetLastError() == ERROR_ALREADY_EXISTS)
return -1;

at the top of your (win)main function.

You don't need to close the handle or release the mutex (the system does that automagiclly when the process exits).

This will probably not protect you from multiple users on the same system running the app simultaneously but I would consider that wanted behaviour in this particular case.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by DigitalDelusion
Just put:

HANDLE hMtx = CreateMutex( 0, TRUE, "ThereCanBeOnlyOne");


This will probably not protect you from multiple users on the same system running the app simultaneously but I would consider that wanted behaviour in this particular case.


Actually, this will create a global mutex, so no other user session would be able to launch the app. If you use "Local\\ThereCanBeOnlyOne" then the mutex is scoped to the current user session only.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!