Repository navigation
Expand file tree
/
Copy pathProcessSharp.cs
More file actions
241 lines (179 loc) · 7.88 KB
/
Copy pathProcessSharp.cs
File metadata and controls
241 lines (179 loc) · 7.88 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Process.NET.Execution;
using Process.NET.Memory;
using Process.NET.Modules;
using Process.NET.Native.Types;
using Process.NET.Threads;
using Process.NET.Utilities;
using Process.NET.Windows;
namespace Process.NET
{
/// <summary>A class that offsers several tools to interact with a process.</summary>
/// <seealso cref="IProcess" />
public class ProcessSharp<TExecDesc> : ProcessSharp, IProcess<TExecDesc>
where TExecDesc : class
{
public ProcessSharp(System.Diagnostics.Process native,
MemoryType type,
bool initializeRemoteProcedures = false,
Dictionary<string, int> cachedAddresses = null,
params object[] procedureConstructorParams)
: base(native, type)
{
RemoteExecution = new RemoteExecution<TExecDesc>(this, initializeRemoteProcedures, cachedAddresses, procedureConstructorParams);
}
public ProcessSharp(string processName,
MemoryType type,
bool initializeRemoteProcedures = false,
Dictionary<string, int> cachedAddresses = null,
params object[] procedureConstructorParams)
: this(ProcessHelper.FromName(processName),
type,
initializeRemoteProcedures,
cachedAddresses,
procedureConstructorParams) { }
public ProcessSharp(int processId,
MemoryType type,
bool initializeRemoteProcedures = false,
Dictionary<string, int> cachedAddresses = null,
params object[] procedureConstructorParams)
: this(ProcessHelper.FromProcessId(processId),
type,
initializeRemoteProcedures,
cachedAddresses,
procedureConstructorParams) { }
public TExecDesc Procedures { get; set; }
}
/// <summary>A class that offsers several tools to interact with a process.</summary>
/// <seealso cref="IProcess" />
public class ProcessSharp : MarshalByRefObject, IProcess
{
#region Properties & Fields - Non-Public
protected bool IsDisposed { get; set; }
protected bool MustBeDisposed { get; set; } = true;
#endregion
#region Constructors
/// <summary>Initializes a new instance of the <see cref="ProcessSharp" /> class.</summary>
/// <param name="native">The native process.</param>
/// <param name="type">The type of memory being manipulated.</param>
public ProcessSharp(System.Diagnostics.Process native,
MemoryType type)
{
native.EnableRaisingEvents = true;
native.Exited += (s,
e) =>
{
ProcessExited?.Invoke(s,
e);
HandleProcessExiting();
};
Native = native;
Handle = MemoryHelper.OpenProcess(ProcessAccessFlags.AllAccess,
Native.Id);
switch (type)
{
case MemoryType.Local:
Memory = new LocalProcessMemory(Handle);
break;
case MemoryType.Remote:
Memory = new ExternalProcessMemory(Handle);
break;
default:
throw new ArgumentOutOfRangeException(nameof(type),
type,
null);
}
native.ErrorDataReceived += OutputDataReceived;
native.OutputDataReceived += OutputDataReceived;
ThreadFactory = new ThreadFactory(this);
ModuleFactory = new ModuleFactory(this);
MemoryFactory = new MemoryFactory(this);
WindowFactory = new WindowFactory(this);
}
/// <summary>Initializes a new instance of the <see cref="ProcessSharp" /> class.</summary>
/// <param name="processName">Name of the process.</param>
/// <param name="type">The type of memory being manipulated.</param>
public ProcessSharp(string processName,
MemoryType type) : this(ProcessHelper.FromName(processName),
type) { }
/// <summary>Initializes a new instance of the <see cref="ProcessSharp" /> class.</summary>
/// <param name="processId">The process id of the process to open with all rights.</param>
/// <param name="type">The type of memory being manipulated.</param>
public ProcessSharp(int processId,
MemoryType type) : this(ProcessHelper.FromProcessId(processId),
type) { }
/// <summary>Releases unmanaged and - optionally - managed resources.</summary>
public virtual void Dispose()
{
if (!IsDisposed)
{
IsDisposed = true;
OnDispose?.Invoke(this,
EventArgs.Empty);
ThreadFactory?.Dispose();
ModuleFactory?.Dispose();
MemoryFactory?.Dispose();
WindowFactory?.Dispose();
Handle?.Close();
GC.SuppressFinalize(this);
}
}
#endregion
#region Properties Impl - Public
/// <summary>Class for reading and writing memory.</summary>
public IMemory Memory { get; set; }
/// <summary>Provide access to the opened process.</summary>
public System.Diagnostics.Process Native { get; set; }
/// <summary>The process handle opened with all rights.</summary>
public SafeMemoryHandle Handle { get; set; }
/// <summary>Factory for manipulating threads.</summary>
public IThreadFactory ThreadFactory { get; set; }
/// <summary>Factory for manipulating modules and libraries.</summary>
public IModuleFactory ModuleFactory { get; set; }
/// <summary>Factory for manipulating memory space.</summary>
public IMemoryFactory MemoryFactory { get; set; }
/// <summary>Factory for manipulating windows.</summary>
public IWindowFactory WindowFactory { get; set; }
/// <summary>
/// Enable execution of remote procedures by loading IProcedure members from <typeparamref see="TExecDesc"/>
/// </summary>
public IRemoteExecution RemoteExecution { get; set; }
/// <summary>Gets the <see cref="IProcessModule" /> with the specified module name.</summary>
/// <param name="moduleName">Name of the module.</param>
/// <returns>IProcessModule.</returns>
public IProcessModule this[string moduleName] => ModuleFactory[moduleName];
/// <summary>Gets the <see cref="IPointer" /> with the specified address.</summary>
/// <param name="intPtr">The address the pointer is located at in memory.</param>
/// <returns>IPointer.</returns>
public IPointer this[IntPtr intPtr] => new MemoryPointer(this,
intPtr);
public IPointer this[ObjPtr objPtr] => new MemoryPointer(this,
objPtr);
#endregion
#region Methods Impl
~ProcessSharp()
{
if (MustBeDisposed)
Dispose();
}
#endregion
#region Methods
/// <summary>Handles the process exiting.</summary>
/// <remarks>Created 2012-02-15</remarks>
protected virtual void HandleProcessExiting() { }
private static void OutputDataReceived(object sender,
DataReceivedEventArgs e)
{
Trace.WriteLine(e.Data);
}
#endregion
#region Events
/// <summary>Raises when the <see cref="ProcessSharp" /> object is disposed.</summary>
public event EventHandler OnDispose;
/// <summary>Event queue for all listeners interested in ProcessExited events.</summary>
public event EventHandler ProcessExited;
#endregion
}
}