gpt4 book ai didi

c++ - 枚举内存范围内的页面

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:55:14 26 4
gpt4 key购买 nike

我在 Visual Studio 2012 下的 Windows 上使用 C++。

我有一个开始和结束内存地址,需要生成一个页面基地址/两个地址之间的句柄列表。我考虑过手动探测页面的可能性,但认为必须有更好的方法来枚举它们。

有这种方式吗?

免责声明; 最终页面句柄必须是基地址,以便在将窗口页面的大小添加到它们时,生成的地址不会重叠到下一页。首选解决方案不是特定于平台的,并且与带/不带 WOW64 的 32 位兼容。

最佳答案

VirtualQuery几乎是您唯一的选择。它应该相当有效:

The function determines the attributes of the first page in the region and then scans subsequent pages until it scans the entire range of pages or until it encounters a page with a nonmatching set of attributes.

因此,您首先会在您关心的范围的开头调用它,然后您会返回单个页面 block 。下一个调用将在该 block 之后立即开始,然后是下一个调用,依此类推。

这是一个完全未经测试的函数,它将填充 MEMORY_BASIC_INFORMATION 的数组结构:

int EnumVirtualAllocations(const void* ptr, size_t length, MEMORY_BASIC_INFORMATION* info, int size)
{
const void* end = (const void*)((const char*)ptr + length);
int index = 0;
while (index < size && ptr < end &&
VirtualQuery(ptr, &info[index], sizeof(*info)) == sizeof(*info))
{
MEMORY_BASIC_INFORMATION* i = &info[index];
if (i->State != MEM_FREE) index++;
ptr = (const void*)((const char*)(i->BaseAddress) + i->RegionSize);
}
return index;
}

关于c++ - 枚举内存范围内的页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15018376/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com