Borland C ++ Builder FAQ
The content The last update: 12/12/2008

How not to admit the second start of the program?

The author: OlegGG
The initial link: - - -

Variant #1 - Muteksy:

In the program beginning (ИМХО, is better to "Application-> Initialize ();") we write:
hMutex = CreateMutex (NULL, true, "some_big_big_unique_mutex_name"); 
if (GetLastError () == ERROR_ALREADY_EXISTS) 
        { 
        //we already are - we swear and we quit 
        ShowMessage ("proga already loaded"); 
        Application-> Terminate (); 
        }
In the end we write:
ReleaseMutex (hMutex);//it is mandatory, and that before reboot a program the second time will not be launched 
Variant #2 - Atoms:

Basically, similar previous the variant.
In the program beginning (ИМХО, is better to "Application-> Initialize ();") we write:
AnsiString AtomName = "some_big_big_unique_atom_name"; 
ATOM Atom; 

if (GlobalFindAtom (AtomName.c_str() == 0) 
        { 
        //we create atom 
        Atom = GlobalAddAtom (AtomName); 
        }
else 
        { 
        //we already are - we swear and we quit 
        ShowMessage ("proga already loaded"); 
        Application-> Terminate (); 
        }
In the end we write:
GlobalDeleteAtom (Atom);//it is mandatory, ато before reboot a program the second time will not be launched 
Variant #3 - FindWindow:

For this method it is necessary, that the primary window of your program had unique class name or title.
For example, class name of the principal form - "Tformochka".
Then in the program beginning (ИМХО, is better to "Application-> Initialize ();") we write:

HWND hWnd = FindWindow ("Tformochka", NULL);//we search for the window 
if (hWnd! =NULL)//if found 
        { 
        ShowMessage ("proga already loaded");//we swear 
        SetForegroundWindow (hWnd);//it is activated the previous copy of a program 
        Application-> Terminate ();//we are closed 
        }