对于我的一个项目,我需要列出一个流程的所有页面。
所以这是我制作的代码:
private List<MEMORY_BASIC_INFORMATION> GetMemoryInfo()
{
var output = new List<MEMORY_BASIC_INFORMATION>();
var addy = new IntPtr();
while (true)
{
MEMORY_BASIC_INFORMATION memInfo = new MEMORY_BASIC_INFORMATION();
int memDump = VirtualQueryEx(handle, addy, out memInfo, Marshal.SizeOf(memInfo));
if (memDump == 0)
{
var err = Marshal.GetLastWin32Error();
throw new Exception("GetMemoryInfo failed : GetLastError() : " + new Win32Exception(err).Message);
}
if ((memInfo.State & 0x1000) != 0 && (memInfo.Protect & 0x100) == 0)
output.Add(memInfo);
addy = new IntPtr(memInfo.BaseAddress.ToInt32() + memInfo.RegionSize);
}
return output;
}
问题出在执行上,VirtualQueryEx 返回 0。当我调用 GetLastError 时,它也返回零。我的代码中的问题在哪里?
编辑:这是 MEMORY_BASIC_INFORMATION 和 VirtualQueryEx() 的声明:
[StructLayout(LayoutKind.Sequential)]
protected struct MEMORY_BASIC_INFORMATION
{
public IntPtr BaseAddress;
public IntPtr AllocationBase;
public uint AllocationProtect;
public uint RegionSize;
public uint State;
public uint Protect;
public uint Type;
}
[DllImport("kernel32.dll")]
protected static extern int VirtualQueryEx(IntPtr hProcess, IntPtr lpAddress, out MEMORY_BASIC_INFORMATION lpBuffer, int dwLength);