-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcessMemory.cs
128 lines (109 loc) · 4.08 KB
/
ProcessMemory.cs
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using System;
using System.Diagnostics;
using System.Linq;
using JHelper.Common.ProcessInterop.API;
namespace JHelper.Common.ProcessInterop;
/// <summary>
/// Provides functionality for interacting with the memory of an external process.
/// </summary>
public partial class ProcessMemory : IDisposable
{
/// <summary>
/// Gets the process ID of the attached process.
/// </summary>
public int Id { get; }
/// <summary>
/// Internal handle to the external process.
/// </summary>
private readonly IntPtr pHandle;
/// <summary>
/// The name of the hooked process
/// </summary>
public string ProcessName => processName ??= WinAPI.GetProcessName(pHandle);
private string? processName = null;
/// <summary>
/// Indicates whether the attached process is 64-bit.
/// </summary>
public bool Is64Bit => is64Bit ??= WinAPI.Is64Bit(pHandle);
private bool? is64Bit = null;
/// <summary>
/// Gets the size of pointers for the process (8 bytes for 64-bit, 4 bytes for 32-bit).
/// </summary>
public byte PointerSize => pointerSize ??= (byte)(Is64Bit ? 8 : 4);
private byte? pointerSize = null;
/// <summary>
/// Checks if the process handle is still valid and the process is open.
/// </summary>
public bool IsOpen => WinAPI.IsOpen(pHandle);
/// <summary>
/// The main module (executable) of the attached process.
/// </summary>
public ProcessModule MainModule => mainModule ??= new ProcessModuleCollection(pHandle, true).First();
private ProcessModule? mainModule = null;
/// <summary>
/// Gets the collection of loaded modules (DLLs or shared libraries) in the process.
/// </summary>
public ProcessModuleCollection Modules
{
get
{
modules ??= new ProcessModuleCollection(pHandle, false);
return modules.Value;
}
}
private ProcessModuleCollection? modules = null;
/// <summary>
/// Gets a collection of the memory pages in the process.
/// </summary>
public MemoryPagesSnapshot MemoryPages
{
get
{
memoryPages ??= new MemoryPagesSnapshot(pHandle, true);
return memoryPages.Value;
}
}
private MemoryPagesSnapshot? memoryPages = null;
private ProcessMemory(int processId, IntPtr handle)
{
Id = processId;
pHandle = handle;
}
/// <summary>
/// Attaches to an external process by its process ID.
/// </summary>
/// <param name="processId">The ID of the process to hook into.</param>
/// <returns>A ProcessMemory instance if successful, otherwise null.</returns>
public static ProcessMemory? HookProcess(int processId) => WinAPI.OpenProcess(processId, out IntPtr handle) ? new(processId, handle) : null;
/// <summary>
/// Attaches to an external process using a Process object.
/// </summary>
/// <param name="process">The Process object representing the target process.</param>
/// <returns>A ProcessMemory instance if successful, otherwise null.</returns>
public static ProcessMemory? HookProcess(Process process) => WinAPI.OpenProcess(process.Id, out IntPtr handle) ? new(process.Id, handle) : null;
/// <summary>
/// Attaches to an external process by its name.
/// </summary>
/// <param name="processName">The name of the process to hook into.</param>
/// <returns>A ProcessMemory instance if successful, otherwise null.</returns>
public static ProcessMemory? HookProcess(string processName)
{
if (!WinAPI.OpenProcessHandleByName(processName, out int pId, out IntPtr pHandle))
return null;
return new ProcessMemory(pId, pHandle);
}
/// <summary>
/// Releases resources used by the ProcessMemory instance and closes the process handle.
/// </summary>
public void Dispose()
{
if (!_disposed)
{
if (pHandle != IntPtr.Zero)
WinAPI.CloseProcessHandle(pHandle);
}
_disposed = true;
GC.SuppressFinalize(this);
}
private bool _disposed = false;
}