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 


Reading memory chunks in C#

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

Joined: 03 Mar 2020
Posts: 7

PostPosted: Thu Apr 01, 2021 7:16 am    Post subject: Reading memory chunks in C# Reply with quote

Code:

List<byte[]> dumps = new List<byte[]>();
long max = (long)sys.maximumApplicationAddress;
long address = (long)sys.minimumApplicationAddress;

Natives.MEMORY_BASIC_INFORMATION mem;
while (address < max &&
         Natives.VirtualQueryEx(Memory.Process.Handle, (IntPtr)address,
         out mem, (uint)Marshal.SizeOf(typeof(Natives.MEMORY_BASIC_INFORMATION))) != 0)
            {
                if (mem.Protect == Natives.AllocationProtect.PAGE_READWRITE &&
                    mem.State == Natives.StateEnum.MEM_COMMIT)
                {
                    byte[] buffer = new byte[(int)mem.RegionSize];
                    IntPtr br;
                    if (Natives.ReadProcessMemory(Memory.ProcessHandle, (ulong)address,
                        buffer, (int)mem.RegionSize, out br) && br != IntPtr.Zero)
                    {
                        dumps.Add(buffer);
                    }
                }
                address = (long)mem.BaseAddress + (long)mem.RegionSize;
            }
            return dumps.ToArray(); //byte[][]


I'm trying to search for a value inside the process memory, for better performance I'm reading chunks of memory. How can I get the values inside this chunks ?
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 198

Joined: 25 Jan 2006
Posts: 8516
Location: 127.0.0.1

PostPosted: Tue Jun 08, 2021 2:49 pm    Post subject: Reply with quote

Quote:
I'm trying to search for a value inside the process memory, for better performance I'm reading chunks of memory. How can I get the values inside this chunks ?


You would need to then look through the chunks bytes to find what value(s) you are actually looking for. However, with what you showed, there is no real means for you to tell where a set of data came from to know the actual address of the data afterward when looking through it.

Two options would be:

1. Remove the List<> altogether and instead do the additional scanning step per-chunk dumped where you currently have the 'dumps.Add(buffer)'. This way you still have access to the current 'mem.BaseAddress' information and can properly determine the full address of each found match you are looking for within the dumps.

2. Change the container you are dumping to from a List<>, or change the type being stored from just a 'byte[]' to a key/value pair or similar so that you can store the 'mem.BaseAddress' with each dumped chunk to know where that chunk started from.

I would also suggest not doing things like your last line of converting the list to an array, it's extra overhead that isn't needed. Just use the List<> object as it is, or change to a different type to begin with. Converting [potentially] huge objects like that can come with a heavy overhead cost on memory and performance that isn't needed and can just be avoided altogether. Any additional operations needed to be done on the dumped chunks in that List<> can be done directly to the List<> object itself.

_________________
- 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