-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathMemoryRegion.cs
More file actions
97 lines (86 loc) · 3.59 KB
/
Copy pathMemoryRegion.cs
File metadata and controls
97 lines (86 loc) · 3.59 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System;
using Process.NET.Native.Types;
using Process.NET.Utilities;
namespace Process.NET.Memory
{
/// <summary>
/// Represents a contiguous block of memory in the remote process.
/// </summary>
public class MemoryRegion : MemoryPointer, IEquatable<MemoryRegion>
{
public MemoryRegion(IProcess processPlus, IntPtr baseAddress) : base(processPlus, baseAddress)
{
}
/// <summary>
/// Contains information about the memory.
/// </summary>
public MemoryBasicInformation Information => MemoryHelper.Query(Process.Handle, BaseAddress);
/// <summary>
/// Gets if the <see cref="MemoryRegion" /> is valid.
/// </summary>
public override bool IsValid => base.IsValid && Information.State != MemoryStateFlags.Free;
/// <summary>
/// Returns a value indicating whether this instance is equal to a specified object.
/// </summary>
public bool Equals(MemoryRegion other)
{
if (ReferenceEquals(null, other)) return false;
return ReferenceEquals(this, other) ||
(BaseAddress.Equals(other.BaseAddress) && Process.Equals(other.Process) &&
Information.RegionSize.Equals(other.Information.RegionSize));
}
/// <summary>
/// Changes the protection of the n next bytes in remote process.
/// </summary>
/// <param name="protection">The new protection to apply.</param>
/// <param name="mustBeDisposed">The resource will be automatically disposed when the finalizer collects the object.</param>
/// <returns>A new instance of the <see cref="MemoryProtection" /> class.</returns>
public MemoryProtection ChangeProtection(
MemoryProtectionFlags protection = MemoryProtectionFlags.ExecuteReadWrite, bool mustBeDisposed = true)
{
return new MemoryProtection(Process.Handle, BaseAddress, Information.RegionSize, protection, mustBeDisposed);
}
/// <summary>
/// Determines whether the specified object is equal to the current object.
/// </summary>
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
return obj.GetType() == GetType() && Equals((MemoryRegion) obj);
}
/// <summary>
/// Serves as a hash function for a particular type.
/// </summary>
public override int GetHashCode()
{
return BaseAddress.GetHashCode() ^ Process.GetHashCode() ^ Information.RegionSize.GetHashCode();
}
public static bool operator ==(MemoryRegion left, MemoryRegion right)
{
return Equals(left, right);
}
public static bool operator !=(MemoryRegion left, MemoryRegion right)
{
return !Equals(left, right);
}
/// <summary>
/// Releases the memory used by the region.
/// </summary>
public void Release()
{
// Release the memory
MemoryHelper.Free(Process.Handle, BaseAddress);
// Remove the pointer
BaseAddress = IntPtr.Zero;
}
/// <summary>
/// Returns a string that represents the current object.
/// </summary>
public override string ToString()
{
return
$"BaseAddress = 0x{BaseAddress.ToInt64():X} Size = 0x{Information.RegionSize:X} Protection = {Information.Protect}";
}
}
}