Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[C#] Getting multiple pointers, can this be any better?

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
NullBy7e
Cheater
Reputation: 0

Joined: 23 Jul 2014
Posts: 47

PostPosted: Wed Jul 23, 2014 5:17 pm    Post subject: [C#] Getting multiple pointers, can this be any better? Reply with quote

This is how I get the current Soul Memory in Dark Souls II.
I am fairly new to the memory hacking expect and would like to know if theres a more efficient way to write the below code.


Code:
byte[] ptr1 = ProcessMemoryReaderApi.ReadMemory(process, (IntPtr)(process.MainModule.BaseAddress + 0x010226C0), 4, out bytesRead);
                    IntPtr addr1 = (IntPtr)(BitConverter.ToInt32(ptr1, 0) + 0x80);
                    byte[] ptr2 = ProcessMemoryReaderApi.ReadMemory(process, addr1, 4, out bytesRead);
                    IntPtr addr2 = (IntPtr)(BitConverter.ToInt32(ptr2, 0) + 0x180);
                    byte[] ptr3 = ProcessMemoryReaderApi.ReadMemory(process, addr2, 4, out bytesRead);
                    int soul_memory = BitConverter.ToInt32(ptr3, 0);
Back to top
View user's profile Send private message
661089799107
Expert Cheater
Reputation: 3

Joined: 25 Jan 2009
Posts: 186

PostPosted: Wed Jul 23, 2014 9:19 pm    Post subject: Reply with quote

You could write a function that takes your offsets and loops over them.

I'm not familiar with C#, but I'm assuming you can reuse a single byte array and 'IntPtr' without creating them for every call.
Back to top
View user's profile Send private message
Matze500
Expert Cheater
Reputation: 8

Joined: 25 Jan 2012
Posts: 241
Location: Germany

PostPosted: Wed Jul 23, 2014 9:35 pm    Post subject: Reply with quote

Here an example for a function in C#.That is the one I use.

Code:
public void ReadProcessPointerMemory(IntPtr baseaddress, IntPtr[] Offsets, MemoryByteFlags sv_Type, out IntPtr finalAddress, out byte[] finalValue)
        {

            int offsetCount = Offsets.Length;
            IntPtr ptrRead = baseaddress;
            IntPtr tmp_Address = IntPtr.Zero;
            byte[] read = new byte[(uint)sv_Type];

            for (int i = 0; i < offsetCount; i++)
            {
                tmp_Address = IntPtr.Add(ptrRead, Offsets[i].ToInt32());

                int bytesRead = 0;
                read = ReadProcessMemory(tmp_Address, (uint)sv_Type, out bytesRead);

                ptrRead = new IntPtr(BitConverter.ToInt32(read, 0));
            }

            finalAddress = tmp_Address;
            finalValue = read;
        }

public enum MemoryByteFlags : uint
        {
            Byte = 0x00000001,
            Char = 0x00000001,
            Wchar = 0x00000002,
            Short = 0x00000002,
            Ushort = 0x00000002,
            Word = 0x00000002,
            Int = 0x00000004,
            Long = 0x00000004,
            Bool = 0x00000004,
            Uint = 0x00000004,
            Ulong = 0x00000004,
            Dword = 0x00000004,
            Int64 = 0x00000008,
            Uint64 = 0x00000008,
            Hwd = 0x00000004,
            Handle = 0x00000004,
            Float = 0x00000004,
            Double = 0x00000008,
            Boolean = 0x00000001
        }


Greets Matze
Back to top
View user's profile Send private message
NullBy7e
Cheater
Reputation: 0

Joined: 23 Jul 2014
Posts: 47

PostPosted: Thu Jul 24, 2014 1:29 am    Post subject: Reply with quote

Thanks alot!
If I understand correctly, I can follow the entire pointer chain to the final address by calling that function once?

I tried declaring the ReadMemory function but upon debug it gives a marshal directive error.

Code:
 [DllImport("kernel32.dll", SetLastError = true)]
        static extern byte[] ReadProcessMemory(IntPtr lpBaseAddress, uint sv_flags, out int lpNumberOfBytesRead);


EDIT:

Fixed by changing the ReadProcessMemory in your function and by passing Process for hProc as first argument.

Code:
 IntPtr[] sm_offsets = { (IntPtr)0x010226C0, (IntPtr)0x80, (IntPtr)0x180 };
                    IntPtr sm_addr;
                    byte[] sm_data;
                    api.ReadProcessPointerMemory(process, process.MainModule.BaseAddress, sm_offsets, ProcessMemoryReaderApi.MemoryByteFlags.Int, out sm_addr, out sm_data);
                    int soul_memory = BitConverter.ToInt32(sm_data, 0);
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites