Code:
public static Util util;
[DllImport("kernel32.dll")]
public static extern Int32 ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,
[In, Out] IntPtr buffer, UInt32 size, out int lpNumberOfBytesRead);
public static Structures.BattleListEntry GetBattleListEntry(int index)
{
int numberOfBytesRead;
Type type = typeof(Structures.BattleListEntry);
int size = Marshal.SizeOf(type);
IntPtr BytesRead = Marshal.AllocHGlobal(size);
ReadProcessMemory(util._Handle, (IntPtr)(BLAddress.Start + util._Base), BytesRead, (uint)size, out numberOfBytesRead);
Structures.BattleListEntry ble = (Structures.BattleListEntry)Marshal.PtrToStructure(BytesRead, type);
Marshal.FreeHGlobal(BytesRead);
return ble;
}
So what I'm doing is initialising my util class:
Code:
public class Util
{
public Process _Tibia;
public UInt32 _Base;
public IntPtr _Handle;
public Util(Process Tibia)
{
_Tibia = Tibia;
_Base = (UInt32)Tibia.MainModule.BaseAddress.ToInt32();
_Handle = Tibia.MainWindowHandle;
}
}
With the first client it can find. The struct currently looks like this:
Code:
public struct BattleListEntry
{
public UInt32 cid; // 4 bytes
public string name; // 32 bytes
public byte[] unknown; // 16 unknown bytes
public UInt32 z; // 4 bytes z
public UInt32 y; // 4 bytes y
public UInt32 x; // 4 bytes x
public byte[] unknown2; // 8 bytes unknown
public UInt32 direction; // Currently facing (0 north, 1 east, 2 south, 3 west)
public UInt32 timeLastMovement; // The actual time you last moved
public UInt32 rnd3; // Repeat of previous?
public UInt32 rnd4; // 4 more bytes of random, appears to be primarily 0-32
public UInt32 rnd5; // as previous
public UInt32 rnd6; // always 900 on npc?
public UInt32 isWalking; // Is walking value
public byte[] unknown1; // 12 bytes of unknown shit, last one is quite a long number (2147483748)
public UInt32 Outfit1; // 4 byte outfit type, 129 for npc
public UInt32 Outfit2; // 4 byte outfit type, 19 for npc
public UInt32 Outfit3; // 4 byte outfit type, 113 for npc
public UInt32 Outfit4; // 4 byte outfit type, 95 for npc
public UInt32 Outfit5; // 4 byte outfit type, 115 for NPC
public byte[] unknown3; // 24 bytes of empty values on npc...
public UInt32 hppc; // 4 byte hppc (100 for npc and self in test)
public UInt32 rnd7; // 4 bytes, 45 on npc...
public UInt32 rnd8; // 4 bytes, 1 on npc...
public byte[] unknown4; // 12 bytes unknown values
public UInt32 rnd9; // 4 bytes, 1 on npc
public UInt32 rnd10; // 4 byte value, 357 for npc
public UInt32 rnd11; // 4 byte value 0
public UInt32 rnd12; // 4 byte value 153
public UInt32 rnd13; // 4 byte value 0
public UInt32 rnd14; // 4 byte value 0
public UInt32 rnd15; // 4 byte value 0
public UInt32 rnd16; // 4 byte value 2
}
It's throwing an access violation on this line:
Code:
Structures.BattleListEntry ble = (Structures.BattleListEntry)Marshal.PtrToStructure(BytesRead, type);
Saying I tried to write to or read protected memory... I presume that's because BytesRead is inaccessible from the thread which is running the unsafe code...?