I have a raw code and I want to execute it. To execute the raw code I have allocated memory, copied the raw code the allocated memory and used VirtualProtect then tried to execute it.
unsigned char RawCode[7680] = {...};
int main()
{
DWORD old_protect;
LPVOID executable = VirtualAlloc(NULL, 7680, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
if (executable != 0)
{
memcpy(executable, RawCode, 7680);
VirtualProtect(executable, 7680, PAGE_EXECUTE_READWRITE, &old_protect);
int(*f)() = (int(*)()) executable;
f(); // Throws the exception at this line.
VirtualProtect(executable, 7680, old_protect, &old_protect);
VirtualFree(executable, 7680, MEM_RELEASE);
}
return 0;
}
But I had an Access violation error at line of "f();". I could not see the problem, so that could not solve it.
User contributions licensed under CC BY-SA 3.0