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 |
ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(m_hProcess, (IntPtr)MemoryAddress, data, bytesToRead, out bytesRead); |
270 |
} |
271 |
catch (SEHException ex) |
272 |
{ |
273 |
logger.Error.WriteLine("ReadProcessMemory() SEHException was thrown: (0x{0:x8}) - {1}", ex.ErrorCode, ex.Message); |
274 |
logger.Error.WriteLine(ex.ToString()); |
275 |
throw ex; |
276 |
} |
277 |
catch (Exception ex) |
278 |
{ |
279 |
logger.Error.WriteLine("ReadProcessMemory(): Exception"); |
280 |
logger.Error.WriteLine(ex.ToString()); |
281 |
throw ex; |
282 |
} |
283 |
} |
284 |
public void ReadProcessMemory(uint MemoryAddress, uint bytesToRead, out int bytesRead, out byte[] data) |
285 |
{ |
286 |
try |
287 |
{ |
288 |
bytesRead = 0; |
289 |
|
290 |
List<byte> buffer_list = new List<byte>(); |
291 |
uint _MemoryAddress = MemoryAddress; |
292 |
for (int i = 0; i < bytesToRead; i++) |
293 |
{ |
294 |
byte[] buffer = new byte[1]; |
295 |
int _bytesRead = 0; |
296 |
ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(m_hProcess, (IntPtr)_MemoryAddress, buffer, 1, out _bytesRead); |
297 |
if (_bytesRead > 0) |
298 |
{ |
299 |
foreach (byte b in buffer) |
300 |
{ |
301 |
buffer_list.Add(b); |
302 |
} |
303 |
_MemoryAddress += (uint)buffer.Length; |
304 |
} |
305 |
else |
306 |
{ |
307 |
//buffer_list.Add((byte)'?'); |
308 |
buffer_list.Add(0); |
309 |
_MemoryAddress++; |
310 |
} |
311 |
|
312 |
} |
313 |
bytesRead = buffer_list.Count; |
314 |
data = buffer_list.ToArray(); |
315 |
} |
316 |
catch (SEHException ex) |
317 |
{ |
318 |
logger.Error.WriteLine("ReadProcessMemory() SEHException was thrown: (0x{0:x8}) - {1}", ex.ErrorCode, ex.Message); |
319 |
logger.Error.WriteLine(ex.ToString()); |
320 |
throw ex; |
321 |
} |
322 |
catch (Exception ex) |
323 |
{ |
324 |
logger.Error.WriteLine("ReadProcessMemory(): Exception"); |
325 |
logger.Error.WriteLine(ex.ToString()); |
326 |
throw ex; |
327 |
} |
328 |
} |
329 |
#endregion |
330 |
#endregion |
331 |
|
332 |
#region IMemoryWriter Members |
333 |
#region public void WriteProcessMemory(uint MemoryAddress, byte[] bytesToWrite, out int bytesWritten) |
334 |
public void WriteProcessMemory(int MemoryAddress, byte[] bytesToWrite, out int bytesWritten) |
335 |
{ |
336 |
uint _bytesWritten = 0; |
337 |
WriteProcessMemory((uint)MemoryAddress, bytesToWrite, out _bytesWritten); |
338 |
bytesWritten = (int)_bytesWritten; |
339 |
} |
340 |
public void WriteProcessMemory(uint MemoryAddress, byte[] bytesToWrite, out uint bytesWritten) |
341 |
{ |
342 |
try |
343 |
{ |
344 |
uint _bytesWritten = 0; |
345 |
uint _MemoryAddress = MemoryAddress; |
346 |
for (int i = 0; i < bytesToWrite.Length; i++) |
347 |
{ |
348 |
UIntPtr _ptrBytesWritten = UIntPtr.Zero; |
349 |
byte[] buffer = new byte[] { bytesToWrite[i] }; |
350 |
ProcessMemoryReaderApi.WriteProcessMemory(m_hProcess, (IntPtr)_MemoryAddress, buffer, (uint)buffer.Length, out _ptrBytesWritten); |
351 |
_MemoryAddress++; |
352 |
_bytesWritten++; |
353 |
} |
354 |
bytesWritten = _bytesWritten; |
355 |
} |
356 |
catch (SEHException ex) |
357 |
{ |
358 |
logger.Error.WriteLine("WriteProcessMemory() SEHException was thrown: (0x{0:x8}) - {1}", ex.ErrorCode, ex.Message); |
359 |
logger.Error.WriteLine(ex.ToString()); |
360 |
throw ex; |
361 |
} |
362 |
catch (Exception ex) |
363 |
{ |
364 |
|
365 |
logger.Error.WriteLine("WriteProcessMemory() Exception"); |
366 |
logger.Error.WriteLine(ex.ToString()); |
367 |
bytesWritten = 0; |
368 |
throw ex; |
369 |
} |
370 |
} |
371 |
#endregion |
372 |
#endregion |
373 |
|
374 |
#region IFileWriter Members |
375 |
#region public bool WriteProcessMemoryToFile(string filename, uint MemoryAddress, uint bytesToRead, out int bytesRead) |
376 |
public bool WriteProcessMemoryToFile(string filename, int MemoryAddress, uint bytesToRead, out int bytesRead) |
377 |
{ |
378 |
//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)); |
379 |
bytesRead = 0; |
380 |
uint byte_alignment = 102400; |
381 |
int address = MemoryAddress; |
382 |
uint _bytesToRead = bytesToRead; |
383 |
byte[] buffer = new byte[] { }; |
384 |
try |
385 |
{ |
386 |
FileInfo fi = new FileInfo(filename); |
387 |
if (fi.Exists) |
388 |
fi.Delete(); |
389 |
using (FileStream fs = new FileStream(filename, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite)) |
390 |
{ |
391 |
BinaryWriter bw = new BinaryWriter(fs); |
392 |
//foreach (byte b in data) { bw.Write(b); } |
393 |
|
394 |
for (uint i = 0; i <= bytesToRead; ) |
395 |
{ |
396 |
if (_bytesToRead < byte_alignment) |
397 |
{ |
398 |
_bytesToRead = bytesToRead; |
399 |
buffer = new byte[_bytesToRead]; |
400 |
} |
401 |
else |
402 |
{ |
403 |
_bytesToRead = byte_alignment; |
404 |
buffer = new byte[byte_alignment]; |
405 |
} |
406 |
ReadProcessMemoryAtOnce(address, _bytesToRead, out bytesRead, out buffer); |
407 |
bw.Write(buffer); |
408 |
bw.Flush(); |
409 |
|
410 |
if (_bytesToRead < byte_alignment) |
411 |
{ |
412 |
i += _bytesToRead; |
413 |
address += (int)_bytesToRead; |
414 |
} |
415 |
else |
416 |
{ |
417 |
i += byte_alignment; |
418 |
address += (int)byte_alignment; |
419 |
} |
420 |
|
421 |
|
422 |
} |
423 |
bw.Close(); |
424 |
} |
425 |
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)); |
426 |
return true; |
427 |
} |
428 |
catch (SEHException ex) |
429 |
{ |
430 |
logger.Error.WriteLine("WriteProcessMemoryToFile() SEHException was thrown: (0x{0:x8}) - {1}", ex.ErrorCode, ex.Message); |
431 |
logger.Error.WriteLine(ex.ToString()); |
432 |
throw ex; |
433 |
} |
434 |
catch (OutOfMemoryException ex) |
435 |
{ |
436 |
logger.Error.WriteLine("WriteProcessMemoryToFile(): Exception"); |
437 |
logger.Error.WriteLine(ex.ToString()); |
438 |
} |
439 |
catch (Exception ex) |
440 |
{ |
441 |
logger.Error.WriteLine("WriteProcessMemoryToFile(): Exception"); |
442 |
logger.Error.WriteLine(ex.ToString()); |
443 |
throw ex; |
444 |
} |
445 |
return false; |
446 |
} |
447 |
#endregion |
448 |
#endregion |
449 |
|
450 |
#region IPatchMemory members |
451 |
#region public virtual bool PatchMemory(int address, byte value) |
452 |
public virtual bool PatchMemory(int address, byte value) |
453 |
{ |
454 |
byte[] bitData = BitConverter.GetBytes(value); |
455 |
UIntPtr ptrBytesWritten; |
456 |
ProcessMemoryReaderApi.WriteProcessMemory(m_hProcess, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten); |
457 |
if (ptrBytesWritten.ToUInt32() == 0) |
458 |
return false; |
459 |
byte check = 0; |
460 |
ReadMemory(address, out check); |
461 |
if (check == value) return true; |
462 |
return false; |
463 |
} |
464 |
#endregion |
465 |
#region public virtual bool PatchMemory(int address, sbyte value) |
466 |
public virtual bool PatchMemory(int address, sbyte value) |
467 |
{ |
468 |
byte[] bitData = BitConverter.GetBytes(value); |
469 |
UIntPtr ptrBytesWritten; |
470 |
ProcessMemoryReaderApi.WriteProcessMemory(m_hProcess, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten); |
471 |
if (ptrBytesWritten.ToUInt32() == 0) |
472 |
return false; |
473 |
sbyte check = 0; |
474 |
ReadMemory(address, out check); |
475 |
if (check == value) return true; |
476 |
return false; |
477 |
} |
478 |
#endregion |
479 |
#region public virtual bool PatchMemory(int address, ushort value) |
480 |
public virtual bool PatchMemory(int address, ushort value) |
481 |
{ |
482 |
byte[] bitData = BitConverter.GetBytes(value); |
483 |
UIntPtr ptrBytesWritten; |
484 |
ProcessMemoryReaderApi.WriteProcessMemory(m_hProcess, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten); |
485 |
if (ptrBytesWritten.ToUInt32() == 0) |
486 |
return false; |
487 |
ushort check = 0; |
488 |
ReadMemory(address, out check); |
489 |
if (check == value) return true; |
490 |
return false; |
491 |
} |
492 |
#endregion |
493 |
#region public virtual bool PatchMemory(int address, short value) |
494 |
public virtual bool PatchMemory(int address, short value) |
495 |
{ |
496 |
byte[] bitData = BitConverter.GetBytes(value); |
497 |
UIntPtr ptrBytesWritten; |
498 |
ProcessMemoryReaderApi.WriteProcessMemory(m_hProcess, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten); |
499 |
if (ptrBytesWritten.ToUInt32() == 0) |
500 |
return false; |
501 |
short check = 0; |
502 |
ReadMemory(address, out check); |
503 |
if (check == value) return true; |
504 |
return false; |
505 |
} |
506 |
#endregion |
507 |
#region public virtual bool PatchMemory(int address, uint value) |
508 |
public virtual bool PatchMemory(int address, uint value) |
509 |
{ |
510 |
byte[] bitData = BitConverter.GetBytes(value); |
511 |
UIntPtr ptrBytesWritten; |
512 |
ProcessMemoryReaderApi.WriteProcessMemory(m_hProcess, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten); |
513 |
if (ptrBytesWritten.ToUInt32() == 0) |
514 |
return false; |
515 |
uint 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(int address, int value) |
522 |
public virtual bool PatchMemory(int address, int value) |
523 |
{ |
524 |
byte[] bitData = BitConverter.GetBytes(value); |
525 |
UIntPtr ptrBytesWritten; |
526 |
ProcessMemoryReaderApi.WriteProcessMemory(m_hProcess, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten); |
527 |
if (ptrBytesWritten.ToUInt32() == 0) |
528 |
return false; |
529 |
int 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(int address, ulong value) |
536 |
public virtual bool PatchMemory(int address, ulong value) |
537 |
{ |
538 |
byte[] bitData = BitConverter.GetBytes(value); |
539 |
UIntPtr ptrBytesWritten; |
540 |
ProcessMemoryReaderApi.WriteProcessMemory(m_hProcess, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten); |
541 |
if (ptrBytesWritten.ToUInt32() == 0) |
542 |
return false; |
543 |
ulong 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(int address, long value) |
550 |
public virtual bool PatchMemory(int address, long value) |
551 |
{ |
552 |
byte[] bitData = BitConverter.GetBytes(value); |
553 |
UIntPtr ptrBytesWritten; |
554 |
ProcessMemoryReaderApi.WriteProcessMemory(m_hProcess, (IntPtr)address, bitData, (uint)bitData.Length, out ptrBytesWritten); |
555 |
if (ptrBytesWritten.ToUInt32() == 0) |
556 |
return false; |
557 |
long check = 0; |
558 |
ReadMemory(address, out check); |
559 |
if (check == value) return true; |
560 |
return false; |
561 |
} |
562 |
#endregion |
563 |
#endregion |
564 |
|
565 |
#region IReadMemory members |
566 |
#region public virtual bool ReadMemory(int address, out byte value) |
567 |
public virtual bool ReadMemory(int address, out byte value) |
568 |
{ |
569 |
value = 0; |
570 |
try |
571 |
{ |
572 |
int bytesRead; |
573 |
uint size = sizeof(byte); |
574 |
byte[] bitData = new byte[size]; |
575 |
ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(m_hProcess, (IntPtr)address, bitData, size, out bytesRead); |
576 |
if (bytesRead == 0) |
577 |
return false; |
578 |
value = bitData[0]; |
579 |
return true; |
580 |
} |
581 |
catch |
582 |
{ |
583 |
value = 0x00; |
584 |
return false; |
585 |
} |
586 |
} |
587 |
#endregion |
588 |
#region public virtual bool ReadMemory(int address, out sbyte value) |
589 |
public virtual bool ReadMemory(int address, out sbyte value) |
590 |
{ |
591 |
value = 0; |
592 |
try |
593 |
{ |
594 |
int bytesRead; |
595 |
uint size = sizeof(sbyte); |
596 |
byte[] bitData = new byte[size]; |
597 |
ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(m_hProcess, (IntPtr)address, bitData, size, out bytesRead); |
598 |
if (bytesRead == 0) |
599 |
return false; |
600 |
value = Convert.ToSByte(bitData[0]); |
601 |
return true; |
602 |
} |
603 |
catch |
604 |
{ |
605 |
value = 0x00; |
606 |
return false; |
607 |
} |
608 |
} |
609 |
#endregion |
610 |
#region public virtual bool ReadMemory(int address, out ushort value) |
611 |
public virtual bool ReadMemory(int address, out ushort value) |
612 |
{ |
613 |
value = 0; |
614 |
try |
615 |
{ |
616 |
int bytesRead; |
617 |
uint size = sizeof(ushort); |
618 |
byte[] bitData = new byte[size]; |
619 |
ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(m_hProcess, (IntPtr)address, bitData, size, out bytesRead); |
620 |
if (bytesRead == 0) |
621 |
return false; |
622 |
value = BitConverter.ToUInt16(bitData, 0); |
623 |
return true; |
624 |
} |
625 |
catch |
626 |
{ |
627 |
value = 0x00; |
628 |
return false; |
629 |
} |
630 |
} |
631 |
#endregion |
632 |
#region public virtual bool ReadMemory(int address, out short value) |
633 |
public virtual bool ReadMemory(int address, out short value) |
634 |
{ |
635 |
value = 0; |
636 |
try |
637 |
{ |
638 |
int bytesRead; |
639 |
uint size = sizeof(short); |
640 |
byte[] bitData = new byte[size]; |
641 |
ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(m_hProcess, (IntPtr)address, bitData, size, out bytesRead); |
642 |
if (bytesRead == 0) |
643 |
return false; |
644 |
value = BitConverter.ToInt16(bitData, 0); |
645 |
return true; |
646 |
} |
647 |
catch |
648 |
{ |
649 |
value = 0x00; |
650 |
return false; |
651 |
} |
652 |
} |
653 |
#endregion |
654 |
#region public virtual bool ReadMemory(int address, out uint value) |
655 |
public virtual bool ReadMemory(int address, out uint value) |
656 |
{ |
657 |
value = 0; |
658 |
try |
659 |
{ |
660 |
int bytesRead; |
661 |
uint size = sizeof(uint); |
662 |
byte[] bitData = new byte[size]; |
663 |
ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(m_hProcess, (IntPtr)address, bitData, size, out bytesRead); |
664 |
if (bytesRead == 0) |
665 |
return false; |
666 |
value = BitConverter.ToUInt32(bitData, 0); |
667 |
return true; |
668 |
} |
669 |
catch |
670 |
{ |
671 |
value = 0x00; |
672 |
return false; |
673 |
} |
674 |
} |
675 |
#endregion |
676 |
#region public virtual bool ReadMemory(int address, out int value) |
677 |
public virtual bool ReadMemory(int address, out int value) |
678 |
{ |
679 |
value = 0; |
680 |
try |
681 |
{ |
682 |
int bytesRead; |
683 |
uint size = sizeof(int); |
684 |
byte[] bitData = new byte[size]; |
685 |
ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(m_hProcess, (IntPtr)address, bitData, size, out bytesRead); |
686 |
if (bytesRead == 0) |
687 |
return false; |
688 |
value = BitConverter.ToInt32(bitData, 0); |
689 |
return true; |
690 |
} |
691 |
catch |
692 |
{ |
693 |
value = 0x00; |
694 |
return false; |
695 |
} |
696 |
} |
697 |
#endregion |
698 |
#region public virtual bool ReadMemory(int address, out ulong value) |
699 |
public virtual bool ReadMemory(int address, out ulong value) |
700 |
{ |
701 |
value = 0; |
702 |
try |
703 |
{ |
704 |
int bytesRead; |
705 |
uint size = sizeof(ulong); |
706 |
byte[] bitData = new byte[size]; |
707 |
ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(m_hProcess, (IntPtr)address, bitData, size, out bytesRead); |
708 |
if (bytesRead == 0) |
709 |
return false; |
710 |
value = BitConverter.ToUInt64(bitData, 0); |
711 |
return true; |
712 |
} |
713 |
catch |
714 |
{ |
715 |
value = 0x00; |
716 |
return false; |
717 |
} |
718 |
} |
719 |
#endregion |
720 |
#region public virtual bool ReadMemory(int address, out long value) |
721 |
public virtual bool ReadMemory(int address, out long value) |
722 |
{ |
723 |
value = 0; |
724 |
try |
725 |
{ |
726 |
int bytesRead; |
727 |
uint size = sizeof(long); |
728 |
byte[] bitData = new byte[size]; |
729 |
ProcessMemoryReader.ProcessMemoryReaderApi.ReadProcessMemory(m_hProcess, (IntPtr)address, bitData, size, out bytesRead); |
730 |
if (bytesRead == 0) |
731 |
return false; |
732 |
value = BitConverter.ToInt64(bitData, 0); |
733 |
return true; |
734 |
} |
735 |
catch |
736 |
{ |
737 |
value = 0x00; |
738 |
return false; |
739 |
} |
740 |
} |
741 |
#endregion |
742 |
#endregion |
743 |
} |
744 |
#endregion |
745 |
} |