Second Process Program Example
The second process uses the OpenMutex() function to open a handle to the existing mutex. This function fails if a mutex object with the specified name does not exist. The access parameter requests full access to the mutex object, which is necessary for the handle to be used in any of the wait functions.
Create a new empty Win32 console application project. Give a suitable project name and change the project location if needed.
Then, add the source file and give it a suitable name.
Next, add the following source code.
// This process opens a handle to a mutex created by another process
hMutex = OpenMutex(
MUTEX_ALL_ACCESS, // request full access
FALSE, // handle not inheritable
L"MyGedikMutex"); // object name
if (hMutex == NULL)
wprintf(L"OpenMutex() failed, error %d\n", GetLastError() );
wprintf(L"OpenMutex() successfully opened the mutex...\n");
if(CloseHandle(hMutex) != 0)
wprintf(L"CloseHandle() is OK!\n");
wprintf(L"CloseHandle() failed with error %d\n", GetLastError());
Firstly, re-run the previous program, then this program. The following screenshot is a sample output.