Ok, this is extending too much for such a simple thing. Laveer: How are you verifying that the mutex is not being destroyed? How do you know for sure? Also show the code that closes the handle and explain when it is called.
I'm using Process Explorer to view the given handles being run off by the process. As far as the code for actually closing the mutex handle, I have yet to figure out how to actually close it, that's what I created this thread. As stated I've used ; HANDLE hMutex = OpenMutex(MUTEX_ALL_ACCESS, TRUE, "wpavkdlxjabxprtm");
To create / duplicate the mutex string I'm trying to rid myself of. Though I can't quite get the syntax for closing a mutex string... I've also tried ; CloseHandle(hMutex); and CloseHandle("wpavkdlxjabxprtm");
First and foremost: OpenMutex() will only succeed if the mutex already exists. If it doesn't exist, it FAILS. See http://msdn.microsoft.com/en-us/library/windows/desktop/ms684315(v=vs.85).aspx for detailed information. Since you are never mentioning CreateMutex(), I imagine you never use it. Well, this IS the function you must use first. If the mutex already exists, CreateMutex() will perform an Open operation. You must always check the return values of the functions. Never assume they worked.
You can successfully close a mutex handle using CloseHandle(hMutex);. This will also destroy the mutex if the handle being closed was the last handle open for that mutex.
Sorry for being rather slow, though I am still thoroughly learning C/C++. Lets say I wanted to use CloseHandle(hMutex); to close "wpavkdlxjabxprtm", would I need to specify wpavkdlxjabxprtm anywhere? I've tried doing so as simply CloseHandle("wpavkdlxjabxprtm"); though my results were ultimately crap... Where would I specify in the code it's a mutex handle?
HANDLE hMutex = CreateMutex(
NULL //No special security descriptor
, TRUE //Or false, whatever is best
//Make sure of a successful mutex creation.
if (hMutex == NULL)
//Mutex creation failed. Use GetLastError() and FormatMessage() to learn why it failed.
return; //Or return a failure code, or whatever applies to your case.
//Now hMutex is a handle to a mutex named wpavkdlxjabxprtm.
//Ok, mutex no longer necessary. Close the handle that represents it.
//This destroys the underlying mutex if this is the last handle to that mutex.
//This is the mutex named wpavkdlxjabxprtm.
I've gotten advice from an old friend, he told me as I stated in the main post that it can easily be done by duplicating the mutex handle and closing it's source. Sadly I don't understand what that means and he had to get off the phone to take his sister to his aunts house for the weekend.
If it helps, this individual was trying to do exactly as I am now and figured it out by doing again as stated above in bold ;
A Mutex is an object created in the kernel. You can't access it directly, but when you ask for it, the kernel will give you a handle to it. This handle is just a handle, not the object itself. The object is own managed and manipulated by the kernel. When you close your handle, you're just revoking access to the object. The kernel may keep the object around if it needs to.
The handle is just for your process, or your child's. You can copy the handle with DuplicateHandle, but that's just asking the kernel for another legitimate handle to the same object.
The handle is just an integral value (a number) and is meaningless outside the context it was meant for (your process).
The handle is not the mutex, just a reference to the mutex.