#include <winhvplatform.h>
#include <memoryapi.h>
add(int a ,int b){
return a+b;
}
硬编码
unsigned char code[] =
{ 0x55,
0x48,0x89,0xe5,0x89,0x4d,0x10,0x89,
0x55,0x18,0x8b,0x55,0x10,0x8b,0x45,0x18,
0x01,0xd0,0x5d,0xc3,
};
申请可执行的内存
LPVOID pAdd = VirtualAlloc(nullptr, sizeof(code), MEM_COMMIT, PAGE_EXECUTE_READWRITE);
memcpy(pAdd, code, sizeof(code));
int (__cdecl *Add)(int, int) = (int (__cdecl *)(int, int)) pAdd;
int ret = Add(1, 3);
运行结果
ret = 4