title: Close handle to a mutex in another process
body: <p>I want to close a handle to a mutex located in another process, so I can run more than one instance of the application.</p>
  
  <p>I already know this can be done, <a href="http://technet.microsoft.com/en-us/sysinternals/bb896653" rel="nofollow noreferrer">see Process Explorer</a>. Example: Windows <em>Minesweeper</em> (Windows 7) uses a mutex to only allow one game, so I thought I would use it as an example since it's pre-installed with Windows and therefore easier for you guys to guide me.</p>
  
  <p>The mutex that I need to close is <code>\Sessions\1\BaseNamedObjects\Oberon_Minesweeper_Singleton</code>, which I found using Process Explorer.</p>
  
  <p>After closing this mutex I was able to launch two games of <em>Minesweeper</em>, but I want to do this in my program using C++.</p>
  
  <p>After some searching I have found that I might need the API <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms724251%28v=vs.85%29.aspx" rel="nofollow noreferrer">DuplicateHandle</a>. So far I haven't been able to close the handle on this mutex.</p>
  
  <p>Here is my code so far:</p>
  
  <pre><code>#include &lt;Windows.h&gt;
  #include &lt;iostream&gt;
  
  using namespace std;
  
  void printerror(LPSTR location){
      printf("Error: %s_%d", location, GetLastError());
      cin.get();
  }
  
  int main(){
      DWORD pid = 0;
      HWND hMineWnd = FindWindow("Minesweeper", "Minesveiper");
      GetWindowThreadProcessId(hMineWnd, &amp;pid);
      HANDLE hProc =OpenProcess(PROCESS_DUP_HANDLE, 0, pid);
      if(hProc == NULL){
          printerror("1");
          return 1;
      }
      HANDLE hMutex = OpenMutex(MUTEX_ALL_ACCESS, TRUE, "Oberon_Minesweeper_Singleton");
      if(hMutex == NULL){
          printerror("2");
          return 2;
      }
      if(DuplicateHandle(hProc, hMutex, NULL, 0, 0, FALSE, DUPLICATE_CLOSE_SOURCE) == 0){
          printerror("3");
          return 3;
      }
      if(CloseHandle(hMutex) == 0){
          printerror("4");
          return 4;
      }
      return 0;
  }
  </code></pre>
  
  <p>This code returns 0, but the mutex is still there, and I am not able to launch more games of Minesweeper. I think some of my parameters to DuplicateHandle are wrong.</p>