Createmutex Function
Check whether the current system has an instance of the specified process. If not, create a mutex. The createmutex () function can be used to create a famous or unknown mutex object. Its function prototype is
VC statement
Handle createmutex (
Lpsecurity_attributesLpmutexattributes, // Pointer to the Security Attribute
BoolBinitialowner, // Initialize the owner of the mutex object
LpctstrLpname// Pointer to the mutex object name
);
Edit this section Description
Create a mutex)
Return Value
Long,
If the execution is successful, the handle of the mutex object is returned. Zero indicates an error. Getlasterror is set. Even if a valid handle is returned, if the specified name already exists, getlasterror is set to error_already_exists.
Parameter table
Parameter type and description
Lpmutexattributes security_attributes, specifying a security_attributes structure, or passing a zero value (declaring the parameter as byval as long and passing the zero value), indicates that the default descriptor that cannot be inherited is used.
Binitialowner long. If the creation process wants to have a mutex immediately, set it to true. A mutex can only be owned by one thread.
Lpname string, which specifies the name of the mutex object. Use vbnullstring to create an unnamed mutex object. If an event with this name already exists, open the existing named mutex. This name may not match the existing event, signal, wait timer, or file ing.
Edit this section Annotation
Once it is no longer needed, you must use the closehandle function to close the mutex handle. After all the handles that belong to it are closed, the object will be deleted.
Before stopping a process, release the mutex. If you do not take this action, the mutex is marked as obsolete and the ownership is automatically released. Other applications that share this mutex may still be able to use it, but will receive an obsolete state information indicating that the previous process failed to close normally. The impact of this situation depends on the specific application involved.Program
Edit this section Example:
(1), h_mutex1 = createmutex (null, false, "mutex_for_readcount"); // create a mutex
(2), handle m_hmutex = createmutex (null, false, "sample07"); // check the error Code
If (getlasterror () = error_already_exists)
{< br> // If a mutex exists, release the handle and reset the mutex.
closehandle (m_hmutex);
m_hmutex = NULL;
// exit the Program
return false;
};
// The code above demonstrates the usage of the famous mutex in process mutex. The core of the Code is the creation of the famous mutex by createmutex.
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 ).