Link to home
Start Free TrialLog in
Avatar of jhanson040697
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?
Avatar of kanni
kanni

Hi

You can find solutions with sample code in the codeguru.com site.
ASKER CERTIFIED SOLUTION
Avatar of kanni
kanni

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
there is a more simple solution using a named mutex (see CMutex or as kanni said an example at codeguru.com)

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

fkurucz 's solution is common way in win32.
Avatar of jhanson040697

ASKER

The example on codeguru.com worked very well for me.