Method CreateMutex() to prevent program from starting twice

       In the project file, add the following code to the WinMain function (this code runs under BCB6.0):
         HANDLE hMutex = CreateMutex(NULL, false, “Process”);
         if (GetLastError() == ERROR_ALREADY_EXISTS)
         {
            CloseHandle(hMutex) ;
            MessageBox(Application->Handle, "The program is already running and cannot be restarted!", "Prompt", MB_OK +MB_ICONWARNING);
            Application->Terminate();
            return 0;
         }
         Application->CreateForm(__classid(TForm1) , &Form1);

        Mainly use the CreateMutex() function and GetLastError() and a constant ERROR_ALREADY_EXISTS.

        Of course, if your program has a form, you can also use FindWindow().
        void *handle = FindWindow(NULL, WindowName.c_str());
        if ( handle!=NULL)
            return 0;


here is an excerpt: http://topic.csdn.net/t/20051121/15/4407868.html

Mutually exclusive running of processes

  Under normal circumstances, the running of one process generally does not affect other running processes. However, for some programs with special requirements, such as exclusive use of serial ports and other hardware devices, other programs that try to use this port device are not allowed to run during the running of their processes, and such programs are usually not allowed to run. Multiple instances of the same program. This leads to the problem of process mutual exclusion.

  The core idea of ​​implementing process mutual exclusion is relatively simple: when a process starts, it first checks whether an instance of the process already exists in the current system. If not, the process will successfully create and set a flag that identifies the existing instance. After that, when a process is created, it will know that its instance already exists through this mark, so as to ensure that only one instance of the process exists in the system. Specifically, it can be implemented by various methods such as memory-mapped files, named events, named mutexes, and global shared variables. The following will introduce the two representative methods of named mutex and global shared variable:

// Create mutex
HANDLE m_hMutex = CreateMutex(NULL, FALSE, "Sample07");
// Check error code
if (GetLastError() == ERROR_ALREADY_EXISTS) {
 // If mutex already exists, release handle and reset mutex
 Mutex CloseHandle(m_hMutex); m_hMutex
 = NULL;
 // program exit
 return FALSE;
}

  The above code demonstrates the usage of named mutex in process mutex. The core of the code is the creation of a named mutex by CreateMutex(). The CreateMutex() function can be used to create a named or unnamed mutex object, and its function prototype is:

HANDLE CreateMutex(
 LPSECURITY_ATTRIBUTES lpMutexAttributes, // pointer to security attributes
 BOOL bInitialOwner, // initializes the owner of the mutex object
 LPCTSTR lpName // pointer to the name of the mutex object
);

  If the function executes successfully, it will return a handle to the mutex object. If a mutex with the same name already exists before CreateMutex() is executed, the function will return a handle to the existing mutex, and the error code ERROR_ALREADY_EXIST can be obtained through GetLastError(). It can be seen that the mutual exclusion of the process by CreateMutex() can be realized by detecting the error code ERROR_ALREADY_EXIST.

        Create a mutex for synchronization. If one thread acquires the mutex, the second thread that wants to acquire the mutex is suspended until the first thread releases the mutex.

The parameter
lpMutexAttributes
points to a pointer to a SECURITY_ATTRIBUTES structure that determines whether mutex handles are inherited by child processes.     
bInitialOwner
Boolean type, determines whether the mutex creator is the owner
lpName
pointer to the mutex name string. Mutexes can have names.
The advantage of mutexes is that they can be shared between processes

Experience:
    CreateMutex() is used for programs with exclusive requirements (do not allow other programs that use this port device to run during the running of its process, or do not allow programs with the same name to run). If the program with the same name is running, the error code ERROR_ALREADY_EXIST will be obtained through GetLastError() . The result obtained by executing it just now (program name samp)        In general: as soon as the debugging phase is entered, the samp process appears in the process manager, and the process handle is returned when CreateMutex is executed, and if(GetLastError() == is executed) ERROR_ALREADY_EXISTS ) When judging, skip and do not execute the content in if, so it means that there is no mutual exclusion.        Before debugging, run samp.exe in debug and then debug: As soon as the debugging stage is entered, two samp processes appear in the process manager. When the CreateMutex is executed, the process handle is returned, and the execution is carried out to if(GetLastError() == ERROR_ALREADY_EXISTS ) When judging, the content in if is executed, indicating that there is mutual exclusion.




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325866042&siteId=291194637