Mutex creation and opening failing?

closed account (13bSLyTq)
Hi,

I am learning to use Mutexes and I tried to create a basic mutex using CreateMutex then see if the mutex is there via using OpenMutex().

However....

I did created 2 projects (CreateMutant.sln, IdentifyMutant.sln):

CreateMutant.cpp:
1
2
3
4
5
6
7
8
#include<Windows.h>
#include<conio.h>

int main()
{
	CreateMutex(0,false,"TESTMUTEX");
    _getch(); //Halt Program
}


IdentifyMutant.cpp:
1
2
3
4
5
6
7
8
9
#include<Windows.h>
#include<conio.h>

HANDLE h = OpenMutex(0, true, "TESTMUTEX");
	if ( h == 0)
	{
          MessageBoxA(0,"We did not get any handle","Error",MB_ICONERROR);
        }
_getch();



When I start - CreateMutant.exe there is no error: IT WORKS!

then I while CreateMutant.exe is still running I start IdentifyMutant.exe.

BUT IdentifyMutant.exe gives me a error code 2:

ERROR_FILE_NOT_FOUND
2 (0x2)
The system cannot find the file specified.

Reference: http://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx


My question here is why is this throwing this error? Am I using the function correctly?

Thanks!
Try OpenMutex(SYNCHRONIZE, true, "TESTMUTEX");

The documentation says that only the SYNCHRONIZE access right is required to use a mutex
http://msdn.microsoft.com/en-us/library/windows/desktop/ms684315(v=vs.85).aspx

I checked here http://msdn.microsoft.com/en-us/library/windows/desktop/ms686670(v=vs.85).aspx and 0x00000000 is not a valid access right.
Last edited on
closed account (13bSLyTq)
Hi,

It still does not function\work as expected. I still get:

ERROR_FILE_NOT_FOUND
2 (0x2)
The system cannot find the file specified.
I tested the code and it worked for me (my solution). Check what CreateMutex returns.
closed account (13bSLyTq)
Hi,

I got the correct handle; no errors,

handle: 0x000000a8

I got no errors when creating the Mutex but in terms of Opening the mutex I am confused.
closed account (13bSLyTq)
Hi,

I found the Error, it is not SYNCHRONIZE but instead MUTEX_ALL_ACCESS similar to OpenProcess function dwDesiredAccess parameter.
Forgive me for wasting your precious time.


Thanks! Null
Last edited on
Topic archived. No new replies allowed.