Repository navigation
Expand file tree
/
Copy pathProcessSharp.cs
More file actions
186 lines (158 loc) · 5.72 KB
/
Copy pathProcessSharp.cs
File metadata and controls
186 lines (158 loc) · 5.72 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
using System;
using System.Diagnostics;
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;
using _Process = System.Diagnostics.Process;
namespace Process.NET
{
/// <summary>
/// A class that offsers several tools to interact with a process.
/// </summary>
/// <seealso cref="IProcess" />
public class ProcessSharp : IProcess
{
IMemory IProcess.Memory => Memory;
/// <summary>
/// Initializes a new instance of the <see cref="ProcessSharp" /> class.
/// </summary>
/// <param name="native">The native process.</param>
public ProcessSharp(_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>
/// initiatize a new instance of the ProcessSharp class
/// </summary>
/// <param name="processName"></param>
public ProcessSharp(string processName, MemoryType type) : this(ProcessHelper.FromName(processName), type)
{
}
/// <summary>
/// init a new instance of the ProcessSharp class
/// </summary>
/// <param name="processId"></param>
public ProcessSharp(int processId, MemoryType type) : this(ProcessHelper.FromProcessId(processId), type)
{
}
/// <summary>
/// Raises when the ProcessSharp object is disposed
/// </summary>
public event EventHandler OnDispose;
/// <summary>
/// Class for reading and writing memory.
/// </summary>
public IMemory Memory { get; set; }
/// <summary>
/// Provide access to the opened process.
/// </summary>
public _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; }
protected string ownerUser = null;
/// <summary>
/// Gets the name of the user this process belongs to.
/// </summary>
public string OwnerUser
{
get
{
if (string.IsNullOrWhiteSpace(ownerUser))
{
ownerUser = ProcessHelper.GetProcessUser(Native.Id);
}
return ownerUser;
}
}
public IProcessModule this[string moduleName] => ModuleFactory[moduleName];
public IPointer this[IntPtr intPtr] => new MemoryPointer(this, intPtr);
protected bool IsDisposed { get; set; }
protected bool MustBeDisposed { get; set; } = true;
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
public virtual void Dispose()
{
if (!IsDisposed)
{
IsDisposed = true;
// TODO Consider adding a null check here, or a nasty crash deadlock can make it in here occasionally.
// LOL little did you know nullable types would arrive soon after.
OnDispose?.Invoke(this, EventArgs.Empty);
ThreadFactory?.Dispose();
ModuleFactory?.Dispose();
MemoryFactory?.Dispose();
WindowFactory?.Dispose();
Handle?.Close();
GC.SuppressFinalize(this);
}
}
/// <summary>
/// Handles the process exiting.
/// </summary>
/// <remarks>Created 2012-02-15</remarks>
protected virtual void HandleProcessExiting()
{
}
/// <summary>
/// Event queue for all listeners interested in ProcessExited events.
/// </summary>
public event EventHandler ProcessExited;
private static void OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Trace.WriteLine(e.Data);
}
~ProcessSharp()
{
if (MustBeDisposed)
{
Dispose();
}
}
}
}