VirtualQueryEx返回零,没有GetLastError()

时间:2013-06-27 19:47:17

标签: c# winapi getlasterror

对于我的一个项目,我需要列出流程的所有页面。

所以这是我制作的代码:

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);

0 个答案:

没有答案