Skip to content

Commit 9dd9135

Browse files
committed
Removed ProcessInfo functionality.
1 parent 1113096 commit 9dd9135

File tree

5 files changed

+88
-86
lines changed

5 files changed

+88
-86
lines changed

Forms/MainForm.cs

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ public MainForm(NativeHelper nativeHelper)
5353
remoteProcess = new RemoteProcess(nativeHelper);
5454
remoteProcess.ProcessChanged += delegate (RemoteProcess sender)
5555
{
56-
if (sender.Process == null)
56+
if (!sender.IsValid)
5757
{
5858
Text = Constants.ApplicationName;
5959
processInfoToolStripStatusLabel.Text = "No process selected";
6060
}
6161
else
6262
{
63-
var text = $"{sender.Process.Name} (PID: {sender.Process.Id})";
63+
var text = $"{sender.UnderlayingProcess.Name} (PID: {sender.UnderlayingProcess.Id.ToString()})";
6464

6565
Text = $"{Constants.ApplicationName} - {text}";
6666
processInfoToolStripStatusLabel.Text = text;
@@ -146,10 +146,7 @@ private async void MainForm_FormClosing(object sender, FormClosingEventArgs e)
146146
Close();
147147
}
148148

149-
if (remoteProcess.Process != null)
150-
{
151-
remoteProcess.Process.Close();
152-
}
149+
remoteProcess.Dispose();
153150
}
154151

155152
#region Menustrip
@@ -162,32 +159,32 @@ private void attachToProcessToolStripMenuItem_Click(object sender, EventArgs e)
162159
{
163160
if (pb.SelectedProcess != null)
164161
{
165-
DetachFromCurrentProcess();
162+
remoteProcess.Close();
166163

167-
remoteProcess.Process = pb.SelectedProcess;
164+
remoteProcess.Open(pb.SelectedProcess);
168165
remoteProcess.UpdateProcessInformations();
169166
if (pb.LoadSymbols)
170167
{
171168
LoadAllSymbolsForCurrentProcess();
172169
}
173170

174-
Program.Settings.LastProcess = remoteProcess.Process.Name;
171+
Program.Settings.LastProcess = remoteProcess.UnderlayingProcess.Name;
175172
}
176173
}
177174
}
178175
}
179176

180177
private void detachToolStripMenuItem_Click(object sender, EventArgs e)
181178
{
182-
DetachFromCurrentProcess();
179+
remoteProcess.Close();
183180
}
184181

185182
private void newClassToolStripButton_Click(object sender, EventArgs e)
186183
{
187184
var node = ClassNode.Create();
188185
node.AddBytes(64);
189186

190-
var mainModule = remoteProcess.GetModuleByName(remoteProcess.Process?.Name);
187+
var mainModule = remoteProcess.GetModuleByName(remoteProcess.UnderlayingProcess?.Name);
191188
if (mainModule != null)
192189
{
193190
node.Address = mainModule.Start;
@@ -327,7 +324,7 @@ private void ControlRemoteProcessToolStripMenuItem_Click(object sender, EventArg
327324
action = ControlRemoteProcessAction.Suspend;
328325
}
329326

330-
nativeHelper.ControlRemoteProcess(remoteProcess.Process.Handle, action);
327+
remoteProcess.ControlRemoteProcess(action);
331328
}
332329

333330
private void loadSymbolToolStripMenuItem_Click(object sender, EventArgs e)
@@ -579,16 +576,6 @@ internal void DeregisterNodeType(Type type)
579576
memoryViewControl.DeregisterNodeType(type);
580577
}
581578

582-
/// <summary>Detach from current process.</summary>
583-
private void DetachFromCurrentProcess()
584-
{
585-
if (remoteProcess.Process != null)
586-
{
587-
remoteProcess.Process.Close();
588-
remoteProcess.Process = null;
589-
}
590-
}
591-
592579
/// <summary>Shows the code form with the given <paramref name="generator"/>.</summary>
593580
/// <param name="generator">The generator.</param>
594581
private void ShowCodeForm(ICodeGenerator generator)

Forms/ProcessBrowserForm.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public ProcessInfo SelectedProcess
3131
var row = (processDataGridView.SelectedRows.Cast<DataGridViewRow>().FirstOrDefault()?.DataBoundItem as DataRowView)?.Row;
3232
if (row != null)
3333
{
34-
return new ProcessInfo(nativeHelper, row.Field<IntPtr>("id"), row.Field<string>("name"), row.Field<string>("path"));
34+
return new ProcessInfo(row.Field<IntPtr>("id"), row.Field<string>("name"), row.Field<string>("path"));
3535
}
3636
return null;
3737
}

Forms/ProcessInfoForm.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ public ProcessInfoForm(RemoteProcess process, ClassNodeView classesView)
5757
modules.Columns.Add("path", typeof(string));
5858
modules.Columns.Add("module", typeof(Module));
5959

60-
process.NativeHelper.EnumerateRemoteSectionsAndModules(
61-
process.Process.Handle,
60+
process.EnumerateRemoteSectionsAndModules(
6261
delegate (Section section)
6362
{
6463
var row = sections.NewRow();

Memory/ProcessInfo.cs

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,22 @@
11
using System;
22
using System.Diagnostics.Contracts;
3-
using ReClassNET.Native;
4-
using ReClassNET.Util;
53

64
namespace ReClassNET.Memory
75
{
8-
public class ProcessInfo : IDisposable
6+
public class ProcessInfo
97
{
10-
private readonly object sync = new object();
11-
12-
private readonly NativeHelper nativeHelper;
13-
14-
private IntPtr handle;
15-
168
public IntPtr Id { get; }
17-
public IntPtr Handle => Open();
189
public string Name { get; }
1910
public string Path { get; }
2011

21-
public ProcessInfo(NativeHelper nativeHelper, IntPtr id, string name, string path)
12+
public ProcessInfo(IntPtr id, string name, string path)
2213
{
23-
Contract.Requires(nativeHelper != null);
2414
Contract.Requires(name != null);
2515
Contract.Requires(path != null);
2616

27-
this.nativeHelper = nativeHelper;
28-
2917
Id = id;
3018
Name = name;
3119
Path = path;
3220
}
33-
34-
public void Dispose()
35-
{
36-
Close();
37-
}
38-
39-
public IntPtr Open()
40-
{
41-
if (handle.IsNull())
42-
{
43-
lock (sync)
44-
{
45-
if (handle.IsNull())
46-
{
47-
handle = nativeHelper.OpenRemoteProcess(Id, ProcessAccess.Full);
48-
}
49-
}
50-
}
51-
return handle;
52-
}
53-
54-
public void Close()
55-
{
56-
if (!handle.IsNull())
57-
{
58-
lock (sync)
59-
{
60-
if (!handle.IsNull())
61-
{
62-
nativeHelper.CloseRemoteProcess(handle);
63-
64-
handle = IntPtr.Zero;
65-
}
66-
}
67-
}
68-
}
6921
}
7022
}

Memory/RemoteProcess.cs

Lines changed: 75 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,16 @@
1414

1515
namespace ReClassNET.Memory
1616
{
17-
public class RemoteProcess
17+
public class RemoteProcess : IDisposable
1818
{
19+
private readonly object processSync = new object();
20+
1921
private readonly NativeHelper nativeHelper;
2022
public NativeHelper NativeHelper => nativeHelper;
2123

2224
private ProcessInfo process;
23-
public ProcessInfo Process
24-
{
25-
get { return process; }
26-
set { if (process != value) { process = value; rttiCache.Clear(); ProcessChanged?.Invoke(this); } }
27-
}
25+
private IntPtr handle;
26+
public ProcessInfo UnderlayingProcess => process;
2827

2928
public delegate void RemoteProcessChangedEvent(RemoteProcess sender);
3029
public event RemoteProcessChangedEvent ProcessChanged;
@@ -38,7 +37,7 @@ public ProcessInfo Process
3837
private readonly SymbolStore symbols = new SymbolStore();
3938
public SymbolStore Symbols => symbols;
4039

41-
public bool IsValid => process != null && nativeHelper.IsProcessValid(process.Handle);
40+
public bool IsValid => process != null && nativeHelper.IsProcessValid(handle);
4241

4342
public RemoteProcess(NativeHelper nativeHelper)
4443
{
@@ -47,6 +46,51 @@ public RemoteProcess(NativeHelper nativeHelper)
4746
this.nativeHelper = nativeHelper;
4847
}
4948

49+
public void Dispose()
50+
{
51+
Close();
52+
}
53+
54+
public void Open(ProcessInfo info)
55+
{
56+
Contract.Requires(info != null);
57+
58+
if (process != info)
59+
{
60+
lock (processSync)
61+
{
62+
Close();
63+
64+
rttiCache.Clear();
65+
66+
process = info;
67+
68+
handle = nativeHelper.OpenRemoteProcess(process.Id, ProcessAccess.Full);
69+
}
70+
71+
ProcessChanged?.Invoke(this);
72+
}
73+
}
74+
75+
public void Close()
76+
{
77+
if (process != null)
78+
{
79+
lock (processSync)
80+
{
81+
//detach debugger, remove breakpoints
82+
83+
nativeHelper.CloseRemoteProcess(handle);
84+
85+
handle = IntPtr.Zero;
86+
87+
process = null;
88+
}
89+
90+
ProcessChanged?.Invoke(this);
91+
}
92+
}
93+
5094
#region ReadMemory
5195

5296
/// <summary>Reads remote memory from the address into the buffer.</summary>
@@ -76,14 +120,14 @@ public bool ReadRemoteMemoryIntoBuffer(IntPtr address, ref byte[] buffer, int of
76120

77121
if (!IsValid)
78122
{
79-
Process = null;
123+
Close();
80124

81125
buffer.FillWithZero();
82126

83127
return false;
84128
}
85129

86-
return nativeHelper.ReadRemoteMemory(Process.Handle, address, buffer, offset, length);
130+
return nativeHelper.ReadRemoteMemory(handle, address, buffer, offset, length);
87131
}
88132

89133
/// <summary>Reads <paramref name="size"/> bytes from the address in the remote process.</summary>
@@ -344,7 +388,7 @@ public bool WriteRemoteMemory(IntPtr address, byte[] data)
344388
return false;
345389
}
346390

347-
return nativeHelper.WriteRemoteMemory(Process.Handle, address, data, data.Length);
391+
return nativeHelper.WriteRemoteMemory(handle, address, data, data.Length);
348392
}
349393

350394
/// <summary>Writes the given <paramref name="value"/> to the <paramref name="address"/> in the remote process.</summary>
@@ -417,6 +461,16 @@ public string GetNamedAddress(IntPtr address)
417461
return null;
418462
}
419463

464+
public void EnumerateRemoteSectionsAndModules(Action<Section> callbackSection, Action<Module> callbackModule)
465+
{
466+
if (!IsValid)
467+
{
468+
return;
469+
}
470+
471+
nativeHelper.EnumerateRemoteSectionsAndModules(handle, callbackSection, callbackModule);
472+
}
473+
420474
/// <summary>Updates the process informations.</summary>
421475
public void UpdateProcessInformations()
422476
{
@@ -448,7 +502,7 @@ public Task UpdateProcessInformationsAsync()
448502
var newModules = new List<Module>();
449503
var newSections = new List<Section>();
450504

451-
nativeHelper.EnumerateRemoteSectionsAndModules(process.Handle, newSections.Add, newModules.Add);
505+
EnumerateRemoteSectionsAndModules(newSections.Add, newModules.Add);
452506

453507
newModules.Sort((m1, m2) => m1.Start.CompareTo(m2.Start));
454508
newSections.Sort((s1, s2) => s1.Start.CompareTo(s2.Start));
@@ -535,5 +589,15 @@ public Task LoadAllSymbolsAsync(IProgress<Tuple<Module, IEnumerable<Module>>> pr
535589
TaskScheduler.FromCurrentSynchronizationContext()
536590
);
537591
}
592+
593+
public void ControlRemoteProcess(ControlRemoteProcessAction action)
594+
{
595+
if (!IsValid)
596+
{
597+
return;
598+
}
599+
600+
nativeHelper.ControlRemoteProcess(handle, action);
601+
}
538602
}
539603
}

0 commit comments

Comments
 (0)