Well, OP
is an uninitialized pointer. You cannot pass that to VirtualProtect
. Instead of
PDWORD OP;
you need:
DWORD OldProtect;
And then pass &OldProtect
to VirtualProtect
.
VirtualProtect(code_ptr, 14, PAGE_EXECUTE_WRITECOPY, &OldProtect);
You existing code fails on the call to VirtualProtect
. You don't check for errors and so continue regardless. Then the call to memcpy
fails with a general protection fault, because the memory is read only.
Even if you fix your code, I doubt that it will work though. I see no reason for dummy
to be 14 bytes long. You are relying on luck and wishful thinking. So you will probably overwrite the code that you are executing.
If you want 14 bytes of memory to write your code to, call VirtualAlloc
. That way you can be certain of success.
As a piece of general advice, you will need to get into the habit of checking return values for errors. You call VirtualProtect
and ignore the return value. How do you know that your call to VirtualProtect
was successful?