74 |
|
bytesRead = 0; |
75 |
|
uint byte_alignment = 102400; // write to file in 100mb chunks |
76 |
|
uint address = MemoryAddress; |
77 |
+ |
uint _bytesToRead = bytesToRead; |
78 |
+ |
byte[] buffer = new byte[] { }; |
79 |
|
try |
80 |
|
{ |
81 |
< |
using (FileStream fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) |
81 |
> |
FileInfo fi = new FileInfo(filename); |
82 |
> |
if (fi.Exists) |
83 |
> |
fi.Delete(); |
84 |
> |
using (FileStream fs = new FileStream(filename, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite)) |
85 |
|
{ |
86 |
|
BinaryWriter bw = new BinaryWriter(fs); |
87 |
|
//foreach (byte b in data) { bw.Write(b); } |
88 |
|
|
89 |
< |
for (uint i = 0; i <= bytesToRead; i += byte_alignment) |
89 |
> |
for (uint i = 0; i <= bytesToRead;) |
90 |
|
{ |
91 |
< |
byte[] buffer = new byte[byte_alignment]; |
92 |
< |
uint bytes_to_read = byte_alignment; |
91 |
> |
if (_bytesToRead < byte_alignment) |
92 |
> |
{ |
93 |
> |
_bytesToRead = bytesToRead; |
94 |
> |
buffer = new byte[_bytesToRead]; |
95 |
> |
} |
96 |
> |
else |
97 |
> |
{ |
98 |
> |
_bytesToRead = byte_alignment; |
99 |
> |
buffer = new byte[byte_alignment]; |
100 |
> |
} |
101 |
|
IntPtr ptrBytesRead; |
102 |
< |
ProcessMemoryReaderApi.ReadProcessMemory(m_hProcess, (UIntPtr)address, buffer, bytes_to_read, out ptrBytesRead); |
102 |
> |
ProcessMemoryReaderApi.ReadProcessMemory(m_hProcess, (UIntPtr)address, buffer, _bytesToRead, out ptrBytesRead); |
103 |
|
bytesRead = ptrBytesRead.ToInt32(); |
104 |
|
bw.Write(buffer); |
105 |
|
bw.Flush(); |
106 |
< |
address += byte_alignment; |
106 |
> |
|
107 |
> |
if (_bytesToRead < byte_alignment) |
108 |
> |
{ |
109 |
> |
i += _bytesToRead; |
110 |
> |
address += _bytesToRead; |
111 |
> |
} |
112 |
> |
else |
113 |
> |
{ |
114 |
> |
i += byte_alignment; |
115 |
> |
address += byte_alignment; |
116 |
> |
} |
117 |
> |
|
118 |
> |
|
119 |
|
} |
120 |
|
bw.Close(); |
121 |
|
} |