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 408 by william, Thu Jun 21 18:10:21 2012 UTC vs.
Revision 409 by william, Thu Jun 21 20:02:40 2012 UTC

--- trunk/Win32/Sojaner.MemoryScanner/MemoryScanner.cs	2012/06/21 18:10:21	408
+++ trunk/Win32/Sojaner.MemoryScanner/MemoryScanner.cs	2012/06/21 20:02:40	409
@@ -88,8 +88,8 @@ namespace Sojaner.MemoryScanner
         private Process m_ReadProcess = null;
 
         //SafeWaitHandle handle;
-        private static IntPtr m_hProcess = IntPtr.Zero;
-
+        private static IntPtr handle = IntPtr.Zero;
+        SafeWaitHandle m_hProcess;
         public void OpenProcess()
         {
             try
@@ -104,7 +104,8 @@ namespace Sojaner.MemoryScanner
                     | ProcessAccessType.PROCESS_VM_WRITE
                     | ProcessAccessType.PROCESS_VM_OPERATION;
                 //m_hProcess = ProcessMemoryReaderApi.OpenProcess((uint)access, 1, (uint)m_ReadProcess.Id);
-               m_hProcess = ProcessMemoryReaderApi.OpenProcess((uint)access, 1, (uint)m_ReadProcess.Id);
+                handle = ProcessMemoryReaderApi.OpenProcess((uint)access, 1, (uint)m_ReadProcess.Id);
+                m_hProcess = new SafeWaitHandle(handle, false);
             }
             catch (SEHException ex)
             {
@@ -123,12 +124,13 @@ namespace Sojaner.MemoryScanner
         {
             try
             {
-                SafeWaitHandle handle = new SafeWaitHandle(m_ReadProcess.Handle, false);
-                if (handle.IsInvalid) { return; }
-                if (handle.IsClosed) { return; }
-                handle.Close();
-                handle = null;
-                //m_hProcess = IntPtr.Zero;
+                //if (handle.IsInvalid) { return; }
+                //if (handle.IsClosed) { return; }
+                m_hProcess.Close();
+                m_hProcess.Dispose();
+                m_hProcess = null;
+                handle = IntPtr.Zero;
+                m_ReadProcess = null;
                 //string stack_trace = System.Environment.StackTrace;
                 //int iRetValue;
                 //iRetValue = ProcessMemoryReaderApi.CloseHandle(m_hProcess);
@@ -297,13 +299,14 @@ namespace Sojaner.MemoryScanner
             uint size = 1024 * 128;
             for (uint j = MemoryAddress; j < bytesToRead; j += size)
             {
-                ProcessMemoryChunk mem = new ProcessMemoryChunk(m_ReadProcess, (IntPtr)j, (int)size);
-                byte[] bigMem = mem.Read();
-                if (this.OnBytesRead != null)
-                    this.OnBytesRead.Invoke(new OnBytesReadEventArgs(this, UserState, bigMem, j, bytesToRead));
-                bigMem = null;
+                using (ProcessMemoryChunk mem = new ProcessMemoryChunk(m_ReadProcess, (IntPtr)j, (int)size))
+                {
+                    byte[] bigMem = mem.Read();
+                    if (this.OnBytesRead != null)
+                        this.OnBytesRead.Invoke(new OnBytesReadEventArgs(this, UserState, bigMem, j, bytesToRead));
+                    bigMem = null;
+                }
             }
-            
         }
         public void ReadProcessMemoryAtOnce(uint MemoryAddress, uint bytesToRead, out int bytesRead, out byte[] data)
         {
@@ -313,10 +316,12 @@ namespace Sojaner.MemoryScanner
             List<byte> buffer_list = new List<byte>();
             for (uint j = MemoryAddress; j < bytesToRead; j += size)
             {
-                ProcessMemoryChunk mem = new ProcessMemoryChunk(m_ReadProcess, (IntPtr)j, (int)size);
-                byte[] bigMem = mem.Read();
-                foreach (byte b in bigMem) { buffer_list.Add(b); }
-                bigMem = null;
+                using (ProcessMemoryChunk mem = new ProcessMemoryChunk(m_ReadProcess, (IntPtr)j, (int)size))
+                {
+                    byte[] bigMem = mem.Read();
+                    foreach (byte b in bigMem) { buffer_list.Add(b); }
+                    bigMem = null;
+                }
             }
             bytesRead = buffer_list.Count;
             data = new byte[bytesToRead];
@@ -363,7 +368,7 @@ namespace Sojaner.MemoryScanner
                         buffer_list.RemoveAt(buffer_list.Count-1);
                     }
 
-                    ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(m_ReadProcess.Handle, (IntPtr)_MemoryAddress, buffer, alignment, out _bytesRead);
+                    ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(handle, (IntPtr)_MemoryAddress, buffer, alignment, out _bytesRead);
                     int LastError = Marshal.GetLastWin32Error();
                     if (LastError != ResultWin32.ERROR_SUCCESS)
                     {
@@ -415,7 +420,7 @@ namespace Sojaner.MemoryScanner
                 {
                     UIntPtr _ptrBytesWritten = UIntPtr.Zero;
                     byte[] buffer = new byte[] { bytesToWrite[i] };
-                    ProcessMemoryReaderApi.WriteProcessMemory(m_ReadProcess.Handle, (IntPtr)_MemoryAddress, buffer, (uint)buffer.Length, out _ptrBytesWritten);
+                    ProcessMemoryReaderApi.WriteProcessMemory(handle, (IntPtr)_MemoryAddress, buffer, (uint)buffer.Length, out _ptrBytesWritten);
                     _MemoryAddress++;
                     _bytesWritten++;
                 }
@@ -521,7 +526,7 @@ namespace Sojaner.MemoryScanner
         {
             byte[] bitData = BitConverter.GetBytes(value);
             UIntPtr ptrBytesWritten;
-            ProcessMemoryReaderApi.WriteProcessMemory(m_ReadProcess.Handle, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten);
+            ProcessMemoryReaderApi.WriteProcessMemory(handle, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten);
             if (ptrBytesWritten.ToUInt32() == 0)
                 return false;
             byte check = 0;
@@ -535,7 +540,7 @@ namespace Sojaner.MemoryScanner
         {
             byte[] bitData = BitConverter.GetBytes(value);
             UIntPtr ptrBytesWritten;
-            ProcessMemoryReaderApi.WriteProcessMemory(m_ReadProcess.Handle, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten);
+            ProcessMemoryReaderApi.WriteProcessMemory(handle, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten);
             if (ptrBytesWritten.ToUInt32() == 0)
                 return false;
             sbyte check = 0;
@@ -549,7 +554,7 @@ namespace Sojaner.MemoryScanner
         {
             byte[] bitData = BitConverter.GetBytes(value);
             UIntPtr ptrBytesWritten;
-            ProcessMemoryReaderApi.WriteProcessMemory(m_ReadProcess.Handle, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten);
+            ProcessMemoryReaderApi.WriteProcessMemory(handle, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten);
             if (ptrBytesWritten.ToUInt32() == 0)
                 return false;
             ushort check = 0;
@@ -563,7 +568,7 @@ namespace Sojaner.MemoryScanner
         {           
             byte[] bitData = BitConverter.GetBytes(value);
             UIntPtr ptrBytesWritten;
-            ProcessMemoryReaderApi.WriteProcessMemory(m_ReadProcess.Handle, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten);
+            ProcessMemoryReaderApi.WriteProcessMemory(handle, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten);
             if (ptrBytesWritten.ToUInt32() == 0)
                 return false;
             short check = 0;
@@ -577,7 +582,7 @@ namespace Sojaner.MemoryScanner
         {
             byte[] bitData = BitConverter.GetBytes(value);
             UIntPtr ptrBytesWritten;
-            ProcessMemoryReaderApi.WriteProcessMemory(m_ReadProcess.Handle, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten);
+            ProcessMemoryReaderApi.WriteProcessMemory(handle, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten);
             if (ptrBytesWritten.ToUInt32() == 0)
                 return false;
             uint check = 0;
@@ -591,7 +596,7 @@ namespace Sojaner.MemoryScanner
         {
             byte[] bitData = BitConverter.GetBytes(value);
             UIntPtr ptrBytesWritten;
-            ProcessMemoryReaderApi.WriteProcessMemory(m_ReadProcess.Handle, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten);
+            ProcessMemoryReaderApi.WriteProcessMemory(handle, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten);
             if (ptrBytesWritten.ToUInt32() == 0)
                 return false;
             int check = 0;
@@ -605,7 +610,7 @@ namespace Sojaner.MemoryScanner
         {
             byte[] bitData = BitConverter.GetBytes(value);
             UIntPtr ptrBytesWritten;
-            ProcessMemoryReaderApi.WriteProcessMemory(m_ReadProcess.Handle, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten);
+            ProcessMemoryReaderApi.WriteProcessMemory(handle, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten);
             if (ptrBytesWritten.ToUInt32() == 0)
                 return false;
             ulong check = 0;
@@ -619,7 +624,7 @@ namespace Sojaner.MemoryScanner
         {
             byte[] bitData = BitConverter.GetBytes(value);
             UIntPtr ptrBytesWritten;
-            ProcessMemoryReaderApi.WriteProcessMemory(m_ReadProcess.Handle, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten);
+            ProcessMemoryReaderApi.WriteProcessMemory(handle, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten);
             if (ptrBytesWritten.ToUInt32() == 0)
                 return false;
             long check = 0;
@@ -640,7 +645,7 @@ namespace Sojaner.MemoryScanner
                 int bytesRead;
                 uint size = sizeof(byte);
                 byte[] bitData = new byte[size];
-                ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(m_ReadProcess.Handle, (IntPtr)address, bitData, size, out bytesRead);
+                ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(handle, (IntPtr)address, bitData, size, out bytesRead);
                 if (bytesRead == 0)
                     return false;
                 value = bitData[0];
@@ -662,7 +667,7 @@ namespace Sojaner.MemoryScanner
                 int bytesRead;
                 uint size = sizeof(sbyte);
                 byte[] bitData = new byte[size];
-                ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(m_ReadProcess.Handle, (IntPtr)address, bitData, size, out bytesRead);
+                ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(handle, (IntPtr)address, bitData, size, out bytesRead);
                 if (bytesRead == 0)
                     return false;
                 value = Convert.ToSByte(bitData[0]);
@@ -684,7 +689,7 @@ namespace Sojaner.MemoryScanner
                 int bytesRead;
                 uint size = sizeof(ushort);
                 byte[] bitData = new byte[size];
-                ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(m_ReadProcess.Handle, (IntPtr)address, bitData, size, out bytesRead);
+                ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(handle, (IntPtr)address, bitData, size, out bytesRead);
                 if (bytesRead == 0)
                     return false;
                 value = BitConverter.ToUInt16(bitData, 0);
@@ -706,7 +711,7 @@ namespace Sojaner.MemoryScanner
                 int bytesRead;
                 uint size = sizeof(short);
                 byte[] bitData = new byte[size];
-                ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(m_ReadProcess.Handle, (IntPtr)address, bitData, size, out bytesRead);
+                ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(handle, (IntPtr)address, bitData, size, out bytesRead);
                 if (bytesRead == 0)
                     return false;
                 value = BitConverter.ToInt16(bitData, 0);
@@ -728,7 +733,7 @@ namespace Sojaner.MemoryScanner
                 int bytesRead;
                 uint size = sizeof(uint);
                 byte[] bitData = new byte[size];
-                ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(m_ReadProcess.Handle, (IntPtr)address, bitData, size, out bytesRead);
+                ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(handle, (IntPtr)address, bitData, size, out bytesRead);
                 if (bytesRead == 0)
                     return false;
                 value = BitConverter.ToUInt32(bitData, 0);
@@ -750,7 +755,7 @@ namespace Sojaner.MemoryScanner
                 int bytesRead;
                 uint size = sizeof(int);
                 byte[] bitData = new byte[size];
-                ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(m_ReadProcess.Handle, (IntPtr)address, bitData, size, out bytesRead);
+                ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(handle, (IntPtr)address, bitData, size, out bytesRead);
                 if (bytesRead == 0)
                     return false;
                 value = BitConverter.ToInt32(bitData, 0);
@@ -772,7 +777,7 @@ namespace Sojaner.MemoryScanner
                 int bytesRead;
                 uint size = sizeof(ulong);
                 byte[] bitData = new byte[size];
-                ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(m_ReadProcess.Handle, (IntPtr)address, bitData, size, out bytesRead);
+                ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(handle, (IntPtr)address, bitData, size, out bytesRead);
                 if (bytesRead == 0)
                     return false;
                 value = BitConverter.ToUInt64(bitData, 0);
@@ -794,7 +799,7 @@ namespace Sojaner.MemoryScanner
                 int bytesRead;
                 uint size = sizeof(long);
                 byte[] bitData = new byte[size];
-                ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(m_ReadProcess.Handle, (IntPtr)address, bitData, size, out bytesRead);
+                ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(handle, (IntPtr)address, bitData, size, out bytesRead);
                 if (bytesRead == 0)
                     return false;
                 value = BitConverter.ToInt64(bitData, 0);