0xC0000005: Access violation writing location 0x00920000

-2

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.

c++
asked on Stack Overflow Oct 20, 2020 by kaiken

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0