My OS is Vista with UAC turned on, I create a global Mutex object in Server side, then my AP with UI want to use CreateMutex with same name to get the Mutex object which has been created in server, but the function tell me I don't have right to access it. How can I do it?
+3
A:
I think in your case you'll need to explicitly allow all-access to your mutex via initializing corresponding security attributes.
Try creating mutex this way (consider it as semi-pseudo-code):
SECURITY_ATTRIBUTES sa;
SECURITY_DESCRIPTOR sd;
InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorDacl(&sd, TRUE, NULL, FALSE);
sa.nLength = sizeof(sa);
sa.lpSecurityDescriptor = &sd;
CreateMutex(&sa, ...);
And by the way - it is Ok to use CreateMutex to open an existing mutex. But, OpenMutex allows you to specify required access level.
Also note, that if you need a really global mutex - you'll have to prefix it's name with "Global\" (refer to MSDN's "Kernel Object Namespaces" article)
Andrey
2009-05-15 16:52:23
well, it is not a matter of good practice: imagine that you have a global hook DLL having to initialize and access the same mutex - OpenMutex is not convenient in this scenario. But if in your code creation of a mutex and accessing that mutex can be easily separated - then you code will be more clear if CreateMutex creates and OpenMutex opens existing mutex with desired access rights
Andrey
2009-05-16 09:22:37
Thanks, it's woring well now.
Yigang Wu
2009-05-17 14:27:52