Thomas Weidenmueller wrote:
Joseph Galbraith wrote:
Regardless of whether the mutex is newly created or pre-existing, CreateMutex() will return a handle to it.
CreateMutex fails if a named object is supposed to be created but already exists. GetLastError() then will - as you mentioned below - return ERROR_ALREADY_EXISTS or another failure code.
This is not correct. CreateMutex fails only if the named object already exists and if the object is not a mutex.
In the case of a pre-existing mutex, the handle to the existing object is returned.
Not by CreateMutex.
This is not correct. If the mutex already exist, CreateMutex returns a handle to the existing mutex.
If CreateMutex() returns NULL, it failed for some other reason (than that the mutex already existed, since CreateMutex doesn't fail in this case.)
Quote from the PSDK: "If lpName 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 name space."
Some lines later in the PSDK: "If the function succeeds, the return value is a handle to the mutex object. If the named mutex object existed before the function call, the function returns a handle to the existing object and GetLastError() returns ERROR_ALREADY_EXISTS. Otherwise, the caller created the mutex."
- Hartmut