/[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 425 - (show annotations) (download)
Tue May 28 12:33:14 2013 UTC (8 years, 4 months ago) by william
File size: 35362 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; /* unused */
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 < (MemoryAddress + 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 List<byte> buffer_list = new List<byte>();
330 //for (uint j = MemoryAddress; j < (MemoryAddress+bytesToRead); j += size)
331 //{
332 // using (ProcessMemoryChunk mem = new ProcessMemoryChunk(m_ReadProcess, (IntPtr)j, (int)bytesToRead))
333 // {
334 // byte[] bigMem = mem.Read();
335 // foreach (byte b in bigMem) { buffer_list.Add(b); }
336 // bigMem = null;
337 // }
338 //}
339
340 using (ProcessMemoryChunk mem = new ProcessMemoryChunk(m_ReadProcess, (IntPtr)MemoryAddress, (int)bytesToRead))
341 {
342 byte[] bigMem = mem.Read();
343 foreach (byte b in bigMem) { buffer_list.Add(b); }
344 bigMem = null;
345 }
346
347 bytesRead = buffer_list.Count;
348 data = new byte[bytesToRead];
349 data.Initialize();
350 Buffer.BlockCopy(buffer_list.ToArray(), 0, data, 0, (int)bytesToRead);
351 st.Stop();
352 logger.Profiler.WriteLine("ReadProcessMemoryAtOnce(): start=0x{0:x8} end=0x{1:x8} took {2:0.0000} seconds", MemoryAddress, MemoryAddress + bytesToRead, st.Elapsed.TotalSeconds);
353 }
354 #region public void ReadProcessMemory(int MemoryAddress, uint bytesToRead, out int bytesRead, out byte[] data)
355 public void ReadProcessMemory(int MemoryAddress, uint bytesToRead, out int bytesRead, out byte[] data)
356 {
357 ReadProcessMemory((uint)MemoryAddress, bytesToRead, out bytesRead, out data);
358 }
359 public void ReadProcessMemory(uint MemoryAddress, uint bytesToRead, out int bytesRead, out byte[] data)
360 {
361 try
362 {
363 ReadProcessMemoryAtOnce(MemoryAddress, bytesToRead, out bytesRead, out data);
364 //bytesRead = 0;
365 //data = new byte[bytesToRead];
366 //const int alignment = 1;
367 //List<byte> buffer_list = new List<byte>();
368 //uint _MemoryAddress = MemoryAddress;
369 //for (int i = 0; i < bytesToRead; i++)
370 //{
371 // byte[] buffer = new byte[alignment];
372 // int _bytesRead = 0;
373 // MEMORY_BASIC_INFORMATION m;
374 // ProcessMemoryReaderApi.VirtualQueryEx(m_ReadProcess.Handle, (IntPtr)_MemoryAddress, out m, (uint)Marshal.SizeOf(typeof(MEMORY_BASIC_INFORMATION)));
375
376 // if (m.AllocationProtect.HasFlag(AllocationProtect.PAGE_NOACCESS) ||
377 // m.AllocationProtect == 0)
378 // {
379 // uint OrignalAddress = _MemoryAddress;
380 // while (m.AllocationProtect.HasFlag(AllocationProtect.PAGE_NOACCESS) || m.AllocationProtect == 0)
381 // {
382 // ProcessMemoryReaderApi.VirtualQueryEx(m_ReadProcess.Handle, (IntPtr)_MemoryAddress, out m, (uint)Marshal.SizeOf(typeof(MEMORY_BASIC_INFORMATION)));
383 // _MemoryAddress++;
384 // buffer_list.Add(0);
385 // }
386 // _MemoryAddress--;
387 // uint diff = _MemoryAddress - OrignalAddress;
388 // i += (int)diff;
389 // buffer_list.RemoveAt(buffer_list.Count-1);
390 // }
391
392 // ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(handle, (IntPtr)_MemoryAddress, buffer, alignment, out _bytesRead);
393 // int LastError = Marshal.GetLastWin32Error();
394 // if (LastError != ResultWin32.ERROR_SUCCESS)
395 // {
396 // string ErrorName = ResultWin32.GetErrorName(LastError);
397 // }
398 // if (_bytesRead > 0)
399 // {
400 // foreach (byte b in buffer)
401 // {
402 // buffer_list.Add(b);
403 // }
404 // _MemoryAddress += (uint)buffer.Length;
405 // }
406 //}
407 //bytesRead = buffer_list.Count;
408 //Buffer.BlockCopy(buffer_list.ToArray(), 0, data, 0, (int)bytesToRead);
409 }
410 catch (SEHException ex)
411 {
412 logger.Error.WriteLine("ReadProcessMemory() SEHException was thrown: (0x{0:x8}) - {1}", ex.ErrorCode, ex.Message);
413 logger.Error.WriteLine(ex.ToString());
414 throw ex;
415 }
416 catch (Exception ex)
417 {
418 logger.Error.WriteLine("ReadProcessMemory(): Exception");
419 logger.Error.WriteLine(ex.ToString());
420 throw ex;
421 }
422 }
423 #endregion
424 #endregion
425
426 #region IMemoryWriter Members
427 #region public void WriteProcessMemory(uint MemoryAddress, byte[] bytesToWrite, out int bytesWritten)
428 public void WriteProcessMemory(int MemoryAddress, byte[] bytesToWrite, out int bytesWritten)
429 {
430 uint _bytesWritten = 0;
431 WriteProcessMemory((uint)MemoryAddress, bytesToWrite, out _bytesWritten);
432 bytesWritten = (int)_bytesWritten;
433 }
434 public void WriteProcessMemory(uint MemoryAddress, byte[] bytesToWrite, out uint bytesWritten)
435 {
436 try
437 {
438 uint _bytesWritten = 0;
439 uint _MemoryAddress = MemoryAddress;
440 for (int i = 0; i < bytesToWrite.Length; i++)
441 {
442 UIntPtr _ptrBytesWritten = UIntPtr.Zero;
443 byte[] buffer = new byte[] { bytesToWrite[i] };
444 ProcessMemoryReaderApi.WriteProcessMemory(handle, (IntPtr)_MemoryAddress, buffer, (uint)buffer.Length, out _ptrBytesWritten);
445 _MemoryAddress++;
446 _bytesWritten++;
447 }
448 bytesWritten = _bytesWritten;
449 }
450 catch (SEHException ex)
451 {
452 logger.Error.WriteLine("WriteProcessMemory() SEHException was thrown: (0x{0:x8}) - {1}", ex.ErrorCode, ex.Message);
453 logger.Error.WriteLine(ex.ToString());
454 throw ex;
455 }
456 catch (Exception ex)
457 {
458
459 logger.Error.WriteLine("WriteProcessMemory() Exception");
460 logger.Error.WriteLine(ex.ToString());
461 bytesWritten = 0;
462 throw ex;
463 }
464 }
465 #endregion
466 #endregion
467
468 #region IFileWriter Members
469 #region public bool WriteProcessMemoryToFile(string filename, uint MemoryAddress, uint bytesToRead, out int bytesRead)
470 public bool WriteProcessMemoryToFile(string filename, int MemoryAddress, uint bytesToRead, out int bytesRead)
471 {
472 //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));
473 bytesRead = 0;
474 uint byte_alignment = 102400;
475 int address = MemoryAddress;
476 uint _bytesToRead = bytesToRead;
477 byte[] buffer = new byte[] { };
478 try
479 {
480 FileInfo fi = new FileInfo(filename);
481 if (fi.Exists)
482 fi.Delete();
483 using (FileStream fs = new FileStream(filename, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite))
484 {
485 BinaryWriter bw = new BinaryWriter(fs);
486 //foreach (byte b in data) { bw.Write(b); }
487
488 for (uint i = 0; i <= bytesToRead; )
489 {
490 if (_bytesToRead < byte_alignment)
491 {
492 _bytesToRead = bytesToRead;
493 buffer = new byte[_bytesToRead];
494 }
495 else
496 {
497 _bytesToRead = byte_alignment;
498 buffer = new byte[byte_alignment];
499 }
500 ReadProcessMemory(address, _bytesToRead, out bytesRead, out buffer);
501 bw.Write(buffer);
502 bw.Flush();
503
504 if (_bytesToRead < byte_alignment)
505 {
506 i += _bytesToRead;
507 address += (int)_bytesToRead;
508 }
509 else
510 {
511 i += byte_alignment;
512 address += (int)byte_alignment;
513 }
514
515
516 }
517 bw.Close();
518 }
519 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));
520 return true;
521 }
522 catch (SEHException ex)
523 {
524 logger.Error.WriteLine("WriteProcessMemoryToFile() SEHException was thrown: (0x{0:x8}) - {1}", ex.ErrorCode, ex.Message);
525 logger.Error.WriteLine(ex.ToString());
526 throw ex;
527 }
528 catch (OutOfMemoryException ex)
529 {
530 logger.Error.WriteLine("WriteProcessMemoryToFile(): Exception");
531 logger.Error.WriteLine(ex.ToString());
532 }
533 catch (Exception ex)
534 {
535 logger.Error.WriteLine("WriteProcessMemoryToFile(): Exception");
536 logger.Error.WriteLine(ex.ToString());
537 throw ex;
538 }
539 return false;
540 }
541 #endregion
542 #endregion
543
544 #region IPatchMemory members
545 #region public virtual bool PatchMemory(uint address, byte value)
546 public virtual bool PatchMemory(uint address, byte value)
547 {
548 byte[] bitData = BitConverter.GetBytes(value);
549 UIntPtr ptrBytesWritten;
550 ProcessMemoryReaderApi.WriteProcessMemory(handle, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten);
551 if (ptrBytesWritten.ToUInt32() == 0)
552 return false;
553 byte check = 0;
554 ReadMemory(address, out check);
555 if (check == value) return true;
556 return false;
557 }
558 #endregion
559 #region public virtual bool PatchMemory(uint address, sbyte value)
560 public virtual bool PatchMemory(uint address, sbyte value)
561 {
562 byte[] bitData = BitConverter.GetBytes(value);
563 UIntPtr ptrBytesWritten;
564 ProcessMemoryReaderApi.WriteProcessMemory(handle, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten);
565 if (ptrBytesWritten.ToUInt32() == 0)
566 return false;
567 sbyte check = 0;
568 ReadMemory(address, out check);
569 if (check == value) return true;
570 return false;
571 }
572 #endregion
573 #region public virtual bool PatchMemory(uint address, ushort value)
574 public virtual bool PatchMemory(uint address, ushort value)
575 {
576 byte[] bitData = BitConverter.GetBytes(value);
577 UIntPtr ptrBytesWritten;
578 ProcessMemoryReaderApi.WriteProcessMemory(handle, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten);
579 if (ptrBytesWritten.ToUInt32() == 0)
580 return false;
581 ushort check = 0;
582 ReadMemory(address, out check);
583 if (check == value) return true;
584 return false;
585 }
586 #endregion
587 #region public virtual bool PatchMemory(uint address, short value)
588 public virtual bool PatchMemory(uint address, short value)
589 {
590 byte[] bitData = BitConverter.GetBytes(value);
591 UIntPtr ptrBytesWritten;
592 ProcessMemoryReaderApi.WriteProcessMemory(handle, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten);
593 if (ptrBytesWritten.ToUInt32() == 0)
594 return false;
595 short check = 0;
596 ReadMemory(address, out check);
597 if (check == value) return true;
598 return false;
599 }
600 #endregion
601 #region public virtual bool PatchMemory(uint address, uint value)
602 public virtual bool PatchMemory(uint address, uint value)
603 {
604 byte[] bitData = BitConverter.GetBytes(value);
605 UIntPtr ptrBytesWritten;
606 ProcessMemoryReaderApi.WriteProcessMemory(handle, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten);
607 if (ptrBytesWritten.ToUInt32() == 0)
608 return false;
609 uint check = 0;
610 ReadMemory(address, out check);
611 if (check == value) return true;
612 return false;
613 }
614 #endregion
615 #region public virtual bool PatchMemory(uint address, int value)
616 public virtual bool PatchMemory(uint address, int value)
617 {
618 byte[] bitData = BitConverter.GetBytes(value);
619 UIntPtr ptrBytesWritten;
620 ProcessMemoryReaderApi.WriteProcessMemory(handle, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten);
621 if (ptrBytesWritten.ToUInt32() == 0)
622 return false;
623 int check = 0;
624 ReadMemory(address, out check);
625 if (check == value) return true;
626 return false;
627 }
628 #endregion
629 #region public virtual bool PatchMemory(uint address, ulong value)
630 public virtual bool PatchMemory(uint address, ulong value)
631 {
632 byte[] bitData = BitConverter.GetBytes(value);
633 UIntPtr ptrBytesWritten;
634 ProcessMemoryReaderApi.WriteProcessMemory(handle, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten);
635 if (ptrBytesWritten.ToUInt32() == 0)
636 return false;
637 ulong check = 0;
638 ReadMemory(address, out check);
639 if (check == value) return true;
640 return false;
641 }
642 #endregion
643 #region public virtual bool PatchMemory(uint address, long value)
644 public virtual bool PatchMemory(uint address, long value)
645 {
646 byte[] bitData = BitConverter.GetBytes(value);
647 UIntPtr ptrBytesWritten;
648 ProcessMemoryReaderApi.WriteProcessMemory(handle, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten);
649 if (ptrBytesWritten.ToUInt32() == 0)
650 return false;
651 long check = 0;
652 ReadMemory(address, out check);
653 if (check == value) return true;
654 return false;
655 }
656 #endregion
657 #endregion
658
659 #region IReadMemory members
660 #region public virtual bool ReadMemory(uint address, out byte value)
661 public virtual bool ReadMemory(uint address, out byte value)
662 {
663 value = 0;
664 try
665 {
666 int bytesRead;
667 uint size = sizeof(byte);
668 byte[] bitData = new byte[size];
669 ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(handle, (IntPtr)address, bitData, size, out bytesRead);
670 if (bytesRead == 0)
671 return false;
672 value = bitData[0];
673 return true;
674 }
675 catch
676 {
677 value = 0x00;
678 return false;
679 }
680 }
681 #endregion
682 #region public virtual bool ReadMemory(uint address, out sbyte value)
683 public virtual bool ReadMemory(uint address, out sbyte value)
684 {
685 value = 0;
686 try
687 {
688 int bytesRead;
689 uint size = sizeof(sbyte);
690 byte[] bitData = new byte[size];
691 ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(handle, (IntPtr)address, bitData, size, out bytesRead);
692 if (bytesRead == 0)
693 return false;
694 value = Convert.ToSByte(bitData[0]);
695 return true;
696 }
697 catch
698 {
699 value = 0x00;
700 return false;
701 }
702 }
703 #endregion
704 #region public virtual bool ReadMemory(uint address, out ushort value)
705 public virtual bool ReadMemory(uint address, out ushort value)
706 {
707 value = 0;
708 try
709 {
710 int bytesRead;
711 uint size = sizeof(ushort);
712 byte[] bitData = new byte[size];
713 ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(handle, (IntPtr)address, bitData, size, out bytesRead);
714 if (bytesRead == 0)
715 return false;
716 value = BitConverter.ToUInt16(bitData, 0);
717 return true;
718 }
719 catch
720 {
721 value = 0x00;
722 return false;
723 }
724 }
725 #endregion
726 #region public virtual bool ReadMemory(uint address, out short value)
727 public virtual bool ReadMemory(uint address, out short value)
728 {
729 value = 0;
730 try
731 {
732 int bytesRead;
733 uint size = sizeof(short);
734 byte[] bitData = new byte[size];
735 ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(handle, (IntPtr)address, bitData, size, out bytesRead);
736 if (bytesRead == 0)
737 return false;
738 value = BitConverter.ToInt16(bitData, 0);
739 return true;
740 }
741 catch
742 {
743 value = 0x00;
744 return false;
745 }
746 }
747 #endregion
748 #region public virtual bool ReadMemory(uint address, out uint value)
749 public virtual bool ReadMemory(uint address, out uint value)
750 {
751 value = 0;
752 try
753 {
754 int bytesRead;
755 uint size = sizeof(uint);
756 byte[] bitData = new byte[size];
757 ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(handle, (IntPtr)address, bitData, size, out bytesRead);
758 if (bytesRead == 0)
759 return false;
760 value = BitConverter.ToUInt32(bitData, 0);
761 return true;
762 }
763 catch
764 {
765 value = 0x00;
766 return false;
767 }
768 }
769 #endregion
770 #region public virtual bool ReadMemory(uint address, out int value)
771 public virtual bool ReadMemory(uint address, out int value)
772 {
773 value = 0;
774 try
775 {
776 int bytesRead;
777 uint size = sizeof(int);
778 byte[] bitData = new byte[size];
779 ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(handle, (IntPtr)address, bitData, size, out bytesRead);
780 if (bytesRead == 0)
781 return false;
782 value = BitConverter.ToInt32(bitData, 0);
783 return true;
784 }
785 catch
786 {
787 value = 0x00;
788 return false;
789 }
790 }
791 #endregion
792 #region public virtual bool ReadMemory(uint address, out ulong value)
793 public virtual bool ReadMemory(uint address, out ulong value)
794 {
795 value = 0;
796 try
797 {
798 int bytesRead;
799 uint size = sizeof(ulong);
800 byte[] bitData = new byte[size];
801 ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(handle, (IntPtr)address, bitData, size, out bytesRead);
802 if (bytesRead == 0)
803 return false;
804 value = BitConverter.ToUInt64(bitData, 0);
805 return true;
806 }
807 catch
808 {
809 value = 0x00;
810 return false;
811 }
812 }
813 #endregion
814 #region public virtual bool ReadMemory(uint address, out long value)
815 public virtual bool ReadMemory(uint address, out long value)
816 {
817 value = 0;
818 try
819 {
820 int bytesRead;
821 uint size = sizeof(long);
822 byte[] bitData = new byte[size];
823 ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(handle, (IntPtr)address, bitData, size, out bytesRead);
824 if (bytesRead == 0)
825 return false;
826 value = BitConverter.ToInt64(bitData, 0);
827 return true;
828 }
829 catch
830 {
831 value = 0x00;
832 return false;
833 }
834 }
835 #endregion
836 #endregion
837 }
838 #endregion
839 }

  ViewVC Help
Powered by ViewVC 1.1.22