5 |
|
using System.Threading; |
6 |
|
using System.Runtime.InteropServices; |
7 |
|
using RomCheater.Logging; |
8 |
+ |
using RomCheater.Core; |
9 |
+ |
using System.IO; |
10 |
|
|
11 |
|
namespace Sojaner.MemoryScanner |
12 |
|
{ |
68 |
|
} |
69 |
|
} |
70 |
|
|
71 |
< |
public byte[] ReadProcessMemory(IntPtr MemoryAddress, uint bytesToRead, out int bytesRead) |
71 |
> |
|
72 |
> |
public bool DumpMemory(string filename, uint MemoryAddress, uint bytesToRead, out int bytesRead) |
73 |
> |
{ |
74 |
> |
bytesRead = 0; |
75 |
> |
uint byte_alignment = 512; // 4mb alignment |
76 |
> |
uint address = MemoryAddress; |
77 |
> |
try |
78 |
> |
{ |
79 |
> |
using (FileStream fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) |
80 |
> |
{ |
81 |
> |
BinaryWriter bw = new BinaryWriter(fs); |
82 |
> |
//foreach (byte b in data) { bw.Write(b); } |
83 |
> |
|
84 |
> |
for (uint i = 0; i <= bytesToRead; i += byte_alignment) |
85 |
> |
{ |
86 |
> |
byte[] buffer = new byte[byte_alignment]; |
87 |
> |
uint bytes_to_read = byte_alignment; |
88 |
> |
IntPtr ptrBytesRead; |
89 |
> |
ProcessMemoryReaderApi.ReadProcessMemory(m_hProcess, (IntPtr)address, buffer, bytes_to_read, out ptrBytesRead); |
90 |
> |
bytesRead = ptrBytesRead.ToInt32(); |
91 |
> |
bw.Write(buffer); |
92 |
> |
bw.Flush(); |
93 |
> |
address += byte_alignment; |
94 |
> |
} |
95 |
> |
bw.Close(); |
96 |
> |
} |
97 |
> |
return true; |
98 |
> |
} |
99 |
> |
catch (OutOfMemoryException ex) |
100 |
> |
{ |
101 |
> |
logger.Error.WriteLine("DumpMemory(): OutOfMemoryException"); |
102 |
> |
logger.Error.WriteLine(ex.ToString()); |
103 |
> |
} |
104 |
> |
catch (Exception ex) |
105 |
> |
{ |
106 |
> |
logger.Error.WriteLine("DumpMemory(): Exception"); |
107 |
> |
logger.Error.WriteLine(ex.ToString()); |
108 |
> |
} |
109 |
> |
return false; |
110 |
> |
} |
111 |
> |
|
112 |
> |
public byte[] ReadProcessMemory(uint MemoryAddress, uint bytesToRead, out int bytesRead) |
113 |
|
{ |
114 |
+ |
bytesRead = 0; |
115 |
+ |
uint address = MemoryAddress; |
116 |
+ |
List<byte[]> aligned_array_list = new List<byte[]>(); |
117 |
|
try |
118 |
|
{ |
119 |
< |
byte[] buffer = new byte[bytesToRead - 1]; |
119 |
> |
uint byte_alignment = 512; // 4mb alignment |
120 |
> |
|
121 |
|
|
122 |
< |
IntPtr ptrBytesRead; |
123 |
< |
ProcessMemoryReaderApi.ReadProcessMemory(m_hProcess, MemoryAddress, buffer, bytesToRead, out ptrBytesRead); |
122 |
> |
for (uint i = 0; i <= bytesToRead; i += byte_alignment) |
123 |
> |
{ |
124 |
> |
byte[] buffer = new byte[byte_alignment]; |
125 |
> |
uint bytes_to_read = byte_alignment; |
126 |
> |
IntPtr ptrBytesRead; |
127 |
> |
ProcessMemoryReaderApi.ReadProcessMemory(m_hProcess, (IntPtr)address, buffer, bytes_to_read, out ptrBytesRead); |
128 |
> |
bytesRead = ptrBytesRead.ToInt32(); |
129 |
> |
aligned_array_list.Add(buffer); |
130 |
> |
address += byte_alignment; |
131 |
> |
} |
132 |
|
|
133 |
< |
bytesRead = ptrBytesRead.ToInt32(); |
133 |
> |
//List<byte> big_array = new List<byte>(); |
134 |
> |
//foreach (byte[] aligned_array in aligned_array_list) { foreach (byte b in aligned_array) { big_array.Add(b); } } |
135 |
|
|
136 |
< |
return buffer; |
136 |
> |
return new byte[] { }; |
137 |
|
} |
138 |
|
catch (OutOfMemoryException ex) |
139 |
|
{ |
145 |
|
logger.Error.WriteLine("ReadProcessMemory(): Exception"); |
146 |
|
logger.Error.WriteLine(ex.ToString()); |
147 |
|
} |
92 |
– |
bytesRead = 0; |
148 |
|
return new byte[] { }; |
149 |
|
} |
150 |
|
|