Kernel32CreateMutex Method

Creates or opens a named or unnamed mutex object.

To specify an access mask for the object, use the CreateMutexEx(SecurityAttributes, String, CreateMutexFlags, StandardAccessRights) function.

Definition

Namespace: DevCase.Win32.NativeMethods
Assembly: DevCase.net48 (in DevCase.net48.dll) Version: 6.0.0.0 (6.0)
XMLNS for XAML: Not mapped to an xmlns.
[DllImportAttribute("Kernel32.dll", CharSet = CharSet.Auto, BestFitMapping = false, 
	ThrowOnUnmappableChar = true, SetLastError = true)]
public static IntPtr CreateMutex(
	[OptionalAttribute] SecurityAttributes mutexAttributes,
	bool initialOwner,
	[OptionalAttribute] string name
)

Parameters

mutexAttributes  SecurityAttributes  (Optional)
A pointer to a SecurityAttributes structure.

If this parameter is NULL, the mutex handle cannot be inherited by child processes.

The SecurityDescriptor member specifies a security descriptor for the new mutex.

If mutexAttributes is NULL, the mutex gets a default security descriptor.

The ACLs in the default security descriptor for a mutex come from the primary or impersonation token of the creator.

initialOwner  Boolean
If this value is and the caller created the mutex, the calling thread obtains initial ownership of the mutex object. Otherwise, the calling thread does not obtain ownership of the mutex.

To determine if the caller created the mutex, see the return value section.

name  String  (Optional)
The name of the mutex object. The name is limited to MAX_PATH characters. Name comparison is case sensitive.

If name is NULL, the mutex object is created without a name.

If name matches the name of an existing event, semaphore, waitable timer, job, or file-mapping object, the function fails and the GetLastError function returns ERROR_INVALID_HANDLE. This occurs because these objects share the same namespace.

The name can have a "Global" or "Local" prefix to explicitly create the object in the global or session namespace. The remainder of the name can contain any character except the backslash character ().

Return Value

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

If the function fails, the return value is Zero.

If the mutex is a named mutex and the object existed before this function call, the return value is a handle to the existing object, GetLastError returns ERROR_ALREADY_EXISTS., initialOwner is ignored, and the calling thread is not granted ownership.

However, if the caller has limited access rights, the function will fail with ERROR_ACCESS_DENIED and the caller should use the OpenMutex(UInt32, Boolean, String) function.

Remarks

See Also