I see a few problems.
I assume that 0x00477C3E is the address of the original VM_Create function. You really shouldn't do this. Use &VM_Create . Of course, this will mean that you need to use a different name for your replacement function.
The offset is not calculated correctly. You have the wrong sign. What else is the offset applied to the instruction pointer at the end of the instruction, and not to the beginning. Therefore, you need to shift it by 5 (instruction size). The offset must also be a signed integer.
Ideally, given my first point, the code would look like this:
int32_t offset = (int32_t)&New_VM_Create - ((int32_t)&VM_Create+5);
Thanks to Hans Passant for fixing my own stupid bug in the original version!
If you are working on a 64-bit machine, you need to perform arithmetic in 64 bits and, as soon as you calculate the offset, truncate it to a 32-bit offset.
Another caveat is that you should reset to save memory only after writing a new JMP instruction and calling FlushInstructionCache .
source share