1

I currently am able to close a mutex handle by using the following code:

DuplicateHandle(process, mutex, GetCurrentProcess(), 0, 0, 0, 1);
CloseHandle(mutex);

This enables me to run multiple instances of a program. However I need to reinstate this handle after I've launched multiple instances.
Is there any way to temporarily rename or disable a mutex handle?

Fabio Ceconello
  • 15,819
  • 5
  • 38
  • 51
Tompina
  • 726
  • 1
  • 9
  • 25
  • You need to clarify what exactly the mutex has to do with running multiple instances; you can always launch more instances with CreateProcess() and that doesn't involve mutexes. From your code I infer you called CreateProcess before and used DuplicateHandle to pass the mutex to the new process. What's the purpose of that? Do you need to share a mutex between all the processes? If so, you can create a named mutex. Or do you need to have a 'catalog' of mutexes in the parent process that were passed to each of its children? Then just there's no need to call CloseHandle. – Fabio Ceconello Dec 19 '15 at 20:18
  • Whenever I run the program it checks if the named mutex exists, if it does it wont run another instance, if it doesn't, it lets me run another instance. – Tompina Dec 19 '15 at 20:41
  • 3
    Possible duplicate of [Making a singleton application across all users](http://stackoverflow.com/questions/23579614/making-a-singleton-application-across-all-users). While this question asks for a single instance across all users, the underlying concepts are identical. The answer outlines, how to ensure a single instance per desktop as well. – IInspectable Dec 19 '15 at 20:47
  • OK, now I think I understood what you want to do: normally, your program doesn't allow multiple instances to run (using the method mentioned by @IInspectable); but sometimes you want to temporarily disable that logic and allow additional instances. Is that correct? – Fabio Ceconello Dec 19 '15 at 21:45
  • In that case I'd suggest to use a named event instead of a mutex. The event state can then be used to signal the other process if it can or can't run. – Fabio Ceconello Dec 19 '15 at 22:02
  • You can pass a command-line argument to the child telling it to run despite the mutex. – Harry Johnston Dec 19 '15 at 22:06
  • Note that you're closing `mutex` twice, which results in undefined behaviour. You shouldn't do that. (From your description of the observed result, the duplicate handle happens to have the same value as the original handle, but there's no guarantee of that.) – Harry Johnston Dec 19 '15 at 22:16

1 Answers1

2

Is there any way to temporarily rename or disable a mutex handle?

No, there is not. All you can do is close the original mutex handle, and then later create a new mutex handle using the original name, if needed.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770