/[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 404 - (show annotations) (download)
Thu Jun 21 12:51:57 2012 UTC (8 years, 5 months ago) by william
File size: 33809 byte(s)

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

  ViewVC Help
Powered by ViewVC 1.1.22