OpenMutexW function (synchapi.h)

Opens an existing named mutex object.

Syntax

HANDLE OpenMutexW(
  [in] DWORD   dwDesiredAccess,
  [in] BOOL    bInheritHandle,
  [in] LPCWSTR lpName
);

Parameters

[in] dwDesiredAccess

The access to the mutex object. Only the SYNCHRONIZE access right is required to use a mutex; to change the mutex's security, specify MUTEX_ALL_ACCESS. The function fails if the security descriptor of the specified object does not permit the requested access for the calling process. For a list of access rights, see Synchronization Object Security and Access Rights.

[in] bInheritHandle

If this value is TRUE, processes created by this process will inherit the handle. Otherwise, the processes do not inherit this handle.

[in] lpName

The name of the mutex to be opened. Name comparisons are case sensitive.

This function can open objects in a private namespace. For more information, see Object Namespaces.

Terminal Services:  The name can have a "Global" or "Local" prefix to explicitly open an object in the global or session namespace. The remainder of the name can contain any character except the backslash character (\). For more information, see Kernel Object Namespaces.

Note  Fast user switching is implemented using Terminal Services sessions. The first user to log on uses session 0, the next user to log on uses session 1, and so on. Kernel object names must follow the guidelines outlined for Terminal Services so that applications can support multiple users.

Return value

If the function succeeds, the return value is a handle to the mutex object.

If the function fails, the return value is NULL. To get extended error information, call GetLastError.

If a named mutex does not exist, the function fails and GetLastError returns ERROR_FILE_NOT_FOUND.

Remarks

The OpenMutex function enables multiple processes to open handles of the same mutex object. The function succeeds only if some process has already created the mutex by using the CreateMutex function. The calling process can use the returned handle in any function that requires a handle to a mutex object, such as the wait functions, subject to the limitations of the access specified in the dwDesiredAccess parameter.

The handle can be duplicated by using the DuplicateHandle function. Use the CloseHandle function to close the handle. The system closes the handle automatically when the process terminates. The mutex object is destroyed when its last handle has been closed.

If your multithreaded application must repeatedly create, open, and close a named mutex object, a race condition can occur. In this situation, it is better to use CreateMutex instead of OpenMutex, because CreateMutex opens a mutex if it exists and creates it if it does not.

Examples

For an example that uses OpenMutex, see Using Named Objects.

Requirements

Requirement Value
Minimum supported client Windows XP [desktop apps | UWP apps]
Minimum supported server Windows Server 2003 [desktop apps | UWP apps]
Target Platform Windows
Header synchapi.h (include Windows.h)
Library Kernel32.lib
DLL Kernel32.dll

See also

CloseHandle

CreateMutex

CreateProcess

DuplicateHandle

Mutex Objects

Object Names

ReleaseMutex

Synchronization Functions