Closing a Mutex Handle

Pages: 12
I'm trying to close a mutex handle, which permits one to only run one instance of the program. I know I can open a handle by doing this ;

HANDLE hMutex = OpenMutex(MUTEX_ALL_ACCESS, TRUE, "0");

Though I'm trying to close it. I've already tried CloseHandle and WaitHandle... I'm pretty sure the setup used in ;
Handle hMutex = *OpenMutex*(MUTEX_ALL_ACCESS, TRUE, "0";

Should work with a different use of syntax on OpenMutex though I'm simply stumped.

I read another thread in which someone had the same issue and they said they did it by duplicating the handle and closing it's source. If anyone can explain to me how thats done please do so.
You close your handle to the mutex with CloseHandle.

If you've already called CloseHandle, why do you think it's not closed?
This was what I used alongside with CloseHandle ;


The 0 being the handle name of course. Would I need to add anything concerning the handle being a Mutex in there? If so how would I do so.
closed account (S6k9GNh0)
You're not passing 0 directly are you? Either way, check for errors that CloseHandle returns. That's why it's there.
Use this

CloseHandle(hMutex); //and not CloseHandle(0), it takes HANDLE as parameter 
I'm not using 0 as my actual Mutex value, the mutex handle I'm trying to close is "wpavkdlxjabxprtm".

@Pravesh I'll try that and post my results, thank you.
closed account (DSLq5Di1)
Are you trying to close the mutex of another application..?

Nvm, seems I answered my own question. *rolls eyes*
Last edited on
I've tried everything and still no results, I've tried opening a copy of the handle and closing one of them though it didn't do any good.
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.

Also, what is the mutex name?
Last edited on
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");

Note "wpavkdlxjabxprtm" is the handle name.
Last edited on
First and foremost: OpenMutex() will only succeed if the mutex already exists. If it doesn't exist, it FAILS. See 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?
Last edited on
HANDLE hMutex = CreateMutex(
    NULL //No special security descriptor
    , TRUE //Or false, whatever is best
    , TEXT("wpavkdlxjabxprtm")
//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.

closed account (DSLq5Di1)
He is trying to release a mutex created and owned by another application, google the mutex name. :P
closed account (S6k9GNh0)
Laveer, this doesn't work for reasons that are difficult to explain.

For one, that handle isn't in the "scope" of the program and you don't even have the actual handle.

For two, it's "theoretically possible with shared memory" but don't do this. It's really bad in design and in implementation.

Third, for the very last time, CHECK FOR ERRORS. Had you used GetLastError earlier, you would have realized your problem 20x sooner.

Last edited on
Ah..... thank you sloppy9! That clears A LOT. Then forget about all my posts. They won't help. You cannot hijack and close a mutex handle in another process. Just let go.
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 ;

Last edited on
I really don't know about that. To duplicate a handle you use DuplicateHandle(), but I don't recall ever reading about such functionality. Check it out then.
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.
Closing it in proc ex. allows me to open another process though, that's what I'm trying to accomplish. Simply to close the handle as process explorer does.
Pages: 12