ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/RomCheater/trunk/Win32/Sojaner.MemoryScanner/MemoryScanner.cs
(Generate patch)

Comparing trunk/Win32/Sojaner.MemoryScanner/MemoryScanner.cs (file contents):
Revision 156 by william, Mon May 28 04:14:03 2012 UTC vs.
Revision 162 by william, Mon May 28 07:12:37 2012 UTC

--- trunk/Win32/Sojaner.MemoryScanner/MemoryScanner.cs	2012/05/28 04:14:03	156
+++ trunk/Win32/Sojaner.MemoryScanner/MemoryScanner.cs	2012/05/28 07:12:37	162
@@ -5,6 +5,8 @@ using System.Diagnostics;
 using System.Threading;
 using System.Runtime.InteropServices;
 using RomCheater.Logging;
+using RomCheater.Core;
+using System.IO;
 
 namespace Sojaner.MemoryScanner
 {
@@ -66,18 +68,72 @@ namespace Sojaner.MemoryScanner
             }
         }
 
-        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 = 512; // 4mb alignment
+            uint address = MemoryAddress;
+            try
+            {
+                using (FileStream fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
+                {
+                    BinaryWriter bw = new BinaryWriter(fs);
+                    //foreach (byte b in data) { bw.Write(b); }
+
+                    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, (IntPtr)address, buffer, bytes_to_read, out ptrBytesRead);
+                        bytesRead = ptrBytesRead.ToInt32();
+                        bw.Write(buffer);
+                        bw.Flush();
+                        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, (IntPtr)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,7 +145,6 @@ namespace Sojaner.MemoryScanner
                 logger.Error.WriteLine("ReadProcessMemory(): Exception");
                 logger.Error.WriteLine(ex.ToString());
             }
-            bytesRead = 0; 
             return new byte[] { };
         }