Binarysharp.MemoryManagement.Memory.MemoryCore.Query C# (CSharp) Method

Query() public static method

Retrieves information about a range of pages within the virtual address space of a specified process.
public static Query ( SafeMemoryHandle processHandle, IntPtr addressFrom, IntPtr addressTo ) : IEnumerable
processHandle Binarysharp.MemoryManagement.Native.SafeMemoryHandle A handle to the process whose memory information is queried.
addressFrom System.IntPtr A pointer to the starting address of the region of pages to be queried.
addressTo System.IntPtr A pointer to the ending address of the region of pages to be queried.
return IEnumerable
        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);
        }

Same methods

MemoryCore::Query ( SafeMemoryHandle processHandle, IntPtr baseAddress ) : MemoryBasicInformation