/[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/09 20:07:18	350
+++ trunk/Win32/Sojaner.MemoryScanner/MemoryScanner.cs	2012/06/09 20:52:19	351
@@ -8,6 +8,8 @@
 using System.IO;
 using Sojaner.MemoryScanner.MemoryProviers;
 using Microsoft.Win32.SafeHandles;
+using Microsoft.Win32.Interop;
+using System.ComponentModel;
 
 namespace Sojaner.MemoryScanner
 {
@@ -259,9 +261,32 @@
         {
             try
             {
-                byte[] buffer = new byte[bytesToRead];
-                ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(m_hProcess, (IntPtr)MemoryAddress, buffer, bytesToRead, out bytesRead);
-                data = buffer;
+                bytesRead = 0;
+
+                List<byte> buffer_list = new List<byte>();
+                uint _MemoryAddress = MemoryAddress;
+                for (int i = 0; i < bytesToRead; i++)
+                {
+                    byte[] buffer = new byte[1];
+                    int _bytesRead = 0;
+                    ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(m_hProcess, (IntPtr)_MemoryAddress, buffer, 1, out _bytesRead);
+                    if (_bytesRead > 0)
+                    {
+                        foreach (byte b in buffer)
+                        {
+                            buffer_list.Add(b);
+                        }
+                        _MemoryAddress += (uint)buffer.Length;
+                    }
+                    else
+                    {
+                        buffer_list.Add(0);
+                        _MemoryAddress++;
+                    }
+                    
+                }
+                bytesRead = buffer_list.Count;
+                data = buffer_list.ToArray();
             }
             catch (SEHException ex)
             {
@@ -304,6 +329,20 @@
                 UIntPtr ptrBytesWritten;
                 ProcessMemoryReaderApi.WriteProcessMemory(m_hProcess, (IntPtr)MemoryAddress, bytesToWrite, (uint)bytesToWrite.Length, out ptrBytesWritten);
                 bytesWritten = ptrBytesWritten.ToUInt32();
+                int LastWin32Error = Marshal.GetLastWin32Error();
+                if (LastWin32Error != ResultWin32.ERROR_SUCCESS)
+                {
+                    string error = ResultWin32.GetErrorName(LastWin32Error);
+                    throw new Win32Exception(LastWin32Error, "Unable to read process memory");
+                }
+            }
+            catch (Win32Exception ex)
+            {
+                string error_name = ResultWin32.GetErrorName(ex.ErrorCode);
+                error_name = (error_name == string.Empty) ? "undefined" : error_name;
+                logger.Error.WriteLine("WriteProcessMemory() Win32Exception was thrown: ErrorCode:{0} - {1}", string.Format("0x{0:x8}", ex.ErrorCode, error_name), ex.Message);
+                logger.Error.WriteLine(ex.ToString());
+                throw ex;
             }
             catch (SEHException ex)
             {

 

  ViewVC Help
Powered by ViewVC 1.1.22