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# A little help with conversions

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

Joined: 25 Jul 2013
Posts: 4

PostPosted: Mon Jul 08, 2019 5:46 pm    Post subject: C# A little help with conversions Reply with quote

I've been able to get my pointer through 5 offsets in cheat engine and this works great, however it's a 64bit application and I'm running into some strange results when attempting to ReadProcessMemory in C#

When I step through it, the first ReadProcessMemory call sets BaseValue to -968473392. Where I expect it to be the address 1FBC64644D0 as it is in CE. Any help is appreciated.


Code:

namespace ConsoleApp1
{
    public class MemoryManager
    {
        const int PROCESS_WM_READ = 0x0010;

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

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

        public MemoryManager()
        {

            Process process = Process.GetProcessesByName("XXXXXX_x64").FirstOrDefault();
            IntPtr processHandle = OpenProcess(PROCESS_WM_READ, false, process.Id);

            var bytesRead = 0;
            byte[] buffer = new byte[4];


            Int64 pointer = IntPtr.Add(process.Modules[0].BaseAddress, 0x01DE8B78).ToInt64();
            ReadProcessMemory((int)processHandle, pointer, buffer, buffer.Length, ref bytesRead);
            Int64 baseValue = BitConverter.ToInt32(buffer, 0); // This is a negative integrer instead of "1FBC64644D0"
           
            Int64 firstAddress = baseValue + 0x18;
            ReadProcessMemory((int)processHandle, firstAddress, buffer, buffer.Length, ref bytesRead);
            Int64 firstValue = BitConverter.ToInt32(buffer, 0);





CE_Pointers.png
 Description:
 Filesize:  15.48 KB
 Viewed:  507 Time(s)

CE_Pointers.png


Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 148

Joined: 25 Jan 2006
Posts: 7688
Location: 127.0.0.1

PostPosted: Tue Jul 09, 2019 2:37 am    Post subject: Reply with quote

BitConverter.ToInt32 is wrong for this instance since you are working with 64bit values. You need to convert to 64bit values, not 32bit. Use ToInt64/ToUInt64 instead.
_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
nezoic
How do I cheat?
Reputation: 0

Joined: 25 Jul 2013
Posts: 4

PostPosted: Tue Jul 09, 2019 9:25 am    Post subject: Reply with quote

atom0s wrote:
BitConverter.ToInt32 is wrong for this instance since you are working with 64bit values. You need to convert to 64bit values, not 32bit. Use ToInt64/ToUInt64 instead.



Thanks, I also needed to change the byte[4] to byte[8], and leave the dwSize at 4.
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 368

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

PostPosted: Tue Jul 09, 2019 10:26 am    Post subject: Reply with quote

also lpNumberOfBytesRead is 64 bit in 64-bit processes
_________________
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
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