Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > .NET Languages
You last visited: Today at 21:53

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



[C#]Reading Memory / Pointer with multiple Offsets.

Discussion on [C#]Reading Memory / Pointer with multiple Offsets. within the .NET Languages forum part of the Coders Den category.

Reply
 
Old   #1
 
iCraziE's Avatar
 
elite*gold: 0
Join Date: Jan 2010
Posts: 456
Received Thanks: 218
[C#]Reading Memory / Pointer with multiple Offsets.

I am used to coding in AutoIt. I am trying to move my project into C# but I have only about 2-3 weeks of experience.

In AutoIt, I have this code here that reads the process memory and returns a value.

Code:
$map = _MemoryRead(0x00B5CCB8, $handle) // Pointer address 0x00B5CCB8
$map = _MemoryRead($map + 0x02, $handle) // Offset 0x02
$map = _MemoryRead($map + 0xBD, $handle) // Second Offset 0xBD
I am trying desperately to convert this to C# language and I need some help. Here is what I have so far.

Code:
Process process = Process.GetProcessesByName("My Process").FirstOrDefault();
            int address = 0x00B5CCB8;
            int offset1 = 0x02;
            int offset2 = 0xBD;
            int bytesRead;

            byte[] pointer = ProcessMemoryReaderApi.ReadMemory(process, address, 4, out bytesRead);
Using this works, but I don't know how to add the necessary offsets to the address. If someone could help me out with this one. That would be greatly appreciated.

Here is the class that I am using.

Code:
class ProcessMemoryReaderApi
    {
        // constants information can be found in <winnt.h> 
        [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)
        {
            IntPtr hProc = OpenProcess(ProcessAccessFlags.All, false, process.Id);
            byte[] buffer = new byte[numOfBytes];

            ReadProcessMemory(hProc, new IntPtr(address), buffer, numOfBytes, out bytesRead);
            return buffer;
        }
Thanks in advance.

PS. I would also like to have a message box that will show me the value that is returned when the address is read.
iCraziE is offline  
Old 01/08/2013, 05:08   #2
 
»jD«'s Avatar
 
elite*gold: 237
Join Date: Sep 2010
Posts: 1,152
Received Thanks: 4,910
Assuming you are using .NET 4 you can use the IntPtr Add() and Subtract() methods. For example:

Code:
IntPtr baseAddr = new IntPrt(0x00B5CCB8); 
IntPtr newAddr = IntPtr.Add(baseAddr, 0x02);
Hope that helps! Also, unless doing multiple reads on the same OpenProcess handle, you should probably call CloseHandle() after doing the Read in ReadMemory().

-jD
»jD« is offline  
Thanks
1 User
Old 01/08/2013, 05:44   #3
 
iCraziE's Avatar
 
elite*gold: 0
Join Date: Jan 2010
Posts: 456
Received Thanks: 218
Thanks for the reply!

I got an error but i fixed it.. Everything is working now, I think.. theres no way for me to see what the returned value is.

Is there anyway to show the value that was read at the address? in a messagebox perhaps?
iCraziE is offline  
Old 01/08/2013, 05:55   #4
 
»jD«'s Avatar
 
elite*gold: 237
Join Date: Sep 2010
Posts: 1,152
Received Thanks: 4,910
Include this overload method in the "ProcessMemoryReaderApi" class:

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

            ReadProcessMemory(hProc, address, buffer, numOfBytes, out bytesRead);
            return buffer;
        }
Quote:
Originally Posted by iCraziE View Post
I got an error but i fixed it.. Everything is working now, I think.. theres no way for me to see what the returned value is.

Is there anyway to show the value that was read at the address? in a messagebox perhaps?
Sure, the returned value will be a byte array, assuming it was an integer you read from the memory you can use:

Code:
byte[] valueOut = ProcessMemoryReaderApi.ReadMemory(process, address, 4, out bytesRead);
int value = BitConverter.ToInt32(valueOut, 0);
MessageBox.Show("Value of Integer: " + value.ToString());
-jD
»jD« is offline  
Thanks
1 User
Old 01/08/2013, 06:17   #5
 
iCraziE's Avatar
 
elite*gold: 0
Join Date: Jan 2010
Posts: 456
Received Thanks: 218
valueOfMem does not exist in current context.
should it be valueOut instead of valueOfMem?
iCraziE is offline  
Old 01/08/2013, 06:26   #6
 
»jD«'s Avatar
 
elite*gold: 237
Join Date: Sep 2010
Posts: 1,152
Received Thanks: 4,910
Yes it should sorry -.-

-jD
»jD« is offline  
Old 01/08/2013, 06:30   #7
 
iCraziE's Avatar
 
elite*gold: 0
Join Date: Jan 2010
Posts: 456
Received Thanks: 218
also this line to close the handle is saying invalid arguments,
cannot convert from system.diagnostic.process to intptr

ProcessMemoryReaderApi.CloseHandle(myProcess);
iCraziE is offline  
Old 01/08/2013, 06:32   #8
 
»jD«'s Avatar
 
elite*gold: 237
Join Date: Sep 2010
Posts: 1,152
Received Thanks: 4,910
you should use CloseHandle() inside the ReadMemory call with the 'hProc' variable.

For example:

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

            ReadProcessMemory(hProc, address, buffer, numOfBytes, out bytesRead);
            CloseHandle(hProc);
            return buffer;
        }
Just so I know, I'd be nice to see an output from this or letting me know if the output was what you expected

-jD
»jD« is offline  
Thanks
1 User
Old 01/08/2013, 06:36   #9
 
iCraziE's Avatar
 
elite*gold: 0
Join Date: Jan 2010
Posts: 456
Received Thanks: 218
I hope it will be ill let you know, you've been a GREAT help!!

btw, my applications always say this.. whenever i execute them and then close it.

and lets say i try to rebuild it, rename the exe or delete it, if the target process is still open.

Ill get an error and it says the application is still open in the process execute.

is there anyway to stop this. Like make it so that when i close it, it will not stay attached to the target process?

would a simple application.exit() on the close form event do the trick? or is this just a helpless situation?

also it apparently attaches itself to the process, without me even opening it O_O.

and lastly not sure why, but its not returning the right value..

the value of the address changes everytime i move to a new area in the game. this function is supposed to tell me the value of the area that im in. for instance one place will have a value of 3, then if i move somewhere else it will have a new value, lets say 7. then if i go back to the first area, it will be 3 again. each area has its own value.

I know the address and offsets are correct because if i do this with autoit, it works perfectly fine. =/
iCraziE is offline  
Old 01/08/2013, 07:09   #10
 
»jD«'s Avatar
 
elite*gold: 237
Join Date: Sep 2010
Posts: 1,152
Received Thanks: 4,910
Not quite sure whats going on... maybe a screenshot could help?

-jD
»jD« is offline  
Old 01/08/2013, 08:26   #11
 
iCraziE's Avatar
 
elite*gold: 0
Join Date: Jan 2010
Posts: 456
Received Thanks: 218
Here's my entire code. If you insist on a SS i can do that too. I just figured this would be simpler.

iCraziE is offline  
Old 01/08/2013, 08:32   #12
 
»jD«'s Avatar
 
elite*gold: 237
Join Date: Sep 2010
Posts: 1,152
Received Thanks: 4,910
I'm assuming you change "my process" to the process you are trying to access the memory from?

-jD
»jD« is offline  
Old 01/08/2013, 08:41   #13
 
iCraziE's Avatar
 
elite*gold: 0
Join Date: Jan 2010
Posts: 456
Received Thanks: 218
Yes, of course
iCraziE is offline  
Old 01/09/2013, 05:57   #14
 
»jD«'s Avatar
 
elite*gold: 237
Join Date: Sep 2010
Posts: 1,152
Received Thanks: 4,910
Hmmm, are you sure you called CloseHandle in ReadMemory?

-jD
»jD« is offline  
Old 01/09/2013, 11:27   #15
 
iCraziE's Avatar
 
elite*gold: 0
Join Date: Jan 2010
Posts: 456
Received Thanks: 218
yes, I managed to figure it out. Thanks for your help

Here is my call, if anyone is also stuck with the same problems i had.

Code:
Process myProcess = Process.GetProcessesByName("My Process").FirstOrDefault();
            int bytesRead;

            int pointeraddr = 0x00BDCC32;

            byte[] valueOut = ProcessMemoryReaderApi.ReadMemory(myProcess, pointeraddr, 4, out bytesRead); ///read address 1
            int value = BitConverter.ToInt32(valueOut, 0); /// convert to Int32
            string newAddr = DecToHex(value); // convert to hex
            IntPtr mpAddr = (IntPtr)HexToDec(newAddr); // convert to decimal as IntPtr
            IntPtr mpAddr2 = IntPtr.Add(mpAddr, 0x02); // add offset
            int newmpAddr = mpAddr2.ToInt32(); // convert to int for address 2

            byte[] value2 = ProcessMemoryReaderApi.ReadMemory(myProcess, newmpAddr, 4, out bytesRead); // read address 2
            int value2Out = BitConverter.ToInt32(value2, 0); // convert to int32
            string mp = DecToHex(value2Out); //convert to hex
            IntPtr mp2 = (IntPtr)HexToDec(mp); // convert to decimal as IntPtr
            IntPtr secMpAddr = IntPtr.Add(mp2, 0xBD); //add second offset
            int fMpAddr = secMpAddr.ToInt32(); // convert to int for address 3

            byte[] value3 = ProcessMemoryReaderApi.ReadMemory(myProcess, fMpAddr, 4, out bytesRead); // read address 3
            int value3Out = BitConverter.ToInt32(value3, 0); // convert to int32
            string MpVal = value3Out.ToString(); //convert to string for output.

            MessageBox.Show(MpVal);
iCraziE is offline  
Reply


Similar Threads Similar Threads
[Help] Reading Pointer
07/31/2012 - C/C++ - 4 Replies
recently i was learning to make dll for injection and i found this func code: unsigned long ReadPointer(unsigned long ulBase, int iOffset) { __try { return *(unsigned long*)(*(unsigned long*)ulBase + iOffset); } __except (EXCEPTION_EXECUTE_HANDLER) { return 0; } } for example : addr = 0x0012345
[Vb.NET] WoW Memory Reading
11/20/2010 - World of Warcraft - 1 Replies
Hallo, Ist es irgendwie möglich mit VB.NET die Memory von WoW auszulesen wie bei C# mit der BlackMagic.dll Danke m vorraus
[VB.NET] Need help - memory reading from pointer
09/03/2010 - .NET Languages - 2 Replies
Hi, i'm kinda beginner in memory editing, the only experience i got is vb6 and vb.net - and basic CE skills. However i could get the pointer and the offset for a memory address, it works, tested. Next step is the reading of this value with a vb.net application. I have been googleing for 2 days, got some codes, wrote some by myself, but none of them seems to work. Could anyone help me a bit? Thank you, Regards
Help with memory reading. C++.
06/12/2010 - Aion - 0 Replies
Hello people, I'm kinda new to memory reading in c++. Been doing similiar stuff, and done some other stuff like packet hacks etc but anyway, to the issue. I get weird values from AION when reading. And I'm prolly going about this totally wrong so I'll post you the code and hopefully some kind soul out there will point me in the right direction. int address = 0xA82424; int value; DWORD pid; if(!GameWindow) {
VB.Net Memory Reading
11/03/2006 - .NET Languages - 0 Replies
Basically I'm doing a course in VB.Net and I'm wondering if theres anyone here who can teach me or show me how to read memory values in my project. My course does not cover this, so I'm stuck with either self-research or asking questions. If anyone can help then I'd be very grateful. PS: The target game I'll be testing on is Conquer Online 2



All times are GMT +1. The time now is 21:54.


Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2025 elitepvpers All Rights Reserved.