Skip to content

Commit f08f3d7

Browse files
committed
Add partial read.
1 parent 2772e13 commit f08f3d7

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

Memory/NativeHelper.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,12 +311,17 @@ public void CloseRemoteProcess(IntPtr process)
311311
closeRemoteProcessDelegate(process);
312312
}
313313

314-
public bool ReadRemoteMemory(IntPtr process, IntPtr address, byte[] buffer, int size)
314+
public bool ReadRemoteMemory(IntPtr process, IntPtr address, byte[] buffer, int offset, int length)
315315
{
316316
Contract.Requires(buffer != null);
317317

318+
if (offset + length >= buffer.Length)
319+
{
320+
throw new ArgumentOutOfRangeException("offset + length");
321+
}
322+
318323
GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
319-
var result = readRemoteMemoryDelegate(process, address, handle.AddrOfPinnedObject(), size);
324+
var result = readRemoteMemoryDelegate(process, address, handle.AddrOfPinnedObject() + offset, length);
320325
handle.Free();
321326

322327
return result;

Memory/RemoteProcess.cs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,35 @@ public RemoteProcess(NativeHelper nativeHelper)
5151
/// <summary>Reads remote memory from the address into the buffer.</summary>
5252
/// <param name="address">The address to read from.</param>
5353
/// <param name="data">[out] The data buffer to fill. If the remote process is not valid, the buffer will get filled with zeros.</param>
54-
public void ReadRemoteMemoryIntoBuffer(IntPtr address, ref byte[] data)
54+
public bool ReadRemoteMemoryIntoBuffer(IntPtr address, ref byte[] buffer)
5555
{
56-
Contract.Requires(data != null);
56+
Contract.Requires(buffer != null);
57+
58+
return ReadRemoteMemoryIntoBuffer(address, ref buffer, 0, buffer.Length);
59+
}
60+
61+
/// <summary>Reads remote memory from the address into the buffer.</summary>
62+
/// <param name="address">The address to read from.</param>
63+
/// <param name="data">[out] The data buffer to fill. If the remote process is not valid, the buffer will get filled with zeros.</param>
64+
/// <param name="offset">The offset in the data.</param>
65+
/// <param name="length">The number of bytes to read.</param>
66+
public bool ReadRemoteMemoryIntoBuffer(IntPtr address, ref byte[] buffer, int offset, int length)
67+
{
68+
Contract.Requires(buffer != null);
69+
Contract.Requires(offset >= 0);
70+
Contract.Requires(length >= 0);
71+
Contract.Requires(offset + length < buffer.Length);
5772

5873
if (!IsValid)
5974
{
6075
Process = null;
6176

62-
data.FillWithZero();
77+
buffer.FillWithZero();
6378

64-
return;
79+
return false;
6580
}
6681

67-
nativeHelper.ReadRemoteMemory(Process.Handle, address, data, data.Length);
82+
return nativeHelper.ReadRemoteMemory(Process.Handle, address, buffer, offset, length);
6883
}
6984

7085
/// <summary>Reads <paramref name="size"/> bytes from the address in the remote process.</summary>

0 commit comments

Comments
 (0)