/[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	2013/06/03 05:48:41	470
+++ trunk/Win32/Sojaner.MemoryScanner/MemoryScanner.cs	2013/06/03 07:13:44	471
@@ -25,6 +25,7 @@
 using System.ComponentModel;
 using ManagedWinapi;
 using RomCheater.PluginFramework.Events;
+using libWin32.Win32.Threading;
 
 namespace Sojaner.MemoryScanner
 {
@@ -85,19 +86,19 @@
         /// <summary>	
         /// Process from which to read		
         /// </summary>
-        public Process ReadProcess
+        public int ProcessPID
         {
             get
             {
-                return m_ReadProcess;
+                return m_ProcessPID;
             }
             set
             {
-                m_ReadProcess = value;
+                m_ProcessPID = value;
             }
         }
 
-        private Process m_ReadProcess = null;
+        private int m_ProcessPID = 0;
 
         //SafeWaitHandle handle;
         //private IntPtr handle = IntPtr.Zero;
@@ -257,7 +258,7 @@
                     ReadProcessMemory(mem_address, _bytesToRead, out bytesRead, out buffer);
                     if (buffer.Length == 0 && bytesRead == 0)
                     {
-                        throw new Exception(string.Format("Failed to read memory from process: {0}", ReadProcess.ToString()));
+                        throw new Exception(string.Format("Failed to read memory from process: {0}", ProcessUtils.ProcessName(m_ProcessPID)));
                     }
                     //bw.Write(buffer);
                     //bw.Flush();
@@ -310,57 +311,64 @@
 
         public void ReadProcessMemoryAtOnce(uint MemoryAddress, uint bytesToRead, object UserState)
         {
-            uint size = 1024 * 128;
-            int count = 0;
-            for (uint j = MemoryAddress; j < (MemoryAddress + bytesToRead); j += size)
+            using (Process p = Process.GetProcessById(m_ProcessPID))
             {
-                using (ProcessMemoryChunk mem = new ProcessMemoryChunk(m_ReadProcess, (IntPtr)j, (int)size))
+                uint size = 1024 * 128;
+                int count = 0;
+                for (uint j = MemoryAddress; j < (MemoryAddress + bytesToRead); j += size)
                 {
-                    byte[] bigMem = mem.Read();
-                    count += bigMem.Length;
-                    if (this.OnBytesRead != null)
+                    using (ProcessMemoryChunk mem = new ProcessMemoryChunk(p, (IntPtr)j, (int)size))
                     {
-                        OnBytesReadEventArgs t = new OnBytesReadEventArgs(this, UserState, bigMem, j, bytesToRead);
-                        this.OnBytesRead.Invoke(t);
-                        if (t.Canceled)
+                        byte[] bigMem = mem.Read();
+                        count += bigMem.Length;
+                        if (this.OnBytesRead != null)
                         {
-                            bigMem = null;
-                            break;
+                            OnBytesReadEventArgs t = new OnBytesReadEventArgs(this, UserState, bigMem, j, bytesToRead);
+                            this.OnBytesRead.Invoke(t);
+                            if (t.Canceled)
+                            {
+                                bigMem = null;
+                                break;
+                            }
+
                         }
-                        
+                        bigMem = null;
                     }
-                    bigMem = null;
+
                 }
             }
         }
         public void ReadProcessMemoryAtOnce(uint MemoryAddress, uint bytesToRead, out int bytesRead, out byte[] data)
         {
-            Stopwatch st = new Stopwatch();
-            st.Start();
-            List<byte> buffer_list = new List<byte>();
-            //for (uint j = MemoryAddress; j < (MemoryAddress+bytesToRead); j += size)
-            //{
-            //    using (ProcessMemoryChunk mem = new ProcessMemoryChunk(m_ReadProcess, (IntPtr)j, (int)bytesToRead))
-            //    {
-            //        byte[] bigMem = mem.Read();
-            //        foreach (byte b in bigMem) { buffer_list.Add(b); }
-            //        bigMem = null;
-            //    }
-            //}
-
-            using (ProcessMemoryChunk mem = new ProcessMemoryChunk(m_ReadProcess, (IntPtr)MemoryAddress, (int)bytesToRead))
-            {
-                byte[] bigMem = mem.Read();
-                foreach (byte b in bigMem) { buffer_list.Add(b); }
-                bigMem = null;
-            }
-
-            bytesRead = buffer_list.Count;
-            data = new byte[bytesToRead];
-            data.Initialize();
-            Buffer.BlockCopy(buffer_list.ToArray(), 0, data, 0, (int)bytesToRead);
-            st.Stop();
-            logger.Profiler.WriteLine("ReadProcessMemoryAtOnce(): start=0x{0:x8} end=0x{1:x8} took {2:0.0000} seconds", MemoryAddress, MemoryAddress + bytesToRead, st.Elapsed.TotalSeconds);
+            using (Process p = Process.GetProcessById(m_ProcessPID))
+            {
+                Stopwatch st = new Stopwatch();
+                st.Start();
+                List<byte> buffer_list = new List<byte>();
+                //for (uint j = MemoryAddress; j < (MemoryAddress+bytesToRead); j += size)
+                //{
+                //    using (ProcessMemoryChunk mem = new ProcessMemoryChunk(m_ReadProcess, (IntPtr)j, (int)bytesToRead))
+                //    {
+                //        byte[] bigMem = mem.Read();
+                //        foreach (byte b in bigMem) { buffer_list.Add(b); }
+                //        bigMem = null;
+                //    }
+                //}
+
+                using (ProcessMemoryChunk mem = new ProcessMemoryChunk(p, (IntPtr)MemoryAddress, (int)bytesToRead))
+                {
+                    byte[] bigMem = mem.Read();
+                    foreach (byte b in bigMem) { buffer_list.Add(b); }
+                    bigMem = null;
+                }
+
+                bytesRead = buffer_list.Count;
+                data = new byte[bytesToRead];
+                data.Initialize();
+                Buffer.BlockCopy(buffer_list.ToArray(), 0, data, 0, (int)bytesToRead);
+                st.Stop();
+                logger.Profiler.WriteLine("ReadProcessMemoryAtOnce(): start=0x{0:x8} end=0x{1:x8} took {2:0.0000} seconds", MemoryAddress, MemoryAddress + bytesToRead, st.Elapsed.TotalSeconds);
+            }
         }
         #region public void ReadProcessMemory(int MemoryAddress, uint bytesToRead, out int bytesRead, out byte[] data)
         public void ReadProcessMemory(int MemoryAddress, uint bytesToRead, out int bytesRead, out byte[] data)
@@ -446,24 +454,27 @@
         {
             try
             {
-                //uint _bytesWritten = 0;
-                uint _MemoryAddress = MemoryAddress;
-
-                using (ProcessMemoryChunk mem = new ProcessMemoryChunk(m_ReadProcess, (IntPtr)MemoryAddress, bytesToWrite.Length))
+                using (Process p = Process.GetProcessById(m_ProcessPID))
                 {
-                    mem.Write(0, bytesToWrite);
-                }
+                    //uint _bytesWritten = 0;
+                    uint _MemoryAddress = MemoryAddress;
 
-                //for (int i = 0; i < bytesToWrite.Length; i++)
-                //{
-                //    UIntPtr _ptrBytesWritten = UIntPtr.Zero;
-                //    byte[] buffer = new byte[] { bytesToWrite[i] };
-                //    ProcessMemoryReaderApi.WriteProcessMemory(handle, (IntPtr)_MemoryAddress, buffer, (uint)buffer.Length, out _ptrBytesWritten);
-                //    _MemoryAddress++;
-                //    _bytesWritten++;
-                //}
-                //bytesWritten = _bytesWritten;
-                bytesWritten = (uint)bytesToWrite.Length;
+                    using (ProcessMemoryChunk mem = new ProcessMemoryChunk(p, (IntPtr)MemoryAddress, bytesToWrite.Length))
+                    {
+                        mem.Write(0, bytesToWrite);
+                    }
+
+                    //for (int i = 0; i < bytesToWrite.Length; i++)
+                    //{
+                    //    UIntPtr _ptrBytesWritten = UIntPtr.Zero;
+                    //    byte[] buffer = new byte[] { bytesToWrite[i] };
+                    //    ProcessMemoryReaderApi.WriteProcessMemory(handle, (IntPtr)_MemoryAddress, buffer, (uint)buffer.Length, out _ptrBytesWritten);
+                    //    _MemoryAddress++;
+                    //    _bytesWritten++;
+                    //}
+                    //bytesWritten = _bytesWritten;
+                    bytesWritten = (uint)bytesToWrite.Length;
+                }
             }
             catch (SEHException ex)
             {
@@ -534,7 +545,7 @@
                     }
                     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", ReadProcess.Id, ReadProcess.ProcessName));
+                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", m_ProcessPID, ProcessUtils.ProcessName(m_ProcessPID)));
                 return true;
             }
             catch (SEHException ex)
@@ -563,11 +574,14 @@
         #region public virtual bool PatchMemory(uint address, byte value)
         public virtual bool PatchMemory(uint address, byte value)
         {
-            byte[] bitData = BitConverter.GetBytes(value);
-            //UIntPtr ptrBytesWritten;
-            using (ProcessMemoryChunk mem = new ProcessMemoryChunk(m_ReadProcess, (IntPtr)address, bitData.Length))
+            using (Process p = Process.GetProcessById(m_ProcessPID))
             {
-                mem.Write(0, bitData);
+                byte[] bitData = BitConverter.GetBytes(value);
+                //UIntPtr ptrBytesWritten;
+                using (ProcessMemoryChunk mem = new ProcessMemoryChunk(p, (IntPtr)address, bitData.Length))
+                {
+                    mem.Write(0, bitData);
+                }
             }
             //ProcessMemoryReaderApi.WriteProcessMemory(handle, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten);
             //if (ptrBytesWritten.ToUInt32() == 0)
@@ -576,16 +590,20 @@
             ReadMemory(address, out check);
             if (check == value) return true;
             return false;
+
         }
         #endregion
         #region public virtual bool PatchMemory(uint address, sbyte value)
         public virtual bool PatchMemory(uint address, sbyte value)
         {
-            byte[] bitData = BitConverter.GetBytes(value);
-            //UIntPtr ptrBytesWritten;
-            using (ProcessMemoryChunk mem = new ProcessMemoryChunk(m_ReadProcess, (IntPtr)address, bitData.Length))
+            using (Process p = Process.GetProcessById(m_ProcessPID))
             {
-                mem.Write(0, bitData);
+                byte[] bitData = BitConverter.GetBytes(value);
+                //UIntPtr ptrBytesWritten;
+                using (ProcessMemoryChunk mem = new ProcessMemoryChunk(p, (IntPtr)address, bitData.Length))
+                {
+                    mem.Write(0, bitData);
+                }
             }
             //ProcessMemoryReaderApi.WriteProcessMemory(handle, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten);
             //if (ptrBytesWritten.ToUInt32() == 0)
@@ -599,11 +617,14 @@
         #region public virtual bool PatchMemory(uint address, ushort value)
         public virtual bool PatchMemory(uint address, ushort value)
         {
-            byte[] bitData = BitConverter.GetBytes(value);
-            //UIntPtr ptrBytesWritten;
-            using (ProcessMemoryChunk mem = new ProcessMemoryChunk(m_ReadProcess, (IntPtr)address, bitData.Length))
+            using (Process p = Process.GetProcessById(m_ProcessPID))
             {
-                mem.Write(0, bitData);
+                byte[] bitData = BitConverter.GetBytes(value);
+                //UIntPtr ptrBytesWritten;
+                using (ProcessMemoryChunk mem = new ProcessMemoryChunk(p, (IntPtr)address, bitData.Length))
+                {
+                    mem.Write(0, bitData);
+                }
             }
             //if (ptrBytesWritten.ToUInt32() == 0)
             //    return false;
@@ -615,12 +636,15 @@
         #endregion
         #region public virtual bool PatchMemory(uint address, short value)
         public virtual bool PatchMemory(uint address, short value)
-        {           
-            byte[] bitData = BitConverter.GetBytes(value);
-            //UIntPtr ptrBytesWritten;
-            using (ProcessMemoryChunk mem = new ProcessMemoryChunk(m_ReadProcess, (IntPtr)address, bitData.Length))
+        {
+            using (Process p = Process.GetProcessById(m_ProcessPID))
             {
-                mem.Write(0, bitData);
+                byte[] bitData = BitConverter.GetBytes(value);
+                //UIntPtr ptrBytesWritten;
+                using (ProcessMemoryChunk mem = new ProcessMemoryChunk(p, (IntPtr)address, bitData.Length))
+                {
+                    mem.Write(0, bitData);
+                }
             }
             //if (ptrBytesWritten.ToUInt32() == 0)
             //    return false;
@@ -633,11 +657,14 @@
         #region public virtual bool PatchMemory(uint address, uint value)
         public virtual bool PatchMemory(uint address, uint value)
         {
-            byte[] bitData = BitConverter.GetBytes(value);
-            //UIntPtr ptrBytesWritten;
-            using (ProcessMemoryChunk mem = new ProcessMemoryChunk(m_ReadProcess, (IntPtr)address, bitData.Length))
+            using (Process p = Process.GetProcessById(m_ProcessPID))
             {
-                mem.Write(0, bitData);
+                byte[] bitData = BitConverter.GetBytes(value);
+                //UIntPtr ptrBytesWritten;
+                using (ProcessMemoryChunk mem = new ProcessMemoryChunk(p, (IntPtr)address, bitData.Length))
+                {
+                    mem.Write(0, bitData);
+                }
             }
             //if (ptrBytesWritten.ToUInt32() == 0)
             //    return false;
@@ -650,11 +677,14 @@
         #region public virtual bool PatchMemory(uint address, int value)
         public virtual bool PatchMemory(uint address, int value)
         {
-            byte[] bitData = BitConverter.GetBytes(value);
-            //UIntPtr ptrBytesWritten;
-            using (ProcessMemoryChunk mem = new ProcessMemoryChunk(m_ReadProcess, (IntPtr)address, bitData.Length))
+            using (Process p = Process.GetProcessById(m_ProcessPID))
             {
-                mem.Write(0, bitData);
+                byte[] bitData = BitConverter.GetBytes(value);
+                //UIntPtr ptrBytesWritten;
+                using (ProcessMemoryChunk mem = new ProcessMemoryChunk(p, (IntPtr)address, bitData.Length))
+                {
+                    mem.Write(0, bitData);
+                }
             }
             //if (ptrBytesWritten.ToUInt32() == 0)
             //    return false;
@@ -667,11 +697,14 @@
         #region public virtual bool PatchMemory(uint address, ulong value)
         public virtual bool PatchMemory(uint address, ulong value)
         {
-            byte[] bitData = BitConverter.GetBytes(value);
-            //UIntPtr ptrBytesWritten;
-            using (ProcessMemoryChunk mem = new ProcessMemoryChunk(m_ReadProcess, (IntPtr)address, bitData.Length))
+            using (Process p = Process.GetProcessById(m_ProcessPID))
             {
-                mem.Write(0, bitData);
+                byte[] bitData = BitConverter.GetBytes(value);
+                //UIntPtr ptrBytesWritten;
+                using (ProcessMemoryChunk mem = new ProcessMemoryChunk(p, (IntPtr)address, bitData.Length))
+                {
+                    mem.Write(0, bitData);
+                }
             }
             //if (ptrBytesWritten.ToUInt32() == 0)
             //    return false;
@@ -684,11 +717,14 @@
         #region public virtual bool PatchMemory(uint address, long value)
         public virtual bool PatchMemory(uint address, long value)
         {
-            byte[] bitData = BitConverter.GetBytes(value);
-            //UIntPtr ptrBytesWritten;
-            using (ProcessMemoryChunk mem = new ProcessMemoryChunk(m_ReadProcess, (IntPtr)address, bitData.Length))
+            using (Process p = Process.GetProcessById(m_ProcessPID))
             {
-                mem.Write(0, bitData);
+                byte[] bitData = BitConverter.GetBytes(value);
+                //UIntPtr ptrBytesWritten;
+                using (ProcessMemoryChunk mem = new ProcessMemoryChunk(p, (IntPtr)address, bitData.Length))
+                {
+                    mem.Write(0, bitData);
+                }
             }
             //if (ptrBytesWritten.ToUInt32() == 0)
             //    return false;
@@ -710,10 +746,13 @@
                 //int bytesRead;
                 uint size = sizeof(byte);
                 byte[] bitData = new byte[size];
-                using (ProcessMemoryChunk mem = new ProcessMemoryChunk(m_ReadProcess, (IntPtr)address, (int)size))
+                using (Process p = Process.GetProcessById(m_ProcessPID))
                 {
-                    mem.Read(0, (int)size);
-                }                
+                    using (ProcessMemoryChunk mem = new ProcessMemoryChunk(p, (IntPtr)address, (int)size))
+                    {
+                        mem.Read(0, (int)size);
+                    }
+                }
                 //if (bytesRead == 0)
                 //    return false;
                 value = bitData[0];
@@ -735,10 +774,13 @@
                 //int bytesRead;
                 uint size = sizeof(sbyte);
                 byte[] bitData = new byte[size];
-                using (ProcessMemoryChunk mem = new ProcessMemoryChunk(m_ReadProcess, (IntPtr)address, (int)size))
+                using (Process p = Process.GetProcessById(m_ProcessPID))
                 {
-                    mem.Read(0, (int)size);
-                }  
+                    using (ProcessMemoryChunk mem = new ProcessMemoryChunk(p, (IntPtr)address, (int)size))
+                    {
+                        mem.Read(0, (int)size);
+                    }
+                }
                 //if (bytesRead == 0)
                 //    return false;
                 value = Convert.ToSByte(bitData[0]);
@@ -760,10 +802,13 @@
                 //int bytesRead;
                 uint size = sizeof(ushort);
                 byte[] bitData = new byte[size];
-                using (ProcessMemoryChunk mem = new ProcessMemoryChunk(m_ReadProcess, (IntPtr)address, (int)size))
+                using (Process p = Process.GetProcessById(m_ProcessPID))
                 {
-                    mem.Read(0, (int)size);
-                }  
+                    using (ProcessMemoryChunk mem = new ProcessMemoryChunk(p, (IntPtr)address, (int)size))
+                    {
+                        mem.Read(0, (int)size);
+                    }
+                }
                 //if (bytesRead == 0)
                 //    return false;
                 value = BitConverter.ToUInt16(bitData, 0);
@@ -785,10 +830,13 @@
                 //int bytesRead;
                 uint size = sizeof(short);
                 byte[] bitData = new byte[size];
-                using (ProcessMemoryChunk mem = new ProcessMemoryChunk(m_ReadProcess, (IntPtr)address, (int)size))
+                using (Process p = Process.GetProcessById(m_ProcessPID))
                 {
-                    mem.Read(0, (int)size);
-                }  
+                    using (ProcessMemoryChunk mem = new ProcessMemoryChunk(p, (IntPtr)address, (int)size))
+                    {
+                        mem.Read(0, (int)size);
+                    }
+                }
                 //if (bytesRead == 0)
                 //    return false;
                 value = BitConverter.ToInt16(bitData, 0);
@@ -810,10 +858,13 @@
                 //int bytesRead;
                 uint size = sizeof(uint);
                 byte[] bitData = new byte[size];
-                using (ProcessMemoryChunk mem = new ProcessMemoryChunk(m_ReadProcess, (IntPtr)address, (int)size))
+                using (Process p = Process.GetProcessById(m_ProcessPID))
                 {
-                    mem.Read(0, (int)size);
-                }  
+                    using (ProcessMemoryChunk mem = new ProcessMemoryChunk(p, (IntPtr)address, (int)size))
+                    {
+                        mem.Read(0, (int)size);
+                    }
+                }
                 //if (bytesRead == 0)
                 //    return false;
                 value = BitConverter.ToUInt32(bitData, 0);
@@ -835,10 +886,13 @@
                 //int bytesRead;
                 uint size = sizeof(int);
                 byte[] bitData = new byte[size];
-                using (ProcessMemoryChunk mem = new ProcessMemoryChunk(m_ReadProcess, (IntPtr)address, (int)size))
+                using (Process p = Process.GetProcessById(m_ProcessPID))
                 {
-                    mem.Read(0, (int)size);
-                }  
+                    using (ProcessMemoryChunk mem = new ProcessMemoryChunk(p, (IntPtr)address, (int)size))
+                    {
+                        mem.Read(0, (int)size);
+                    }
+                }
                 //if (bytesRead == 0)
                 //    return false;
                 value = BitConverter.ToInt32(bitData, 0);
@@ -860,10 +914,13 @@
                 //int bytesRead;
                 uint size = sizeof(ulong);
                 byte[] bitData = new byte[size];
-                using (ProcessMemoryChunk mem = new ProcessMemoryChunk(m_ReadProcess, (IntPtr)address, (int)size))
+                using (Process p = Process.GetProcessById(m_ProcessPID))
                 {
-                    mem.Read(0, (int)size);
-                }  
+                    using (ProcessMemoryChunk mem = new ProcessMemoryChunk(p, (IntPtr)address, (int)size))
+                    {
+                        mem.Read(0, (int)size);
+                    }
+                }
                 //if (bytesRead == 0)
                 //    return false;
                 value = BitConverter.ToUInt64(bitData, 0);
@@ -885,10 +942,13 @@
                // int bytesRead;
                 uint size = sizeof(long);
                 byte[] bitData = new byte[size];
-                using (ProcessMemoryChunk mem = new ProcessMemoryChunk(m_ReadProcess, (IntPtr)address, (int)size))
+                using (Process p = Process.GetProcessById(m_ProcessPID))
                 {
-                    mem.Read(0, (int)size);
-                }  
+                    using (ProcessMemoryChunk mem = new ProcessMemoryChunk(p, (IntPtr)address, (int)size))
+                    {
+                        mem.Read(0, (int)size);
+                    }
+                }
                 //if (bytesRead == 0)
                 //    return false;
                 value = BitConverter.ToInt64(bitData, 0);

 

  ViewVC Help
Powered by ViewVC 1.1.22