I think the two functions will return the same kernel handle.
And I write a test programme to prove my view:
In my programme I create a kernel handle with name ‘_MYTEST’
hHandle1 = CreateMutex(NULL, false, _T("_MYTEST"));
then I start a thread and open the handle above in this thread with the following code:
hHandle2 = OpenMutex(MUTEX_ALL_ACCESS, false, _T("_MYTEST"));
when I run the programme, I find the two return values aren’t equal!
The value of hHandle2 is 4 greater than the value of hHandle1.
Why?Are there any problems in my code?If not, why are the two values not equal?
A handle isn’t the object, it’s a descriptor stored in the kernel mapping a value valid only in your program’s address space to a kernel object.
That’s the long way of saying: a handle to the mutex is not the mutex itself. Just because the handles are different does not mean the mutexes underneath are.
That’s like creating two different pointers to one object in C, then comparing the addresses of the pointers (vs the contents of the pointer) to falsely deduce the objects are not the same.