title: Hot Patching A Function
body: <p>I'm trying to hot patch an exe in memory, the source is available but I'm doing this for learning purposes. (so please no comments suggesting i modify the original source or use detours or any other libs)</p>
  
  <p>Below are the functions I am having problems with.</p>
  
  <pre><code>vm_t* VM_Create( const char *module, intptr_t (*systemCalls)(intptr_t *), vmInterpret_t interpret )
  {
      MessageBox(NULL, L"Oh snap! We hooked VM_Create!", L"Success!", MB_OK);
      return NULL;
  }
  
  void Hook_VM_Create(void)
  {
  
      DWORD dwBackup;
      VirtualProtect((void*)0x00477C3E, 7, PAGE_EXECUTE_READWRITE, &amp;dwBackup);
  
      //Patch the original VM_Create to jump to our detoured one.
      BYTE *jmp = (BYTE*)malloc(5);
      uint32_t offset = 0x00477C3E - (uint32_t)&amp;VM_Create; //find the offset of the original function from our own
      memset((void*)jmp, 0xE9, 1);
      memcpy((void*)(jmp+1), &amp;offset, sizeof(offset));
      memcpy((void*)0x00477C3E, jmp, 5);
  
      free(jmp);
  }
  </code></pre>
  
  <p>I have a function VM_Create that I want to be called instead of the original function. I have not yet written a trampoline so it crashes (as expected). However the message box does not popup that I have detoured the original VM create to my own. I believe it is the way I'm overwriting the original instructions.</p>