/[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/05/28 04:14:03	156
+++ trunk/Win32/Sojaner.MemoryScanner/MemoryScanner.cs	2012/05/28 08:31:56	167
@@ -5,6 +5,8 @@
 using System.Threading;
 using System.Runtime.InteropServices;
 using RomCheater.Logging;
+using RomCheater.Core;
+using System.IO;
 
 namespace Sojaner.MemoryScanner
 {
@@ -66,18 +68,97 @@
             }
         }
 
-        public byte[] ReadProcessMemory(IntPtr MemoryAddress, uint bytesToRead, out int bytesRead)
+
+        public bool DumpMemory(string filename, uint MemoryAddress, uint bytesToRead, out int bytesRead)
+        {
+            bytesRead = 0;
+            uint byte_alignment = 102400; // write to file in 100mb chunks
+            uint address = MemoryAddress;
+            uint _bytesToRead = bytesToRead;
+            byte[] buffer = new byte[] { };
+            try
+            {
+                FileInfo fi = new FileInfo(filename);
+                if (fi.Exists)
+                    fi.Delete();
+                using (FileStream fs = new FileStream(filename, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite))
+                {
+                    BinaryWriter bw = new BinaryWriter(fs);
+                    //foreach (byte b in data) { bw.Write(b); }
+
+                    for (uint i = 0; i <= bytesToRead;)
+                    {
+                        if (_bytesToRead < byte_alignment)
+                        {
+                            _bytesToRead = bytesToRead;
+                            buffer = new byte[_bytesToRead];
+                        }
+                        else
+                        {
+                            _bytesToRead = byte_alignment;
+                            buffer = new byte[byte_alignment];
+                        }
+                        IntPtr ptrBytesRead;
+                        ProcessMemoryReaderApi.ReadProcessMemory(m_hProcess, (UIntPtr)address, buffer, _bytesToRead, out ptrBytesRead);
+                        bytesRead = ptrBytesRead.ToInt32();
+                        bw.Write(buffer);
+                        bw.Flush();
+                        
+                        if (_bytesToRead < byte_alignment)
+                        {
+                            i += _bytesToRead;
+                            address += _bytesToRead;
+                        }
+                        else
+                        {
+                            i += byte_alignment;
+                            address += byte_alignment;
+                        }
+
+                        
+                    }
+                    bw.Close();
+                }
+                return true;
+            }
+            catch (OutOfMemoryException ex)
+            {
+                logger.Error.WriteLine("DumpMemory(): OutOfMemoryException");
+                logger.Error.WriteLine(ex.ToString());
+            }
+            catch (Exception ex)
+            {
+                logger.Error.WriteLine("DumpMemory(): Exception");
+                logger.Error.WriteLine(ex.ToString());
+            }
+            return false;
+        }
+
+        public byte[] ReadProcessMemory(uint MemoryAddress, uint bytesToRead, out int bytesRead)
         {
+            bytesRead = 0;
+            uint address = MemoryAddress;
+            List<byte[]> aligned_array_list = new List<byte[]>();
             try
             {
-                byte[] buffer = new byte[bytesToRead - 1];
+                uint byte_alignment = 512; // 4mb alignment
+               
 
-                IntPtr ptrBytesRead;
-                ProcessMemoryReaderApi.ReadProcessMemory(m_hProcess, MemoryAddress, buffer, bytesToRead, out ptrBytesRead);
+                for (uint i = 0; i <= bytesToRead; i += byte_alignment)
+                {
+                    byte[] buffer = new byte[byte_alignment];
+                    uint bytes_to_read = byte_alignment;
+                    IntPtr ptrBytesRead;
+                    ProcessMemoryReaderApi.ReadProcessMemory(m_hProcess, (UIntPtr)address, buffer, bytes_to_read, out ptrBytesRead);
+                    bytesRead = ptrBytesRead.ToInt32();
+                    aligned_array_list.Add(buffer);                    
+                    address += byte_alignment;
+                }
 
-                bytesRead = ptrBytesRead.ToInt32();
+                //List<byte> big_array = new List<byte>();
+                //foreach (byte[] aligned_array in aligned_array_list) { foreach (byte b in aligned_array) { big_array.Add(b); } }
 
-                return buffer;
+                return new byte[] { };
             }
             catch (OutOfMemoryException ex)
             {
@@ -89,11 +170,10 @@
                 logger.Error.WriteLine("ReadProcessMemory(): Exception");
                 logger.Error.WriteLine(ex.ToString());
             }
-            bytesRead = 0; 
             return new byte[] { };
         }
 
-        public void WriteProcessMemory(IntPtr MemoryAddress, byte[] bytesToWrite, out int bytesWritten)
+        public void WriteProcessMemory(UIntPtr MemoryAddress, byte[] bytesToWrite, out int bytesWritten)
         {
             IntPtr ptrBytesWritten;
             ProcessMemoryReaderApi.WriteProcessMemory(m_hProcess, MemoryAddress, bytesToWrite, (uint)bytesToWrite.Length, out ptrBytesWritten);
@@ -148,7 +228,7 @@
             //			SIZE_T * lpNumberOfBytesRead  // number of bytes read
             //			);
             [DllImport("kernel32.dll")]
-            public static extern Int32 ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [In, Out] byte[] buffer, UInt32 size, out IntPtr lpNumberOfBytesRead);
+            public static extern Int32 ReadProcessMemory(IntPtr hProcess, UIntPtr lpBaseAddress, [In, Out] byte[] buffer, UInt32 size, out IntPtr lpNumberOfBytesRead);
 
             //		BOOL WriteProcessMemory(
             //			HANDLE hProcess,                // handle to process
@@ -158,7 +238,7 @@
             //			SIZE_T * lpNumberOfBytesWritten // count of bytes written
             //			);
             [DllImport("kernel32.dll")]
-            public static extern Int32 WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, [In, Out] byte[] buffer, UInt32 size, out IntPtr lpNumberOfBytesWritten);
+            public static extern Int32 WriteProcessMemory(IntPtr hProcess, UIntPtr lpBaseAddress, [In, Out] byte[] buffer, UInt32 size, out IntPtr lpNumberOfBytesWritten);
 
 
         }

 

  ViewVC Help
Powered by ViewVC 1.1.22