public static IEnumerable<MemoryBasicInformation> Query(SafeMemoryHandle processHandle, IntPtr addressFrom, IntPtr addressTo)
{
// Check if the handle is valid
HandleManipulator.ValidateAsArgument(processHandle, "processHandle");
// Convert the addresses to Int64
var numberFrom = addressFrom.ToInt64();
var numberTo = addressTo.ToInt64();
// The first address must be lower than the second
if (numberFrom >= numberTo)
throw new ArgumentException("The starting address must be lower than the ending address.", "addressFrom");
// Create the variable storing the result of the call of VirtualQueryEx
int ret;
// Enumerate the memory pages
do
{
// Allocate the structure to store information of memory
MemoryBasicInformation memoryInfo;
// Get the next memory page
ret = NativeMethods.VirtualQueryEx(processHandle, new IntPtr(numberFrom), out memoryInfo, MarshalType<MemoryBasicInformation>.Size);
// Increment the starting address with the size of the page
numberFrom += memoryInfo.RegionSize;
// Return the memory page
if(memoryInfo.State != MemoryStateFlags.Free)
yield return memoryInfo;
} while (numberFrom < numberTo && ret != 0);
}