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# ReadProcessMemory

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
vyrrt
How do I cheat?
Reputation: 0

Joined: 11 Aug 2013
Posts: 4

PostPosted: Thu Aug 15, 2013 3:50 pm    Post subject: C# ReadProcessMemory Reply with quote

Hi guys,

I am trying to access an address of a game and after 3 days of Googling and still hitting a brick wall I have decided to ask for help. From what I understand, I need to use ReadProcessMemory in order to get the base address.

My MemoryApi class is as follows:
Code:
class MemoryApi
    {
        [Flags]
        public enum ProcessAccessFlags : uint
        {
            All = 0x001F0FFF,
            Terminate = 0x00000001,
            CreateThread = 0x00000002,
            VMOperation = 0x00000008,
            VMRead = 0x00000010,
            VMWrite = 0x00000020,
            DupHandle = 0x00000040,
            SetInformation = 0x00000200,
            QueryInformation = 0x00000400,
            Synchronize = 0x00100000
        }

        [DllImport("kernel32.dll")]
        private static extern IntPtr OpenProcess(ProcessAccessFlags dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, int dwProcessId);

        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, uint nSize, out int lpNumberOfBytesWritten);

        [DllImport("kernel32.dll", SetLastError = true)]
        static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [Out] byte[] lpBuffer, int dwSize, out int lpNumberOfBytesRead);

        [DllImport("kernel32.dll")]
        public static extern Int32 CloseHandle(IntPtr hProcess);

        public static byte[] ReadMemory(Process process, int address, int numOfBytes, out int bytesRead)
        {
            var hProc = OpenProcess(ProcessAccessFlags.All, false, process.Id);

            var buffer = new byte[numOfBytes];

            ReadProcessMemory(hProc, new IntPtr(address), buffer, numOfBytes, out bytesRead);
            return buffer;
        }
    }


This is an example of the code that calls this method:

Code:
class CurrentGameTime
    {
        public static GameTime GetCurrentGameTime()
        {
            var gameTime = new GameTime();
            var gameProcess = BaseProcess.GetGameProcess();
            int bytesRead;
            byte[] test = MemoryApi.ReadMemory(gamrProcess, 0x193D0F4, 4, out bytesRead);
//TODO: Use result to instantiate gameTime
            return gameTime;
        }
    }


Whenever this runs, the byte array that is returned is 4 bytes in size with a value of 0. I am genuinely stuck and would appreciate any advice.

Incase it is needed, here is the code from my BaseProcess class:

Code:
public class BaseProcess
    {
        public static Process GetGameProcess()
        {
            try
            {
                var processes = Process.GetProcessesByName("GameProcessName");
                if (processes.Length > 0)
                {
                    return processes[0];
                }
                throw new Exception("Client was not found.");
            }
            catch
            {
                return null;
            }
        }
    }


Thanks

Vyrrt



ptr.png
 Description:
Screendump of CheatEngine 6.3
 Filesize:  24.93 KB
 Viewed:  15442 Time(s)

ptr.png


Back to top
View user's profile Send private message
TsTg
Master Cheater
Reputation: 5

Joined: 12 Dec 2012
Posts: 340
Location: Somewhere....

PostPosted: Thu Aug 15, 2013 4:14 pm    Post subject: Reply with quote

Use the pointer to the buffer instead:
Code:
ReadProcessMemory(hProc, new IntPtr(address), &buffer, numOfBytes, out bytesRead);


also make sure the read process went ok (check if ReadProcessMemory returns 0 if failed)


or in your ReadMemory function, return the address of the array instead
Back to top
View user's profile Send private message
vyrrt
How do I cheat?
Reputation: 0

Joined: 11 Aug 2013
Posts: 4

PostPosted: Fri Aug 16, 2013 12:35 am    Post subject: Reply with quote

Would that not require me to use unsafe?
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 471

Joined: 09 May 2003
Posts: 25823
Location: The netherlands

PostPosted: Fri Aug 16, 2013 3:30 am    Post subject: Reply with quote

You are reading the wrong address. It's modulebase+0x193D0F4, not 0x193D0F4,
_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
vyrrt
How do I cheat?
Reputation: 0

Joined: 11 Aug 2013
Posts: 4

PostPosted: Fri Aug 16, 2013 1:07 pm    Post subject: Reply with quote

Ah damn, that was it!

Many thanks.
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 205

Joined: 25 Jan 2006
Posts: 8587
Location: 127.0.0.1

PostPosted: Sat Aug 17, 2013 1:16 pm    Post subject: Reply with quote

vyrrt wrote:
Would that not require me to use unsafe?


Unsafe will only help you inside your own memory space (or if you are injected). Also there is basically never a reason to use unsafe in C# when everything you can do with it can be done with the Marshal class.

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
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