jhanson040697
asked on
Limiting an application to only one instance
I need to know how to prevent more than one instance of my application from being loaded at the same time. If a user attempts to load the application while an instance is already running, the currently running instance should be brought up instead. Can anyone tell me how to do this?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
there is a more simple solution using a named mutex (see CMutex or as kanni said an example at codeguru.com)
bye
feri
bye
feri
Ok, ok,
its not that complicated:
in your initialization module insert the following code:
// Check for existence of second Instance, and keep Mutex open
HANDLE hRunOnceMutex;
hRunOnceMutex = CreateMutex(NULL, FALSE, "EsKannNurEinenGeben");
if(GetLastError() == ERROR_ALREADY_EXISTS)
{
// some code to exit your program
}
you need not to call "CloseHandle(hRunOnceMutex )", because the mutex is freed automatically if your program exits.
bye,
feri
its not that complicated:
in your initialization module insert the following code:
// Check for existence of second Instance, and keep Mutex open
HANDLE hRunOnceMutex;
hRunOnceMutex = CreateMutex(NULL, FALSE, "EsKannNurEinenGeben");
if(GetLastError() == ERROR_ALREADY_EXISTS)
{
// some code to exit your program
}
you need not to call "CloseHandle(hRunOnceMutex
bye,
feri
fkurucz 's solution is common way in win32.
ASKER
The example on codeguru.com worked very well for me.
You can find solutions with sample code in the codeguru.com site.