0

假设我想检查我的win32用户模式应用程序的整个 4GB 地址空间。由于尝试直接从随机位置读取会引发access violation,我想先检查每个页面的可读性:

char* p = 0x00000000;
while (p < 0xf0000000 ) 
{
    MEMORY_BASIC_INFORMATION mbi;
    VirtualQuery ( p , & mbi , 100) ;
    if (!(  mbi.Protect | PAGE_NOACCESS ) )        
         char tmp = *p;
    p = p + PAGE_SIZE;


}

还有其他更有效的方法吗?

4

1 回答 1

0

您必须检查 state == MEM_COMMIT 和 protection != PAGE_NOACCESS 以避免无效的内存区域。此代码将列出每个有效内存区域的开始和结束地址:


HANDLE hProc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());

MEMORY_BASIC_INFORMATION mbi;

while (VirtualQuery(addr, &mbi, sizeof(mbi)))
{
    if (mbi.State == MEM_COMMIT && mbi.Protect != PAGE_NOACCESS)
    {
        std::cout << "base : 0x" << std::hex << mbi.BaseAddress << " end : 0x" << std::hex << (uintptr_t)mbi.BaseAddress + mbi.RegionSize << "\n";
    }
    addr += mbi.RegionSize;
}
于 2020-04-21T01:12:41.580 回答