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 


Help me fix my code. (c# and Pointers)

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

Joined: 21 Mar 2016
Posts: 32
Location: Germany

PostPosted: Mon Feb 13, 2017 1:50 pm    Post subject: Help me fix my code. (c# and Pointers) Reply with quote

Something with ReadProcessMemory() is wrong and i don't know what it is. Sad

Code:
class Memory
    {
        const int PROCESS_ALL_ACCESS = 0x1f0fff;

        [DllImport("kernel32.dll")]
        public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);

        [DllImport("kernel32.dll")]
        public static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, int nSize, ref int lpNumberOfBytesRead);

        [DllImport("kernel32.dll")]
        public static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseaddress, byte[] lpBuffer, int nSize, ref int lpNumberOfBytesWritten);

        Process p = new Process();
        IntPtr processHandel;
        IntPtr baseAddress;

        public Memory(Process process)
        {
            p = process;
            processHandel = OpenProcess(PROCESS_ALL_ACCESS, false, p.Id);
            baseAddress = p.MainModule.BaseAddress;
        }

        public IntPtr ReadPointer(int ceAddress, int[] offsets)
        {
            IntPtr pointer;
            int bytesRead = 0;
            byte[] buffer = new byte[8];

            pointer = IntPtr.Add(baseAddress, ceAddress);

            foreach (int offset in offsets)
            {
                ReadProcessMemory(p.Handle, IntPtr.Add(pointer, offset), buffer, buffer.Length, ref bytesRead); //buffer always returns 0
                pointer = new IntPtr(BitConverter.ToInt64(buffer, 0));
            }

            return pointer;
        }
   }


Code:
int[] offsets = { 0x350, 0x38, 0x10, 0x68 };
IntPtr cool = mem.ReadPointer(0xDE5178, offsets);
Back to top
View user's profile Send private message
Matze500
Expert Cheater
Reputation: 8

Joined: 25 Jan 2012
Posts: 241
Location: Germany

PostPosted: Mon Feb 13, 2017 5:06 pm    Post subject: Reply with quote

1. What value type do you want to read?
2. You have to read the process memory one time before the loop or you have to add a 0x0 to your offsets.
3. I would use the processHandle returned by OpenProcess instead of p.Handle in the read call.
4. Is your application compiled in 64bit if you try to access a 64bit process? There is a checkbox in the application setting which need s to be unchecked (prefere 32bit code)

_________________
Back to top
View user's profile Send private message
Zanzer
I post too much
Reputation: 126

Joined: 09 Jun 2013
Posts: 3278

PostPosted: Mon Feb 13, 2017 8:52 pm    Post subject: Reply with quote

Does pointer equal what you are expecting before the loop?
Are your offsets in the correct order? [[[[game.exe+DE5178]+350]+38]+10]+68
Back to top
View user's profile Send private message
Artykalamata
Cheater
Reputation: 0

Joined: 21 Mar 2016
Posts: 32
Location: Germany

PostPosted: Tue Feb 14, 2017 6:51 am    Post subject: Reply with quote

Matze500 wrote:
2. You have to read the process memory one time before the loop or you have to add a 0x0 to your offsets.


Sometimes i feel retarded. Shocked
Thank you!
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