public static List <MEMORY_BASIC_INFORMATION> GetMemoryMap(Process appProcess) { // Get the SYSTEM_INFO to determine the maximum possible // memory for the app. SYSTEM_INFO si = new SYSTEM_INFO(); GetSystemInfo(ref si); IntPtr hReadProcHandle = IntPtr.Zero; IntPtr hToken = IntPtr.Zero; List <MEMORY_BASIC_INFORMATION> memoryPages = new List <MEMORY_BASIC_INFORMATION>(); try { // Open the Process Handle and set it for DEBUG/READ access hReadProcHandle = OpenProcessForDebug(appProcess, out hToken); uint startMem = 0x0; // Loop through until you get to the end of the application // memory while (startMem < si.lpMaximumApplicationAddress) { // Determine the Page info MEMORY_BASIC_INFORMATION mbi = new MEMORY_BASIC_INFORMATION(); int size = VirtualQueryEx(appProcess.Handle, (IntPtr)startMem, ref mbi, Marshal.SizeOf(typeof(MEMORY_BASIC_INFORMATION))); memoryPages.Add(mbi); // Go to next page startMem = (UInt32)mbi.BaseAddress + mbi.RegionSize; } } catch (Exception ex) { Console.WriteLine("ERROR:{0}{1}{0}MORE INFO:{0}{2}", Environment.NewLine, ex.Message, ex); } finally { if (hToken != IntPtr.Zero) { CloseHandle(hToken); } if (hReadProcHandle != IntPtr.Zero) { CloseHandle(hReadProcHandle); } } return(memoryPages); }
public void Add(MEMORY_BASIC_INFORMATION mbi) { RegionSize += mbi.RegionSize; }
static extern Int32 VirtualQueryEx(IntPtr hProcess, IntPtr lpAddress, ref MEMORY_BASIC_INFORMATION buffer, Int32 dwLength);
public MemoryPage(MEMORY_BASIC_INFORMATION mbi) { BaseAddress = mbi.BaseAddress; RegionSize = mbi.RegionSize; }