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