从进程内存中读取int

时间:2013-08-07 20:17:25

标签: c# memory process offset

我正在使用基地址和一些偏移量从内存中读取数据:

public static int ReadInt(long address)
{
    byte[] buffer = new byte[sizeof(int)];
    ReadProcessMemory(pHandle, (UIntPtr)address, buffer, (UIntPtr)4,
                      IntPtr.Zero);
    return BitConverter.ToInt32(buffer, 0);
}

我添加了这样的补偿:

var one = MemoryHandler.ReadInt((long)MemoryHandler.base_adress + 
                                (long)0x0945BB0C);
var two = MemoryHandler.ReadInt(one + (long)0x28);
var three = MemoryHandler.ReadInt(two + (long)0x214);
var four = MemoryHandler.ReadInt(three + (long)0x38);
var five = MemoryHandler.ReadInt(four + (long)0x7EC);
var six = MemoryHandler.ReadInt(five + (long)0x230);

six包含我需要的值。

我试图做一个做同样事情的重载。我的问题是它没有给我相同的价值。我想知道原因:

public static int ReadInt(long address, int[] offsets)
{
    long prev = 0;
    for (int i = 0; i < offsets.Length; i++)
    {
        address = prev > 0 ? ReadInt(prev + (long)offsets[i]) : ReadInt(address);
        prev = address + offsets[i];
    }

    return (int)address;       
}

var offsets = new int[] { 0x28, 0x214, 0x38, 0x7EC, 0x230 };
var result = MemoryHandler.ReadInt((long)MemoryHandler.base_adress + 
                                 (long)0x0945BB0C, offsets);

只是为了澄清:我希望result与上面的six具有相同的价值。

1 个答案:

答案 0 :(得分:2)

请注意,这只适用于32位

public static int ReadInt(long address, int[] offsets)
{
    address = ReadInt(address);

    for (int i = 0; i < offsets.Length; i++)
    {
        address = ReadInt(address + (long)offsets[i]);
    }

    return (int)address;       
}

您有offsets.Length + 1 ReadInt(s)要做,一个没有偏移,offsets.Length有偏移。每个人都返回下一个地址。最后一个返回一个值。

考虑到这只能在32位上工作(因为你正在读32位并将它们用作ptr),使用long是没用的。 int就足够了。