/[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 08:14:02	166
+++ trunk/Win32/Sojaner.MemoryScanner/MemoryScanner.cs	2012/05/28 08:49:42	169
@@ -69,40 +69,69 @@
         }
 
 
-        public bool DumpMemory(string filename, uint MemoryAddress, uint bytesToRead, out int bytesRead)
+        public bool DumpMemory(Process ppid, string filename, uint MemoryAddress, uint bytesToRead, out int bytesRead)
         {
+            logger.Info.WriteLine("Dumping memory (0x{0:x8}-0x{1:x8}) from pid=({3}) to file {2}", MemoryAddress, MemoryAddress + bytesToRead, filename, string.Format("0x{0:x4} {1}.exe", ppid.Id, ppid.ProcessName));
             bytesRead = 0;
             uint byte_alignment = 102400; // write to file in 100mb chunks
             uint address = MemoryAddress;
+            uint _bytesToRead = bytesToRead;
+            byte[] buffer = new byte[] { };
             try
             {
-                using (FileStream fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
+                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; i += byte_alignment)
+                    for (uint i = 0; i <= bytesToRead;)
                     {
-                        byte[] buffer = new byte[byte_alignment];
-                        uint bytes_to_read = byte_alignment;
+                        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, bytes_to_read, out ptrBytesRead);
+                        ProcessMemoryReaderApi.ReadProcessMemory(m_hProcess, (UIntPtr)address, buffer, _bytesToRead, out ptrBytesRead);
                         bytesRead = ptrBytesRead.ToInt32();
                         bw.Write(buffer);
                         bw.Flush();
-                        address += byte_alignment;
+                        
+                        if (_bytesToRead < byte_alignment)
+                        {
+                            i += _bytesToRead;
+                            address += _bytesToRead;
+                        }
+                        else
+                        {
+                            i += byte_alignment;
+                            address += byte_alignment;
+                        }
+
+                        
                     }
                     bw.Close();
                 }
+                logger.Info.WriteLine("Succefully dumped memory (0x{0:x8}-0x{1:x8}) from pid=({3}) to file {2}", MemoryAddress, MemoryAddress + bytesToRead, filename, string.Format("0x{0:x4} {1}.exe", ppid.Id, ppid.ProcessName));
                 return true;
             }
             catch (OutOfMemoryException ex)
             {
+                logger.Error.WriteLine("Failed to dump memory (0x{0:x8}-0x{1:x8}) from pid=({3}) to file {2}", MemoryAddress, MemoryAddress + bytesToRead, filename, string.Format("0x{0:x4} {1}.exe", ppid.Id, ppid.ProcessName));
                 logger.Error.WriteLine("DumpMemory(): OutOfMemoryException");
                 logger.Error.WriteLine(ex.ToString());
             }
             catch (Exception ex)
             {
+                logger.Error.WriteLine("Failed to dump memory (0x{0:x8}-0x{1:x8}) from pid=({3}) to file {2}", MemoryAddress, MemoryAddress + bytesToRead, filename, string.Format("0x{0:x4} {1}.exe", ppid.Id, ppid.ProcessName));
                 logger.Error.WriteLine("DumpMemory(): Exception");
                 logger.Error.WriteLine(ex.ToString());
             }

 

  ViewVC Help
Powered by ViewVC 1.1.22