无法从地址打开映射文件

时间:2019-08-09 21:39:28

标签: c++ mapped-memory

我无法打开映射的内存。当我使用OpenFileMappingA()时,它返回NULL,而GetLastError()返回161(ERROR_BAD_PATHNAME)。我使用以下代码:

MEMORY_BASIC_INFORMATION mbi = { 0 };
VirtualQuery(image->raw_data, &mbi, sizeof(mbi));

if (mbi.Protect & PAGE_EXECUTE_READWRITE)
    ZeroMemory(image->raw_data, sizeof(IMAGE_DOS_HEADER) +  sizeof(IMAGE_NT_HEADERS)  + sizeof(IMAGE_SECTION_HEADER)* nt_header->FileHeader.NumberOfSections);
else if (mbi.Type & MEM_MAPPED)
{
    char buffer[500];
    size_t count = GetMappedFileNameA(GetCurrentProcess(), image->raw_data, buffer, sizeof(buffer));    
    HANDLE hMap = OpenFileMappingA(FILE_MAP_ALL_ACCESS, false, buffer);
    std::cout << hMap << std::endl; //0x00000000 
    std::cout << GetLastError() << std::endl; //161
}

1 个答案:

答案 0 :(得分:0)

“我想在此位置编辑内存,但是它具有PAGE_READONLY保护”

OpenFileMappingA与保护无关。

您需要使用VirtualProtect()将该存储区的保护更改为PAGE_READWRITE (0x04)

DWORD  curProtection = 0;
VirtualProtect(addr, len, PAGE_READWRITE, &curProtection);

//change the memory

//then restore old protection
VirtualProtect(toHook, len, curProtection, &curProtection);