I want to create a MFC program *.exe. My reguirement is at a time only
Try using mutexes (this method is described along WinAPI WinMain
function).
Add to yout CWinApp a HANDLE member variable. Lets name it like
this:
private:
HANDLE m_hOneInstanceMutex;
and initialize it to NULL in constructor.
In your CWinApp InitInstance place at the very beging a code like
this:
hOneInstanceMutex = CreateMutex(NULL, TRUE, UNIQE_NAME);
if(hOneInstanceMutex == NULL) {
if(GetLastError() == ERROR_ALREADY_EXISTS) {
// one instance is already running so display a message or
something like this and exit
return 0;
}
// there was an error, do what you want but it is not clear if this
is first instance or not
}
// if execution comes here then you are the first and only instance of
your application
Add the code at the begining because there is no point in
initialization of anything if you are the exiting because another
instance is already running.
You don't have to delete the mutex while exiting because system
will do it for you (as described in CreateMutex specification).
Choose UNIQE_NAME wisely because if you use a name already used by
antoher mutex (another application) you will end being not able to run
your application (not that a "hacker" can prevent your application
from runnig in this way - as described in WinMain specification).
Also I think question has more common with WinAPI and MFC than C++
so perhaps the choice of group you made was not the best one.
Adam Badura