-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathProcessSharp.cs
More file actions
177 lines (152 loc) · 5.87 KB
/
Copy pathProcessSharp.cs
File metadata and controls
177 lines (152 loc) · 5.87 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
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;
namespace Process.NET
{
/// <summary>
/// A class that offsers several tools to interact with a process.
/// </summary>
/// <seealso cref="IProcess" />
public class ProcessSharp : IProcess
{
/// <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>
/// Raises when the <see cref="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 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>
/// 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);
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;
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();
}
}
}
}