/[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 393 - (show annotations) (download)
Wed Jun 20 10:40:24 2012 UTC (7 years, 8 months ago) by william
File size: 31361 byte(s)
ReadMemoryAtOnce(): don't attempt to buffer the data

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

  ViewVC Help
Powered by ViewVC 1.1.22