The createmutex () method prevents the program from being started twice ()

Source: Internet
Author: User
Add the following to the winmain function in the project file: Code (This code runs in bcb6.0 ):
Handle hmutex = createmutex (null, false, "process ");
If (getlasterror () = error_already_exists)
{
Closehandle (hmutex );
MessageBox (Application-> handle ," Program It is already running and cannot be started again! "," Prompt ", mb_ OK + mb_iconwarning );
Application-> terminate ();
Return 0;
}
Application-> createform (_ classid (tform1), & form1 );

The createmutex () function, 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

Mutex operation of processes

Under normal circumstances, the running of a process generally does not affect other running processes. However, programs with special requirements, such as using a serial port in an exclusively-owned manner, require that other programs that attempt to use the port device do not run during the process, in addition, such programs generally do not allow multiple instances to run the same program. This leads to the process mutex problem.

The core idea of implementing process mutex is relatively simple: when a process is started, first check whether the current system already has an instance of this process. If not, the process will successfully create and set the tag that identifies the existing instance. Then, when you create a process, you will be aware that its instance already exists through this tag, so that the process can only have one instance in the system. You can use multiple methods, such as memory ing files, the number of famous events, the number of famous mutex, and global shared variables. The following describes two representative methods:

// Create mutex
Handle m_hmutex = createmutex (null, false, "sample07 ");
// Check the error code
If (getlasterror () = error_already_exists ){
// If a mutex exists, release the handle and reset the mutex.
Closehandle (m_hmutex );
M_hmutex = NULL;
// Exit the program
Return false;
}

The above Code demonstrates the usage of the famous mutex in process mutex. The core of the Code is the creation of the famous mutex by createmutex. The createmutex () function can be used to create a famous or unknown mutex object. Its prototype is:

Handle createmutex (
Lpsecurity_attributes lpmutexattributes, // pointer to the Security Attribute
Bool binitialowner, // initialize the owner of the mutex object
Lptstr lpname // pointer to the mutex object name
);

If the function is successfully executed, a handle of the mutex object is returned. If a mutex with the same name already exists before createmutex () is executed, the function returns the handle with the same mutex and obtains the error code error_already_exist through getlasterror. It can be seen that through the detection of the error code error_already_exist, createmutex () can be mutually exclusive to the process.

Create a mutex for synchronization. If a thread obtains the mutex, the second thread to obtain the mutex will be suspended until the first thread releases the mutex.

Parameters
Lpmutexattributes
Pointer to a security_attributes structure, which determines whether the mutex handle is inherited by the quilt process.
Binitialowner
Boolean Type, determines whether the creator of the mutex is the owner
Lpname
Pointer to the mutex name string. The mutex can have a name.
The advantage of mutex is that it can be shared between processes.

Experience:
Createmutex () is used for programs with exclusive requirements (other programs that use this port device are not allowed during the running of their processes, or programs with the same name are not allowed ). If a program runs with the same nameGetlasterror ()Get error codeError_already_exist.

The following result is executed just now (program name SAMP)
Generally, the samp process appears in the Process Manager as soon as it enters the debugging stage. When the createmutex process is executed, the Process Handle is returned and the IF (getlasterror () = error_already_exists) process is executed) when determining, skip the content in the IF statement without execution, so it indicates that there is no mutex.
Run samp.exe in debugbefore debugging: as soon as the debugging stage is started, two SAMP processes appear in the Process Manager. When createmutex is executed, the Process Handle is returned and if (getlasterror () = error_already_exists) is executed) when determining, execute the content in IF, indicating that there is a mutex.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.