forked from DaxxTrias/Process.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExternalProcessMemory.cs
More file actions
80 lines (76 loc) · 3.18 KB
/
ExternalProcessMemory.cs
File metadata and controls
80 lines (76 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System;
using ProcessNET.Marshaling;
using ProcessNET.Native.Types;
using ProcessNET.Utilities;
namespace ProcessNET.Memory
{
/// <summary>
/// Class for memory editing a process.
/// </summary>
/// <seealso cref="ProcessMemory" />
public class ExternalProcessMemory : ProcessMemory
{
/// <summary>
/// Initializes a new instance of the <see cref="ExternalProcessMemory" /> class.
/// </summary>
/// <param name="handle">The open handle to the process which contains the memory of interest.</param>
public ExternalProcessMemory(SafeMemoryHandle handle) : base(handle)
{
}
/// <summary>
/// Writes a set of bytes to memory.
/// </summary>
/// <param name="intPtr">The address where the bytes start in memory.</param>
/// <param name="length">The length of the byte chunk to read from the memory address.</param>
/// <returns>
/// The byte array section read from memory.
/// </returns>
public override byte[] Read(IntPtr intPtr, int length)
{
return MemoryHelper.ReadBytes(Handle, intPtr, length);
}
/// <summary>
/// Writes a set of bytes to memory.
/// </summary>
/// <param name="intPtr">The address where the bytes start in memory.</param>
/// <param name="buffer">The buffer to read data onto, it's size determines the count of bytes to read.</param>
public override void Read(IntPtr intPtr, byte[] buffer)
{
MemoryHelper.ReadBytes(Handle, intPtr, buffer);
}
/// <summary>
/// Reads the value of a specified type from memory.
/// </summary>
/// <typeparam name="T">The type of the value.</typeparam>
/// <param name="intPtr">The address where the value is read.</param>
/// <returns>A value.</returns>
public override T Read<T>(IntPtr intPtr)
{
int size = MarshalCache<T>.Size;
if (typeof(T) == typeof(IntPtr))
size = Is32Bit ? 4 : 8;
return MarshalType<T>.ByteArrayToObject(Read(intPtr, size));
}
/// <summary>
/// Writes the values of a specified type to memory.
/// </summary>
/// <typeparam name="T">The type of the value.</typeparam>
/// <param name="intPtr">The address where the value is written.</param>
/// <param name="value">The value to write.</param>
public override void Write<T>(IntPtr intPtr, T value)
{
Write(intPtr, MarshalType<T>.ObjectToByteArray(value));
}
/// <summary>
/// Write an array of bytes in the remote process.
/// </summary>
/// <param name="intPtr">The address where the array is written.</param>
/// <param name="bytesToWrite">The array of bytes to write.</param>
public override int Write(IntPtr intPtr, byte[] bytesToWrite)
{
using (new MemoryProtection(Handle, intPtr, MarshalType<byte>.Size * bytesToWrite.Length))
MemoryHelper.WriteBytes(Handle, intPtr, bytesToWrite);
return bytesToWrite.Length;
}
}
}