/[RomCheater]/trunk/Win32/Sojaner.MemoryScanner/MemoryScanner.cs
ViewVC logotype

Diff of /trunk/Win32/Sojaner.MemoryScanner/MemoryScanner.cs

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- trunk/Win32/Sojaner.MemoryScanner/MemoryScanner.cs	2012/06/10 04:20:04	369
+++ trunk/Win32/Sojaner.MemoryScanner/MemoryScanner.cs	2012/06/10 05:40:24	370
@@ -17,7 +17,7 @@
     #region ProcessMemoryReader class
     //Thanks goes to Arik Poznanski for P/Invokes and methods needed to read and write the Memory
     //For more information refer to "Minesweeper, Behind the scenes" article by Arik Poznanski at Codeproject.com
-    internal class ProcessMemoryReader : IMemoryReader, IMemoryWriter, IFileWriter
+    internal class ProcessMemoryReader : IMemoryReader, IMemoryWriter, IFileWriter, IPatchMemory,IReadMemory
     {
 
         public ProcessMemoryReader()
@@ -304,6 +304,7 @@
                     }
                     else
                     {
+                        //buffer_list.Add((byte)'?');
                         buffer_list.Add(0);
                         _MemoryAddress++;
                     }
@@ -445,6 +446,300 @@
         }
         #endregion
         #endregion
+
+        #region IPatchMemory members
+        #region public virtual bool PatchMemory(int address, byte value)
+        public virtual bool PatchMemory(int address, byte value)
+        {
+            byte[] bitData = BitConverter.GetBytes(value);
+            UIntPtr ptrBytesWritten;
+            ProcessMemoryReaderApi.WriteProcessMemory(m_hProcess, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten);
+            if (ptrBytesWritten.ToUInt32() == 0)
+                return false;
+            byte check = 0;
+            ReadMemory(address, out check);
+            if (check == value) return true;
+            return false;
+        }
+        #endregion
+        #region public virtual bool PatchMemory(int address, sbyte value)
+        public virtual bool PatchMemory(int address, sbyte value)
+        {
+            byte[] bitData = BitConverter.GetBytes(value);
+            UIntPtr ptrBytesWritten;
+            ProcessMemoryReaderApi.WriteProcessMemory(m_hProcess, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten);
+            if (ptrBytesWritten.ToUInt32() == 0)
+                return false;
+            sbyte check = 0;
+            ReadMemory(address, out check);
+            if (check == value) return true;
+            return false;
+        }
+        #endregion
+        #region public virtual bool PatchMemory(int address, ushort value)
+        public virtual bool PatchMemory(int address, ushort value)
+        {
+            byte[] bitData = BitConverter.GetBytes(value);
+            UIntPtr ptrBytesWritten;
+            ProcessMemoryReaderApi.WriteProcessMemory(m_hProcess, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten);
+            if (ptrBytesWritten.ToUInt32() == 0)
+                return false;
+            ushort check = 0;
+            ReadMemory(address, out check);
+            if (check == value) return true;
+            return false;
+        }
+        #endregion
+        #region public virtual bool PatchMemory(int address, short value)
+        public virtual bool PatchMemory(int address, short value)
+        {           
+            byte[] bitData = BitConverter.GetBytes(value);
+            UIntPtr ptrBytesWritten;
+            ProcessMemoryReaderApi.WriteProcessMemory(m_hProcess, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten);
+            if (ptrBytesWritten.ToUInt32() == 0)
+                return false;
+            short check = 0;
+            ReadMemory(address, out check);
+            if (check == value) return true;
+            return false;
+        }
+        #endregion
+        #region public virtual bool PatchMemory(int address, uint value)
+        public virtual bool PatchMemory(int address, uint value)
+        {
+            byte[] bitData = BitConverter.GetBytes(value);
+            UIntPtr ptrBytesWritten;
+            ProcessMemoryReaderApi.WriteProcessMemory(m_hProcess, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten);
+            if (ptrBytesWritten.ToUInt32() == 0)
+                return false;
+            uint check = 0;
+            ReadMemory(address, out check);
+            if (check == value) return true;
+            return false;
+        }
+        #endregion
+        #region public virtual bool PatchMemory(int address, int value)
+        public virtual bool PatchMemory(int address, int value)
+        {
+            byte[] bitData = BitConverter.GetBytes(value);
+            UIntPtr ptrBytesWritten;
+            ProcessMemoryReaderApi.WriteProcessMemory(m_hProcess, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten);
+            if (ptrBytesWritten.ToUInt32() == 0)
+                return false;
+            int check = 0;
+            ReadMemory(address, out check);
+            if (check == value) return true;
+            return false;
+        }
+        #endregion
+        #region public virtual bool PatchMemory(int address, ulong value)
+        public virtual bool PatchMemory(int address, ulong value)
+        {
+            byte[] bitData = BitConverter.GetBytes(value);
+            UIntPtr ptrBytesWritten;
+            ProcessMemoryReaderApi.WriteProcessMemory(m_hProcess, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten);
+            if (ptrBytesWritten.ToUInt32() == 0)
+                return false;
+            ulong check = 0;
+            ReadMemory(address, out check);
+            if (check == value) return true;
+            return false;
+        }
+        #endregion
+        #region public virtual bool PatchMemory(int address, long value)
+        public virtual bool PatchMemory(int address, long value)
+        {
+            byte[] bitData = BitConverter.GetBytes(value);
+            UIntPtr ptrBytesWritten;
+            ProcessMemoryReaderApi.WriteProcessMemory(m_hProcess, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten);
+            if (ptrBytesWritten.ToUInt32() == 0)
+                return false;
+            long check = 0;
+            ReadMemory(address, out check);
+            if (check == value) return true;
+            return false;
+        }
+        #endregion
+        #endregion
+
+        #region IReadMemory members
+        #region public virtual bool ReadMemory(int address, out byte value)
+        public virtual bool ReadMemory(int address, out byte value)
+        {
+            value = 0;           
+            try
+            {
+                int bytesRead;
+                uint size = sizeof(byte);
+                byte[] bitData = new byte[size];
+                ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(m_hProcess, (IntPtr)address, bitData, size, out bytesRead);
+                if (bytesRead == 0)
+                    return false;
+                value = bitData[0];
+                return true;
+            }
+            catch
+            {
+                value = 0x00;
+                return false;
+            }
+        }
+        #endregion
+        #region public virtual bool ReadMemory(int address, out sbyte value)
+        public virtual bool ReadMemory(int address, out sbyte value)
+        {
+            value = 0;           
+            try
+            {
+                int bytesRead;
+                uint size = sizeof(sbyte);
+                byte[] bitData = new byte[size];
+                ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(m_hProcess, (IntPtr)address, bitData, size, out bytesRead);
+                if (bytesRead == 0)
+                    return false;
+                value = Convert.ToSByte(bitData[0]);
+                return true;
+            }
+            catch
+            {
+                value = 0x00;
+                return false;
+            }
+        }
+        #endregion
+        #region public virtual bool ReadMemory(int address, out ushort value)
+        public virtual bool ReadMemory(int address, out ushort value)
+        {
+            value = 0;           
+            try
+            {
+                int bytesRead;
+                uint size = sizeof(ushort);
+                byte[] bitData = new byte[size];
+                ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(m_hProcess, (IntPtr)address, bitData, size, out bytesRead);
+                if (bytesRead == 0)
+                    return false;
+                value = BitConverter.ToUInt16(bitData, 0);
+                return true;
+            }
+            catch
+            {
+                value = 0x00;
+                return false;
+            }
+        }
+        #endregion
+        #region public virtual bool ReadMemory(int address, out short value)
+        public virtual bool ReadMemory(int address, out short value)
+        {
+            value = 0;           
+            try
+            {
+                int bytesRead;
+                uint size = sizeof(short);
+                byte[] bitData = new byte[size];
+                ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(m_hProcess, (IntPtr)address, bitData, size, out bytesRead);
+                if (bytesRead == 0)
+                    return false;
+                value = BitConverter.ToInt16(bitData, 0);
+                return true;
+            }
+            catch
+            {
+                value = 0x00;
+                return false;
+            }
+        }
+        #endregion
+        #region public virtual bool ReadMemory(int address, out uint value)
+        public virtual bool ReadMemory(int address, out uint value)
+        {
+            value = 0;           
+            try
+            {
+                int bytesRead;
+                uint size = sizeof(uint);
+                byte[] bitData = new byte[size];
+                ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(m_hProcess, (IntPtr)address, bitData, size, out bytesRead);
+                if (bytesRead == 0)
+                    return false;
+                value = BitConverter.ToUInt32(bitData, 0);
+                return true;
+            }
+            catch
+            {
+                value = 0x00;
+                return false;
+            }
+        }
+        #endregion
+        #region public virtual bool ReadMemory(int address, out int value)
+        public virtual bool ReadMemory(int address, out int value)
+        {
+            value = 0;           
+            try
+            {
+                int bytesRead;
+                uint size = sizeof(int);
+                byte[] bitData = new byte[size];
+                ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(m_hProcess, (IntPtr)address, bitData, size, out bytesRead);
+                if (bytesRead == 0)
+                    return false;
+                value = BitConverter.ToInt32(bitData, 0);
+                return true;
+            }
+            catch
+            {
+                value = 0x00;
+                return false;
+            }
+        }
+        #endregion
+        #region public virtual bool ReadMemory(int address, out ulong value)
+        public virtual bool ReadMemory(int address, out ulong value)
+        {
+            value = 0;           
+            try
+            {
+                int bytesRead;
+                uint size = sizeof(ulong);
+                byte[] bitData = new byte[size];
+                ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(m_hProcess, (IntPtr)address, bitData, size, out bytesRead);
+                if (bytesRead == 0)
+                    return false;
+                value = BitConverter.ToUInt64(bitData, 0);
+                return true;
+            }
+            catch
+            {
+                value = 0x00;
+                return false;
+            }
+        }
+        #endregion
+        #region public virtual bool ReadMemory(int address, out long value)
+        public virtual bool ReadMemory(int address, out long value)
+        {
+            value = 0;           
+            try
+            {
+                int bytesRead;
+                uint size = sizeof(long);
+                byte[] bitData = new byte[size];
+                ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(m_hProcess, (IntPtr)address, bitData, size, out bytesRead);
+                if (bytesRead == 0)
+                    return false;
+                value = BitConverter.ToInt64(bitData, 0);
+                return true;
+            }
+            catch
+            {
+                value = 0x00;
+                return false;
+            }
+        }
+        #endregion
+        #endregion
     }
     #endregion
 }

 

  ViewVC Help
Powered by ViewVC 1.1.22