[C#] Multi level pointers

10/13/2013 21:29 davydavekk#1
Hello epvps,

I'm writing a class for memory editing, but I'm stuck with multi lvl pointers.

What I have so far :
Code:
//API Declaration :

  [DllImport("kernel32.dll")]
    static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAdress,  byte[] lpBuffer, uint iSize,out uint lpNumberOfBytesRead);

    [DllImport("kernel32.dll")] // Overload for ptr
    static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAdress, out IntPtr lpBuffer, uint iSize, out uint lpNumberOfBytesRead);

//........

     public int Readlvl1Pointer(IntPtr adress)
    {
        uint bytesRead;
        byte[] _Value = new byte[4];

        ReadProcessMemory(_targetProcessHandle, adress, _Value, (uint)IntPtr.Size, out bytesRead);
        int ptrVal = BitConverter.ToInt32(_Value, 0);
        IntPtr ptr = (IntPtr)ptrVal;
        ReadProcessMemory(_targetProcessHandle, adress, _Value, (uint)IntPtr.Size, out bytesRead);

        return BitConverter.ToInt32(_Value, 0); // This function is working fine
    }

    private IntPtr ReadPointer(IntPtr adress)
    {
        IntPtr tempPTR;
        uint NumberOfBytesRead;

        ReadProcessMemory(_targetProcessHandle, adress, out tempPTR, (uint)IntPtr.Size, out NumberOfBytesRead);
        return tempPTR;
    }

    public IntPtr FollowPointer(IntPtr adress, ulong[] Offsets) // To get the final adress
    {
        IntPtr ptr = ReadPointer(adress);
        for (int i = 0; i < Offsets.Length; i++)
        {
            ptr = ReadPointer(new IntPtr(ptr + Offsets[i])); // error here, can't add IntPtr to IntPtr
        }
        return ptr;

    }
And I want to use like that (outside the class) :


Code:
IntPtr finalAdress = m.FollowPointer((IntPtr)0x0631390, {0xC,0x14,0});  // Don't know why I always have to convert here
m.WriteInteger(finalAdress,[new value here]);
Any help would be appreciated
10/13/2013 22:18 Easy-Emu#2
just use ulong[] Offsets or uint
and ptr = ReadPointer(new IntPtr(ptr + Offsets[i]))
10/13/2013 22:24 Shawak#3
You are very mixed up with the data types there, you should try to fix that first.
10/13/2013 22:47 davydavekk#4
@Shawak : Updated for that.

@Easy-Emu : Still the same error:
Quote:
Operator '+' cannot be applied to operands of type 'System.IntPtr' and 'ulong'
10/14/2013 01:12 Easy-Emu#5
ok then use
ReadPointer(new IntPtr((uint)ptr + (uint)Offsets[i]))

and uint[] Offsets
10/14/2013 11:01 KDeluxe#6
[Only registered and activated users can see links. Click Here To Register...]
10/14/2013 18:40 davydavekk#7
Thanks Easy-Emu, it worked.

But now I have a problem with my others functions (I think it's because of the messed up data types, as Shawak said)

When I do :
Code:
Console.WriteLine(m.ReadInteger((IntPtr)0x7F758));
It returns a certain value, which is not the same as the value showed in CE (but it still the exact same adress).
And it change only when I use my WriteInteger function.

I missed something, but I don't know where.

My whole class :

Thanks in advance.
10/15/2013 09:56 Easy-Emu#8
whats about baseAddress?^^
maybe you should make base+address
10/16/2013 02:09 davydavekk#9
Finally got it working, here's how I did :

Thank y'all.

#close request
10/19/2013 03:53 KDeluxe#10
A pointer can be larger than 4 bytes. You should use IntPtr instead of int.