title: Several programs crash when unhooking with UnhookWindowsHookEx()
body: <p>I am doing a global hook to add my DLL to the hook chain:</p>
  
  <pre><code>HHOOK handle = SetWindowsHookEx(WH_CALLWNDPROC, addr, dll, 0);
  </code></pre>
  
  <p>Inside my DLL I am using Detours to intercept several WINAPI function calls. Everything works fine, except for WaitForSingleObject calls. Whenever I add WaitForSingleObject to the detoured functions, several programs crash when I unhook my DLL (Chrome, Skype, ...).
  Here is how the DLL looks:</p>
  
  <pre><code>DWORD (WINAPI* Real_WaitForSingleObject)( HANDLE hHandle, DWORD dwMilliseconds) = WaitForSingleObject;
  DWORD WINAPI Mine_WaitForSingleObject(HANDLE hHandle, DWORD dwMilliseconds);
  INT APIENTRY DllMain(HMODULE hDLL, DWORD Reason, LPVOID Reserved) {
  
      switch(Reason) {
          case DLL_PROCESS_ATTACH: 
              DetourTransactionBegin();
              DetourUpdateThread(GetCurrentThread());
              DetourAttach(&amp;(PVOID&amp;)Real_WaitForSingleObject, Mine_WaitForSingleObject);
              DetourTransactionCommit();
              break;
          case DLL_PROCESS_DETACH: 
              DetourTransactionBegin(); 
              DetourUpdateThread(GetCurrentThread());
              DetourDetach(&amp;(PVOID&amp;)Real_WaitForSingleObject, Mine_WaitForSingleObject);
              DetourTransactionCommit();
              break;
          case DLL_THREAD_ATTACH:
  
              break;
          case DLL_THREAD_DETACH:
  
              break;
      }
      return TRUE;
  }
  DWORD WINAPI Mine_WaitForSingleObject(HANDLE hHandle, DWORD dwMilliseconds) {
  
      return Real_WaitForSingleObject(hHandle, dwMilliseconds);
  }
  
  extern "C" __declspec(dllexport) int meconnect(int code, WPARAM wParam, LPARAM lParam) {
  
      return CallNextHookEx(NULL, code, wParam, lParam);
  }
  </code></pre>
  
  <p>Could someone help me to understand why this is happening and how I can get around that Problem? Thanks!</p>