Fixing "0xC0000005: Access violation reading location" error whilst browsing a memory region and storing bytes into a vector

-1

There's a memory region I would like to read in bytes. To accomplish that I have two variables. One called hModule that holds the start address and variable moduleSize that is hModule + the range.

To store the bytes I use std::vector: std::vector<BYTE> rawData(reinterpret_cast<BYTE*>(hModule), reinterpret_cast<BYTE*>(moduleSize)); Compiling and running the program gives me: 0xC0000005: Access violation reading location 0x00007FFF1A93A000. I made sure moduleSize is within the bounds of the memory region so that the selected memory region is not overlapping different modules.

I have used VirtualProtect to change the permissions of the selected memory region so that I can actually read and execute the code i browse through.

The end code looks like this:

DWORD oldProtection;
uintptr_t result = VirtualProtect(hModule, 0x100000, PAGE_EXECUTE_READ, &oldProtection);
if (result == 0)
    result = GetLastError();
std::vector<BYTE> rawData(reinterpret_cast<BYTE*>(hModule), reinterpret_cast<BYTE*>(moduleSize));
VirtualProtect(hModule, 0x100000, oldProtection, NULL);

This still gives the same error which leads me to ask a solution here that I might have been missing.

c++
asked on Stack Overflow Sep 27, 2019 by Richard

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0