0

我在读取进程的内存时遇到了一些问题,我得到的只是错误 299,有时是错误 5/6,我是内存读/写的新手,可以使用任何帮助。

这是我到目前为止所拥有的:

    private void ScanMemory()
    {
        uint PID;
        GetWindowThreadProcessId(GetWindowHandle(), out PID);
        label3.Text = "" + (int)PID;
        int valueToSearch = 4;
        List<int> matchAddresses = new List<int>();
        long MaxAddress = 0x7fffffff;
        long address = 0;
        do
        {
            MEMORY_BASIC_INFORMATION32 m;
            IntPtr Handle = OpenProcess((int)ProcessAccessFlags.All, false, (int)PID);
            uint result = VirtualQueryEx((int)Handle, (int)address, out m, (int)Marshal.SizeOf(typeof(MEMORY_BASIC_INFORMATION32)));
            if (address == (long)m.BaseAddress + (long)m.RegionSize)
                break;
            address = (long)m.BaseAddress + (long)m.RegionSize;
            SearchIntSizeValue(Handle, (int)m.BaseAddress, (int)m.RegionSize, valueToSearch, matchAddresses);
        } while (address <= MaxAddress);

        foreach (int res in matchAddresses)
        {
            listBox1.Items.Add(res);
        }
    }
    public void SearchIntSizeValue(IntPtr hProcess, int startAddress, int endAddress, int valueToSearch, List<int> addresses)
    {
        byte[] buffer = new byte[4];
        IntPtr bytesread;
        while (startAddress < endAddress)
        {
            ReadProcessMemory(hProcess, (IntPtr)startAddress, buffer, 4, out bytesread);
            int res = BitConverter.ToInt32(buffer, 0);
            if (res == valueToSearch)
            {
                addresses.Add(startAddress);
            }
            else
            {
                int le;
                if ((le = Marshal.GetLastWin32Error()) != 0)
                {

                }
            }
            startAddress += 4;
        }
    }

[StructLayout(LayoutKind.Sequential)]
 struct MEMORY_BASIC_INFORMATION32
{
    public uint BaseAddress;
    public uint AllocationBase;
    public int AllocationProtect;
    public uint RegionSize;
    public int State;
    public int Protect;
    public int lType;
}

我目前正在读取 int 的大小,并没有真正工作:|,从谷歌中阅读,我看到有这个东西有特权,不过,我在 Windows 7 上以管理员权限运行 vs2010,我希望它对你来说足够信息发现我的问题,提前谢谢!

4

1 回答 1

0

我帮助解决了www.codeproject.com上的问题: Solving-Problems-of-Monitoring-Standard-Output

解决方案保护应用程序免受 Microsoft 在其文档中未提及的其他死锁。

于 2015-06-12T13:59:11.427 回答