/[RomCheater]/trunk/Win32/Sojaner.MemoryScanner/MemoryScanner.cs
ViewVC logotype

Contents of /trunk/Win32/Sojaner.MemoryScanner/MemoryScanner.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 415 - (show annotations) (download)
Fri Jun 22 08:33:51 2012 UTC (7 years, 7 months ago) by william
File size: 34840 byte(s)

1 #region Logging Defines
2 // include this any class or method that required logging, and comment-out what is not needed
3
4 #region Enabled logging levels
5 #define LOGGING_ENABLE_INFO
6 #define LOGGING_ENABLE_WARN
7 #define LOGGING_ENABLE_DEBUG
8 #define LOGGING_ENABLE_VERBOSEDEBUG
9 #define LOGGING_ENABLE_ERROR
10 #define LOGGING_ENABLE_VERBOSEERROR
11 #define LOGGING_ENABLE_PROFILER
12 #endregion
13 #endregion
14 using System;
15 using System.Collections.Generic;
16 using System.Text;
17 using System.Diagnostics;
18 using System.Threading;
19 using System.Runtime.InteropServices;
20 using RomCheater.Logging;
21 using System.IO;
22 using Sojaner.MemoryScanner.MemoryProviers;
23 using Microsoft.Win32.SafeHandles;
24 using Microsoft.Win32.Interop;
25 using System.ComponentModel;
26 using ManagedWinapi;
27 using RomCheater.PluginFramework.Events;
28
29 namespace Sojaner.MemoryScanner
30 {
31 // code borrowed from: http://www.codeproject.com/KB/cs/sojaner_memory_scanner.aspx
32 #region ProcessMemoryReader class
33 //Thanks goes to Arik Poznanski for P/Invokes and methods needed to read and write the Memory
34 //For more information refer to "Minesweeper, Behind the scenes" article by Arik Poznanski at Codeproject.com
35 internal class ProcessMemoryReader : IMemoryReader, IMemoryWriter, IFileWriter, IPatchMemory,IReadMemory, IAcceptsBytesReadEvent
36 {
37 // constants information can be found in <winnt.h>
38 [Flags]
39 enum ProcessAccessFlags : uint
40 {
41 All = 0x001F0FFF,
42 Terminate = 0x00000001,
43 CreateThread = 0x00000002,
44 VMOperation = 0x00000008,
45 VMRead = 0x00000010,
46 VMWrite = 0x00000020,
47 DupHandle = 0x00000040,
48 SetInformation = 0x00000200,
49 QueryInformation = 0x00000400,
50 Synchronize = 0x00100000
51 }
52 [Flags]
53 public enum AllocationProtect : uint
54 {
55 // http://msdn.microsoft.com/en-us/library/windows/desktop/aa366786(v=vs.85).aspx
56 NONE = 0,
57 PAGE_NOACCESS = 0x00000001,
58 PAGE_READONLY = 0x00000002,
59 PAGE_READWRITE = 0x00000004,
60 PAGE_WRITECOPY = 0x00000008,
61 PAGE_EXECUTE = 0x00000010,
62 PAGE_EXECUTE_READ = 0x00000020,
63 PAGE_EXECUTE_READWRITE = 0x00000040,
64 PAGE_EXECUTE_WRITECOPY = 0x00000080,
65 PAGE_GUARD = 0x00000100,
66 PAGE_NOCACHE = 0x00000200,
67 PAGE_WRITECOMBINE = 0x00000400
68 }
69 [StructLayout(LayoutKind.Sequential)]
70 public struct MEMORY_BASIC_INFORMATION
71 {
72 public IntPtr BaseAddress;
73 public IntPtr AllocationBase;
74 public AllocationProtect AllocationProtect;
75 public IntPtr RegionSize;
76 public uint State;
77 public uint Protect;
78 public uint Type;
79 }
80
81 public ProcessMemoryReader()
82 {
83 }
84 public event BaseEventHandler<OnBytesReadEventArgs> OnBytesRead;
85 /// <summary>
86 /// Process from which to read
87 /// </summary>
88 public Process ReadProcess
89 {
90 get
91 {
92 return m_ReadProcess;
93 }
94 set
95 {
96 m_ReadProcess = value;
97 }
98 }
99
100 private Process m_ReadProcess = null;
101
102 //SafeWaitHandle handle;
103 private static IntPtr handle = IntPtr.Zero;
104 SafeWaitHandle m_hProcess;
105 public void OpenProcess()
106 {
107 try
108 {
109 // m_hProcess = ProcessMemoryReaderApi.OpenProcess(ProcessMemoryReaderApi.PROCESS_VM_READ, 1, (uint)m_ReadProcess.Id);
110 //if (!TokenAdjuster.AdjustProcessTokenPrivileges("SeDebugPrivilege"))
111 //{
112 // logger.Warn.WriteLine("Failed to set SeDebugPrivilege on current process");
113 //}
114 ProcessAccessFlags access;
115 access = ProcessAccessFlags.VMRead
116 | ProcessAccessFlags.VMWrite
117 | ProcessAccessFlags.VMOperation;
118 //m_hProcess = ProcessMemoryReaderApi.OpenProcess((uint)access, 1, (uint)m_ReadProcess.Id);
119 handle = ProcessMemoryReaderApi.OpenProcess(access, true, m_ReadProcess.Id);
120 //m_hProcess = new SafeWaitHandle(handle, false);
121 }
122 catch (SEHException ex)
123 {
124 logger.Error.WriteLine("WriteProcessMemoryToFile() SEHException was thrown: (0x{0:x8}) - {1}", ex.ErrorCode, ex.Message);
125 logger.Error.WriteLine(ex.ToString());
126 throw ex;
127 }
128 catch (Exception ex)
129 {
130 logger.Error.WriteLine(ex.ToString());
131 throw ex;
132 }
133 }
134
135 public void CloseHandle()
136 {
137 try
138 {
139 ////if (handle.IsInvalid) { return; }
140 ////if (handle.IsClosed) { return; }
141 //m_hProcess.Close();
142 //m_hProcess.Dispose();
143 //m_hProcess = null;
144 //handle = IntPtr.Zero;
145 //m_ReadProcess = null;
146 //string stack_trace = System.Environment.StackTrace;
147 int iRetValue;
148 iRetValue = ProcessMemoryReaderApi.CloseHandle(handle);
149 handle = IntPtr.Zero;
150 ReadProcess = null;
151 if (iRetValue == 0)
152 {
153 throw new Exception("CloseHandle failed");
154 }
155 }
156 catch (SEHException ex)
157 {
158 logger.Error.WriteLine("WriteProcessMemoryToFile() SEHException was thrown: (0x{0:x8}) - {1}", ex.ErrorCode, ex.Message);
159 logger.Error.WriteLine(ex.ToString());
160 throw ex;
161 }
162 catch (Exception ex)
163 {
164 logger.Error.WriteLine(ex.ToString());
165 throw ex;
166 }
167 }
168 /// <summary>
169 /// ProcessMemoryReader is a class that enables direct reading a process memory
170 /// </summary>
171 private class ProcessMemoryReaderApi
172 {
173 [DllImport("kernel32.dll")]
174 public static extern int VirtualQueryEx(IntPtr hProcess, IntPtr lpAddress, out MEMORY_BASIC_INFORMATION lpBuffer, uint dwLength);
175
176 // function declarations are found in the MSDN and in <winbase.h>
177
178 // HANDLE OpenProcess(
179 // DWORD dwDesiredAccess, // access flag
180 // BOOL bInheritHandle, // handle inheritance option
181 // DWORD dwProcessId // process identifier
182 // );
183 [DllImport("kernel32.dll")]
184 public static extern IntPtr OpenProcess(ProcessAccessFlags dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, int dwProcessId);
185
186 // BOOL CloseHandle(
187 // HANDLE hObject // handle to object
188 // );
189 [DllImport("kernel32.dll")]
190 public static extern Int32 CloseHandle(IntPtr hObject);
191
192 // BOOL ReadProcessMemory(
193 // HANDLE hProcess, // handle to the process
194 // LPCVOID lpBaseAddress, // base of memory area
195 // LPVOID lpBuffer, // data buffer
196 // SIZE_T nSize, // number of bytes to read
197 // SIZE_T * lpNumberOfBytesRead // number of bytes read
198 // );
199 [DllImport("kernel32.dll", SetLastError = true)]
200 public static extern bool ReadProcessMemory(
201 IntPtr hProcess,
202 IntPtr lpBaseAddress,
203 [Out] byte[] lpBuffer,
204 uint dwSize,
205 out int lpNumberOfBytesRead
206 );
207
208 // BOOL WriteProcessMemory(
209 // HANDLE hProcess, // handle to process
210 // LPVOID lpBaseAddress, // base of memory area
211 // LPCVOID lpBuffer, // data buffer
212 // SIZE_T nSize, // count of bytes to write
213 // SIZE_T * lpNumberOfBytesWritten // count of bytes written
214 // );
215 [DllImport("kernel32.dll", SetLastError = true)]
216 public static extern bool WriteProcessMemory(
217 IntPtr hProcess,
218 IntPtr lpBaseAddress,
219 byte[] lpBuffer,
220 uint nSize,
221 out UIntPtr lpNumberOfBytesWritten);
222
223
224 }
225
226 #region IMemoryReader Members
227 #region public bool ReadFirstNonZeroByte(uint MemoryAddress, uint bytesToRead, out int address)
228 public bool ReadFirstNonZeroByte(int MemoryAddress, uint bytesToRead, out int address)
229 {
230 //logger.Info.WriteLine("Dumping memory (0x{0:x8}-0x{1:x8}) from pid=({2})", MemoryAddress, MemoryAddress + bytesToRead, string.Format("0x{0:x4} {1}.exe", ppid.Id, ppid.ProcessName));
231 address = 0;
232 uint byte_alignment = 1;
233 // get common init parameters
234 //InitMemoryDump(out byte_alignment);
235 int mem_address = MemoryAddress;
236 uint _bytesToRead = bytesToRead;
237 byte[] buffer = new byte[] { };
238 try
239 {
240 //using (MemoryStream ms = new MemoryStream())
241 //{
242 // //BinaryWriter bw = new BinaryWriter(ms);
243 // //foreach (byte b in data) { bw.Write(b); }
244 for (uint i = 0; i <= bytesToRead; )
245 {
246 if (_bytesToRead < byte_alignment)
247 {
248 _bytesToRead = bytesToRead;
249 buffer = new byte[_bytesToRead];
250 }
251 else
252 {
253 _bytesToRead = byte_alignment;
254 buffer = new byte[byte_alignment];
255 }
256 int bytesRead = 0;
257 ReadProcessMemory(mem_address, _bytesToRead, out bytesRead, out buffer);
258 if (buffer.Length == 0 && bytesRead == 0)
259 {
260 throw new Exception(string.Format("Failed to read memory from process: {0}", ReadProcess.ToString()));
261 }
262 //bw.Write(buffer);
263 //bw.Flush();
264 if (_bytesToRead < byte_alignment)
265 {
266 i += _bytesToRead;
267 mem_address += (int)_bytesToRead;
268 }
269 else
270 {
271 i += byte_alignment;
272 mem_address += (int)byte_alignment;
273 }
274 for (uint j = 0; j < buffer.Length; j++)
275 {
276 if (buffer[j] != 0)
277 {
278 address = mem_address;
279 break;
280 }
281 }
282 if (address != 0)
283 break;
284 }
285 // bw.Close();
286 //}
287 //logger.Info.WriteLine("Succefully dumped memory (0x{0:x8}-0x{1:x8}) from pid=({2})", MemoryAddress, MemoryAddress + bytesToRead, string.Format("0x{0:x4} {1}.exe", ppid.Id, ppid.ProcessName));
288 return true;
289 }
290 catch (SEHException ex)
291 {
292 logger.Error.WriteLine("ReadFirstNonZeroByte() SEHException was thrown: (0x{0:x8}) - {1}", ex.ErrorCode, ex.Message);
293 logger.Error.WriteLine(ex.ToString());
294 throw ex;
295 }
296 catch (OutOfMemoryException ex)
297 {
298 logger.Error.WriteLine("ReadFirstNonZeroByte(): OutOfMemoryException");
299 logger.Error.WriteLine(ex.ToString());
300 }
301 catch (Exception ex)
302 {
303 logger.Error.WriteLine("ReadFirstNonZeroByte(): Exception");
304 logger.Error.WriteLine(ex.ToString());
305 throw ex;
306 }
307 return false;
308 }
309 #endregion
310
311 public void ReadProcessMemoryAtOnce(uint MemoryAddress, uint bytesToRead,object UserState)
312 {
313 uint size = 1024 * 128;
314 for (uint j = MemoryAddress; j < bytesToRead; j += size)
315 {
316 using (ProcessMemoryChunk mem = new ProcessMemoryChunk(m_ReadProcess, (IntPtr)j, (int)size))
317 {
318 byte[] bigMem = mem.Read();
319 if (this.OnBytesRead != null)
320 this.OnBytesRead.Invoke(new OnBytesReadEventArgs(this, UserState, bigMem, j, bytesToRead));
321 bigMem = null;
322 }
323 }
324 }
325 public void ReadProcessMemoryAtOnce(uint MemoryAddress, uint bytesToRead, out int bytesRead, out byte[] data)
326 {
327 Stopwatch st = new Stopwatch();
328 st.Start();
329 uint size = 1024 * 64;
330 List<byte> buffer_list = new List<byte>();
331 for (uint j = MemoryAddress; j < bytesToRead; j += size)
332 {
333 using (ProcessMemoryChunk mem = new ProcessMemoryChunk(m_ReadProcess, (IntPtr)j, (int)size))
334 {
335 byte[] bigMem = mem.Read();
336 foreach (byte b in bigMem) { buffer_list.Add(b); }
337 bigMem = null;
338 }
339 }
340 bytesRead = buffer_list.Count;
341 data = new byte[bytesToRead];
342 data.Initialize();
343 Buffer.BlockCopy(buffer_list.ToArray(), 0, data, 0, (int)bytesToRead);
344 st.Stop();
345 logger.Profiler.WriteLine("ReadProcessMemoryAtOnce(): start=0x{0:x8} end=0x{1:x8} took {2:0.0000} seconds", MemoryAddress, MemoryAddress + bytesToRead, st.Elapsed.TotalSeconds);
346 }
347
348 #region public void ReadProcessMemory(int MemoryAddress, uint bytesToRead, out int bytesRead, out byte[] data)
349 public void ReadProcessMemory(int MemoryAddress, uint bytesToRead, out int bytesRead, out byte[] data)
350 {
351 ReadProcessMemory((uint)MemoryAddress, bytesToRead, out bytesRead, out data);
352 }
353 public void ReadProcessMemory(uint MemoryAddress, uint bytesToRead, out int bytesRead, out byte[] data)
354 {
355 try
356 {
357 bytesRead = 0;
358 data = new byte[bytesToRead];
359 const int alignment = 1;
360 List<byte> buffer_list = new List<byte>();
361 uint _MemoryAddress = MemoryAddress;
362 for (int i = 0; i < bytesToRead; i++)
363 {
364 byte[] buffer = new byte[alignment];
365 int _bytesRead = 0;
366 MEMORY_BASIC_INFORMATION m;
367 ProcessMemoryReaderApi.VirtualQueryEx(m_ReadProcess.Handle, (IntPtr)_MemoryAddress, out m, (uint)Marshal.SizeOf(typeof(MEMORY_BASIC_INFORMATION)));
368
369 if (m.AllocationProtect.HasFlag(AllocationProtect.PAGE_NOACCESS) ||
370 m.AllocationProtect == 0)
371 {
372 uint OrignalAddress = _MemoryAddress;
373 while (m.AllocationProtect.HasFlag(AllocationProtect.PAGE_NOACCESS) || m.AllocationProtect == 0)
374 {
375 ProcessMemoryReaderApi.VirtualQueryEx(m_ReadProcess.Handle, (IntPtr)_MemoryAddress, out m, (uint)Marshal.SizeOf(typeof(MEMORY_BASIC_INFORMATION)));
376 _MemoryAddress++;
377 buffer_list.Add(0);
378 }
379 _MemoryAddress--;
380 uint diff = _MemoryAddress - OrignalAddress;
381 i += (int)diff;
382 buffer_list.RemoveAt(buffer_list.Count-1);
383 }
384
385 ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(handle, (IntPtr)_MemoryAddress, buffer, alignment, out _bytesRead);
386 int LastError = Marshal.GetLastWin32Error();
387 if (LastError != ResultWin32.ERROR_SUCCESS)
388 {
389 string ErrorName = ResultWin32.GetErrorName(LastError);
390 }
391 if (_bytesRead > 0)
392 {
393 foreach (byte b in buffer)
394 {
395 buffer_list.Add(b);
396 }
397 _MemoryAddress += (uint)buffer.Length;
398 }
399 }
400 bytesRead = buffer_list.Count;
401 Buffer.BlockCopy(buffer_list.ToArray(), 0, data, 0, (int)bytesToRead);
402 }
403 catch (SEHException ex)
404 {
405 logger.Error.WriteLine("ReadProcessMemory() SEHException was thrown: (0x{0:x8}) - {1}", ex.ErrorCode, ex.Message);
406 logger.Error.WriteLine(ex.ToString());
407 throw ex;
408 }
409 catch (Exception ex)
410 {
411 logger.Error.WriteLine("ReadProcessMemory(): Exception");
412 logger.Error.WriteLine(ex.ToString());
413 throw ex;
414 }
415 }
416 #endregion
417 #endregion
418
419 #region IMemoryWriter Members
420 #region public void WriteProcessMemory(uint MemoryAddress, byte[] bytesToWrite, out int bytesWritten)
421 public void WriteProcessMemory(int MemoryAddress, byte[] bytesToWrite, out int bytesWritten)
422 {
423 uint _bytesWritten = 0;
424 WriteProcessMemory((uint)MemoryAddress, bytesToWrite, out _bytesWritten);
425 bytesWritten = (int)_bytesWritten;
426 }
427 public void WriteProcessMemory(uint MemoryAddress, byte[] bytesToWrite, out uint bytesWritten)
428 {
429 try
430 {
431 uint _bytesWritten = 0;
432 uint _MemoryAddress = MemoryAddress;
433 for (int i = 0; i < bytesToWrite.Length; i++)
434 {
435 UIntPtr _ptrBytesWritten = UIntPtr.Zero;
436 byte[] buffer = new byte[] { bytesToWrite[i] };
437 ProcessMemoryReaderApi.WriteProcessMemory(handle, (IntPtr)_MemoryAddress, buffer, (uint)buffer.Length, out _ptrBytesWritten);
438 _MemoryAddress++;
439 _bytesWritten++;
440 }
441 bytesWritten = _bytesWritten;
442 }
443 catch (SEHException ex)
444 {
445 logger.Error.WriteLine("WriteProcessMemory() SEHException was thrown: (0x{0:x8}) - {1}", ex.ErrorCode, ex.Message);
446 logger.Error.WriteLine(ex.ToString());
447 throw ex;
448 }
449 catch (Exception ex)
450 {
451
452 logger.Error.WriteLine("WriteProcessMemory() Exception");
453 logger.Error.WriteLine(ex.ToString());
454 bytesWritten = 0;
455 throw ex;
456 }
457 }
458 #endregion
459 #endregion
460
461 #region IFileWriter Members
462 #region public bool WriteProcessMemoryToFile(string filename, uint MemoryAddress, uint bytesToRead, out int bytesRead)
463 public bool WriteProcessMemoryToFile(string filename, int MemoryAddress, uint bytesToRead, out int bytesRead)
464 {
465 //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));
466 bytesRead = 0;
467 uint byte_alignment = 102400;
468 int address = MemoryAddress;
469 uint _bytesToRead = bytesToRead;
470 byte[] buffer = new byte[] { };
471 try
472 {
473 FileInfo fi = new FileInfo(filename);
474 if (fi.Exists)
475 fi.Delete();
476 using (FileStream fs = new FileStream(filename, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite))
477 {
478 BinaryWriter bw = new BinaryWriter(fs);
479 //foreach (byte b in data) { bw.Write(b); }
480
481 for (uint i = 0; i <= bytesToRead; )
482 {
483 if (_bytesToRead < byte_alignment)
484 {
485 _bytesToRead = bytesToRead;
486 buffer = new byte[_bytesToRead];
487 }
488 else
489 {
490 _bytesToRead = byte_alignment;
491 buffer = new byte[byte_alignment];
492 }
493 ReadProcessMemory(address, _bytesToRead, out bytesRead, out buffer);
494 bw.Write(buffer);
495 bw.Flush();
496
497 if (_bytesToRead < byte_alignment)
498 {
499 i += _bytesToRead;
500 address += (int)_bytesToRead;
501 }
502 else
503 {
504 i += byte_alignment;
505 address += (int)byte_alignment;
506 }
507
508
509 }
510 bw.Close();
511 }
512 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));
513 return true;
514 }
515 catch (SEHException ex)
516 {
517 logger.Error.WriteLine("WriteProcessMemoryToFile() SEHException was thrown: (0x{0:x8}) - {1}", ex.ErrorCode, ex.Message);
518 logger.Error.WriteLine(ex.ToString());
519 throw ex;
520 }
521 catch (OutOfMemoryException ex)
522 {
523 logger.Error.WriteLine("WriteProcessMemoryToFile(): Exception");
524 logger.Error.WriteLine(ex.ToString());
525 }
526 catch (Exception ex)
527 {
528 logger.Error.WriteLine("WriteProcessMemoryToFile(): Exception");
529 logger.Error.WriteLine(ex.ToString());
530 throw ex;
531 }
532 return false;
533 }
534 #endregion
535 #endregion
536
537 #region IPatchMemory members
538 #region public virtual bool PatchMemory(uint address, byte value)
539 public virtual bool PatchMemory(uint address, byte value)
540 {
541 byte[] bitData = BitConverter.GetBytes(value);
542 UIntPtr ptrBytesWritten;
543 ProcessMemoryReaderApi.WriteProcessMemory(handle, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten);
544 if (ptrBytesWritten.ToUInt32() == 0)
545 return false;
546 byte check = 0;
547 ReadMemory(address, out check);
548 if (check == value) return true;
549 return false;
550 }
551 #endregion
552 #region public virtual bool PatchMemory(uint address, sbyte value)
553 public virtual bool PatchMemory(uint address, sbyte value)
554 {
555 byte[] bitData = BitConverter.GetBytes(value);
556 UIntPtr ptrBytesWritten;
557 ProcessMemoryReaderApi.WriteProcessMemory(handle, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten);
558 if (ptrBytesWritten.ToUInt32() == 0)
559 return false;
560 sbyte check = 0;
561 ReadMemory(address, out check);
562 if (check == value) return true;
563 return false;
564 }
565 #endregion
566 #region public virtual bool PatchMemory(uint address, ushort value)
567 public virtual bool PatchMemory(uint address, ushort value)
568 {
569 byte[] bitData = BitConverter.GetBytes(value);
570 UIntPtr ptrBytesWritten;
571 ProcessMemoryReaderApi.WriteProcessMemory(handle, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten);
572 if (ptrBytesWritten.ToUInt32() == 0)
573 return false;
574 ushort check = 0;
575 ReadMemory(address, out check);
576 if (check == value) return true;
577 return false;
578 }
579 #endregion
580 #region public virtual bool PatchMemory(uint address, short value)
581 public virtual bool PatchMemory(uint address, short value)
582 {
583 byte[] bitData = BitConverter.GetBytes(value);
584 UIntPtr ptrBytesWritten;
585 ProcessMemoryReaderApi.WriteProcessMemory(handle, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten);
586 if (ptrBytesWritten.ToUInt32() == 0)
587 return false;
588 short check = 0;
589 ReadMemory(address, out check);
590 if (check == value) return true;
591 return false;
592 }
593 #endregion
594 #region public virtual bool PatchMemory(uint address, uint value)
595 public virtual bool PatchMemory(uint address, uint value)
596 {
597 byte[] bitData = BitConverter.GetBytes(value);
598 UIntPtr ptrBytesWritten;
599 ProcessMemoryReaderApi.WriteProcessMemory(handle, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten);
600 if (ptrBytesWritten.ToUInt32() == 0)
601 return false;
602 uint check = 0;
603 ReadMemory(address, out check);
604 if (check == value) return true;
605 return false;
606 }
607 #endregion
608 #region public virtual bool PatchMemory(uint address, int value)
609 public virtual bool PatchMemory(uint address, int value)
610 {
611 byte[] bitData = BitConverter.GetBytes(value);
612 UIntPtr ptrBytesWritten;
613 ProcessMemoryReaderApi.WriteProcessMemory(handle, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten);
614 if (ptrBytesWritten.ToUInt32() == 0)
615 return false;
616 int check = 0;
617 ReadMemory(address, out check);
618 if (check == value) return true;
619 return false;
620 }
621 #endregion
622 #region public virtual bool PatchMemory(uint address, ulong value)
623 public virtual bool PatchMemory(uint address, ulong value)
624 {
625 byte[] bitData = BitConverter.GetBytes(value);
626 UIntPtr ptrBytesWritten;
627 ProcessMemoryReaderApi.WriteProcessMemory(handle, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten);
628 if (ptrBytesWritten.ToUInt32() == 0)
629 return false;
630 ulong check = 0;
631 ReadMemory(address, out check);
632 if (check == value) return true;
633 return false;
634 }
635 #endregion
636 #region public virtual bool PatchMemory(uint address, long value)
637 public virtual bool PatchMemory(uint address, long value)
638 {
639 byte[] bitData = BitConverter.GetBytes(value);
640 UIntPtr ptrBytesWritten;
641 ProcessMemoryReaderApi.WriteProcessMemory(handle, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten);
642 if (ptrBytesWritten.ToUInt32() == 0)
643 return false;
644 long check = 0;
645 ReadMemory(address, out check);
646 if (check == value) return true;
647 return false;
648 }
649 #endregion
650 #endregion
651
652 #region IReadMemory members
653 #region public virtual bool ReadMemory(uint address, out byte value)
654 public virtual bool ReadMemory(uint address, out byte value)
655 {
656 value = 0;
657 try
658 {
659 int bytesRead;
660 uint size = sizeof(byte);
661 byte[] bitData = new byte[size];
662 ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(handle, (IntPtr)address, bitData, size, out bytesRead);
663 if (bytesRead == 0)
664 return false;
665 value = bitData[0];
666 return true;
667 }
668 catch
669 {
670 value = 0x00;
671 return false;
672 }
673 }
674 #endregion
675 #region public virtual bool ReadMemory(uint address, out sbyte value)
676 public virtual bool ReadMemory(uint address, out sbyte value)
677 {
678 value = 0;
679 try
680 {
681 int bytesRead;
682 uint size = sizeof(sbyte);
683 byte[] bitData = new byte[size];
684 ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(handle, (IntPtr)address, bitData, size, out bytesRead);
685 if (bytesRead == 0)
686 return false;
687 value = Convert.ToSByte(bitData[0]);
688 return true;
689 }
690 catch
691 {
692 value = 0x00;
693 return false;
694 }
695 }
696 #endregion
697 #region public virtual bool ReadMemory(uint address, out ushort value)
698 public virtual bool ReadMemory(uint address, out ushort value)
699 {
700 value = 0;
701 try
702 {
703 int bytesRead;
704 uint size = sizeof(ushort);
705 byte[] bitData = new byte[size];
706 ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(handle, (IntPtr)address, bitData, size, out bytesRead);
707 if (bytesRead == 0)
708 return false;
709 value = BitConverter.ToUInt16(bitData, 0);
710 return true;
711 }
712 catch
713 {
714 value = 0x00;
715 return false;
716 }
717 }
718 #endregion
719 #region public virtual bool ReadMemory(uint address, out short value)
720 public virtual bool ReadMemory(uint address, out short value)
721 {
722 value = 0;
723 try
724 {
725 int bytesRead;
726 uint size = sizeof(short);
727 byte[] bitData = new byte[size];
728 ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(handle, (IntPtr)address, bitData, size, out bytesRead);
729 if (bytesRead == 0)
730 return false;
731 value = BitConverter.ToInt16(bitData, 0);
732 return true;
733 }
734 catch
735 {
736 value = 0x00;
737 return false;
738 }
739 }
740 #endregion
741 #region public virtual bool ReadMemory(uint address, out uint value)
742 public virtual bool ReadMemory(uint address, out uint value)
743 {
744 value = 0;
745 try
746 {
747 int bytesRead;
748 uint size = sizeof(uint);
749 byte[] bitData = new byte[size];
750 ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(handle, (IntPtr)address, bitData, size, out bytesRead);
751 if (bytesRead == 0)
752 return false;
753 value = BitConverter.ToUInt32(bitData, 0);
754 return true;
755 }
756 catch
757 {
758 value = 0x00;
759 return false;
760 }
761 }
762 #endregion
763 #region public virtual bool ReadMemory(uint address, out int value)
764 public virtual bool ReadMemory(uint address, out int value)
765 {
766 value = 0;
767 try
768 {
769 int bytesRead;
770 uint size = sizeof(int);
771 byte[] bitData = new byte[size];
772 ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(handle, (IntPtr)address, bitData, size, out bytesRead);
773 if (bytesRead == 0)
774 return false;
775 value = BitConverter.ToInt32(bitData, 0);
776 return true;
777 }
778 catch
779 {
780 value = 0x00;
781 return false;
782 }
783 }
784 #endregion
785 #region public virtual bool ReadMemory(uint address, out ulong value)
786 public virtual bool ReadMemory(uint address, out ulong value)
787 {
788 value = 0;
789 try
790 {
791 int bytesRead;
792 uint size = sizeof(ulong);
793 byte[] bitData = new byte[size];
794 ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(handle, (IntPtr)address, bitData, size, out bytesRead);
795 if (bytesRead == 0)
796 return false;
797 value = BitConverter.ToUInt64(bitData, 0);
798 return true;
799 }
800 catch
801 {
802 value = 0x00;
803 return false;
804 }
805 }
806 #endregion
807 #region public virtual bool ReadMemory(uint address, out long value)
808 public virtual bool ReadMemory(uint address, out long value)
809 {
810 value = 0;
811 try
812 {
813 int bytesRead;
814 uint size = sizeof(long);
815 byte[] bitData = new byte[size];
816 ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(handle, (IntPtr)address, bitData, size, out bytesRead);
817 if (bytesRead == 0)
818 return false;
819 value = BitConverter.ToInt64(bitData, 0);
820 return true;
821 }
822 catch
823 {
824 value = 0x00;
825 return false;
826 }
827 }
828 #endregion
829 #endregion
830 }
831 #endregion
832 }

  ViewVC Help
Powered by ViewVC 1.1.22