Log in

View Full Version : Howto determine allocated memory ranges of a process?


laola
November 11th, 2005, 12:45
Hi there,

I admit it sounds a bit stupid, but how do I determine which ranges in the address space of a process are actually used? (Read: mapped memory ranges that won't cause exception when trying to access them.)
I dug a bit in the MSDN and found VirtualQueryEx, however the required algorithm to wade through memory seems terribly lame to me (according to the documentation, the return result is rounded down to the next page border, so you have to scan each following page for the remainder of the allocated block...).

There has to be a more convenient way to find out about this memory map, but it seems I can't see the wood for the trees.

I don't need complete solutions, just a gentle push into the right direction will do

Thanks a lot in advance!

0rp
November 11th, 2005, 14:58
Code:
SYSTEM_INFO systeminfo;

GetSystemInfo(&systeminfo);

BYTE *mem = NULL;
MEMORY_BASIC_INFORMATION memoryinformation;

while(mem < systeminfo.lpMaximumApplicationAddress)
{
memset(&memoryinformation, 0, sizeof(memoryinformation));
VirtualQueryEx(GetCurrentProcess(), mem, &memoryinformation, sizeof(memoryinformation));

printf("base=%08X, size=%08X\n", (BYTE *)memoryinformation.BaseAddress, memoryinformation.RegionSize);

mem = (BYTE *)memoryinformation.BaseAddress + memoryinformation.RegionSize;
}