title: C++ WriteProcessMemory makes MessageBox crash the programm
body: <p>i want to write a little AntiHack and i have a problem with one part of it.
  The idea was to prevent from dll injecting by hooking the LdrLoadDll from NTDLL.DLL. I found a function by googling which do this:</p>
  
  <pre><code>// function call: BlockAPI(m_hProc, "NTDLL.DLL", "LdrLoadDll");    
  bool zProtect::BlockAPI(HANDLE hProcess, char* libName, char* apiName)
  {
      BYTE pRet[]={ 0x31, 0xC0, // XOR eax, eax
                      0xC3 };    // RET
      HINSTANCE hLib = NULL;
      VOID *pAddr = NULL;
      bool bRet = FALSE;
      DWORD dwRet = 0;
  
  
      hLib = LoadLibrary(libName);
      if(hLib) 
      {
          pAddr = (VOID*)GetProcAddress(hLib, apiName);
          if(pAddr) 
      {
          DWORD dwback;
          if(!VirtualProtectEx(hProcess, (LPVOID)pAddr, sizeof(pRet), PAGE_EXECUTE_READWRITE, &amp;dwback))
              return false;
          if(WriteProcessMemory(hProcess, (LPVOID)pAddr, &amp;pRet, sizeof (pRet), &amp;dwRet)) 
          {
              if(dwRet)
                  bRet = TRUE;
          }
          if(!VirtualProtectEx(hProcess, (LPVOID)pAddr, sizeof(pRet), dwback, &amp;dwback))
              return false;
      }
          FreeLibrary(hLib);
     }
     return bRet;
  }
  </code></pre>
  
  <p>It's working fine, BUT MessageBox(NULL, msg, "DETECTED", MB_OK); crashes if it is excuted after BlockApi(..);</p>
  
  <blockquote>
    <p>Ausnahme (erste Chance) bei 0x75312113 (user32.dll) in DLLTester.exe: 0xC0000005: Zugriffsverletzung beim Lesen an Position 0x0000002D
    Unbehandelte Ausnahme bei 0x75312113 (user32.dll) in DLLTester.exe: 0xC000041D: Ausnahmefehler während eines Benutzerrückrufs</p>
  </blockquote>
  
  <p>Thanks,
  lolxdfly</p>
  
  <p><strong>Edit:</strong>
  I found out, that before the crash with the MessageBox happen the previous call is the Sleep(1000); from my detecting thread!</p>
  
  <p>Old Problem (Crash in Release Mode) was fixed!</p>