C++ How to be sure my appl can only run 1 instance at a time ?
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++)
What operating system are you targeting? Standard C++ has no built-in features to do this.
Oh, and moved to General Programming.
Oh, and moved to General Programming.
Platform dependant but usually such functionality is achived through the use of kernel objects (named mutexes, pipes or in some cases simply a "lock"-file).
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 pointHANDLE 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 endsReleaseMutex(mutex);
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.
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.
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 ?
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 ?
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.
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.
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 !
Thanks a lot to help me out !
Just put:
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.
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement