Skip to content

Commit 962d006

Browse files
committed
Implemented EnumerateRemoteSectionsAndModules.
1 parent fc3a7ec commit 962d006

File tree

3 files changed

+75
-17
lines changed

3 files changed

+75
-17
lines changed

Diff for: LoadBinaryPlugin.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
</ItemGroup>
7070
<ItemGroup>
7171
<Compile Include="LoadBinaryPluginExt.cs" />
72+
<Compile Include="MemoryMappedFileInfo.cs" />
7273
<Compile Include="Properties\AssemblyInfo.cs" />
7374
<Compile Include="Properties\Resources.Designer.cs">
7475
<AutoGen>True</AutoGen>

Diff for: LoadBinaryPluginExt.cs

+52-17
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
using System.Collections.Generic;
33
using System.Diagnostics.Contracts;
44
using System.Drawing;
5+
using System.IO;
56
using System.IO.MemoryMappedFiles;
67
using System.Windows.Forms;
78
using ReClassNET.Core;
89
using ReClassNET.Debugger;
10+
using ReClassNET.Memory;
911
using ReClassNET.Plugins;
1012

1113
namespace LoadBinaryPlugin
@@ -18,7 +20,7 @@ public class LoadBinaryPluginExt : Plugin, ICoreProcessFunctions
1820

1921
private string currentFile;
2022

21-
private Dictionary<IntPtr, MemoryMappedFile> openFiles;
23+
private Dictionary<IntPtr, MemoryMappedFileInfo> openFiles;
2224

2325
public override Image Icon => Properties.Resources.icon;
2426

@@ -29,7 +31,7 @@ public override bool Initialize(IPluginHost host)
2931

3032
host.Process.CoreFunctions.RegisterFunctions("Load Binary", this);
3133

32-
openFiles = new Dictionary<IntPtr, MemoryMappedFile>();
34+
openFiles = new Dictionary<IntPtr, MemoryMappedFileInfo>();
3335

3436
return true;
3537
}
@@ -38,17 +40,17 @@ public override void Terminate()
3840
{
3941
foreach (var kv in openFiles)
4042
{
41-
kv.Value.Dispose();
43+
kv.Value.File.Dispose();
4244
}
4345
openFiles.Clear();
4446

4547
host = null;
4648
}
4749

48-
/// <summary>Gets a <see cref="MemoryMappedFile"/> by its plugin internal identifier.</summary>
50+
/// <summary>Gets a <see cref="MemoryMappedFileInfo"/> by its plugin internal identifier.</summary>
4951
/// <param name="id">The identifier.</param>
5052
/// <returns>The file or null if the identifier doesn't exist.</returns>
51-
private MemoryMappedFile GetMappedFileById(IntPtr id)
53+
private MemoryMappedFileInfo GetMappedFileById(IntPtr id)
5254
{
5355
openFiles.TryGetValue(id, out var file);
5456
return file;
@@ -61,9 +63,9 @@ private void LogErrorAndRemoveFile(IntPtr id, Exception ex)
6163
{
6264
Contract.Requires(ex != null);
6365

64-
if (openFiles.TryGetValue(id, out var file))
66+
if (openFiles.TryGetValue(id, out var info))
6567
{
66-
file.Dispose();
68+
info.File.Dispose();
6769
}
6870

6971
openFiles.Remove(id);
@@ -94,11 +96,18 @@ public IntPtr OpenRemoteProcess(IntPtr id, ProcessAccess desiredAccess)
9496
{
9597
try
9698
{
97-
var file = MemoryMappedFile.CreateFromFile(currentFile);
99+
var mappedFile = MemoryMappedFile.CreateFromFile(currentFile);
98100

99-
var handle = file.SafeMemoryMappedFileHandle.DangerousGetHandle();
101+
var handle = mappedFile.SafeMemoryMappedFileHandle.DangerousGetHandle();
100102

101-
openFiles.Add(handle, file);
103+
openFiles.Add(
104+
handle,
105+
new MemoryMappedFileInfo(
106+
currentFile,
107+
(int)new FileInfo(currentFile).Length,
108+
mappedFile
109+
)
110+
);
102111

103112
return handle;
104113
}
@@ -118,11 +127,11 @@ public void CloseRemoteProcess(IntPtr process)
118127
{
119128
lock (sync)
120129
{
121-
if (openFiles.TryGetValue(process, out var file))
130+
if (openFiles.TryGetValue(process, out var info))
122131
{
123132
openFiles.Remove(process);
124133

125-
file.Dispose();
134+
info.File.Dispose();
126135
}
127136
}
128137
}
@@ -138,12 +147,12 @@ public bool ReadRemoteMemory(IntPtr process, IntPtr address, ref byte[] buffer,
138147
{
139148
lock (sync)
140149
{
141-
var file = GetMappedFileById(process);
142-
if (file != null)
150+
var info = GetMappedFileById(process);
151+
if (info != null)
143152
{
144153
try
145154
{
146-
using (var stream = file.CreateViewStream(address.ToInt64(), size))
155+
using (var stream = info.File.CreateViewStream(address.ToInt64(), size))
147156
{
148157
stream.Read(buffer, 0, size);
149158

@@ -198,6 +207,7 @@ public void EnumerateProcesses(EnumerateProcessCallback callbackProcess)
198207
var data = new EnumerateProcessData
199208
{
200209
Id = (IntPtr)currentFile.GetHashCode(),
210+
Name = Path.GetFileName(currentFile),
201211
Path = currentFile
202212
};
203213

@@ -206,13 +216,38 @@ public void EnumerateProcesses(EnumerateProcessCallback callbackProcess)
206216
}
207217
}
208218

209-
/// <summary>Not supported.</summary>
219+
/// <summary>Reports a single module and section for the loaded file.</summary>
210220
/// <param name="process">The process.</param>
211221
/// <param name="callbackSection">The callback which gets called for every section.</param>
212222
/// <param name="callbackModule">The callback which gets called for every module.</param>
213223
public void EnumerateRemoteSectionsAndModules(IntPtr process, EnumerateRemoteSectionCallback callbackSection, EnumerateRemoteModuleCallback callbackModule)
214224
{
215-
// Not supported.
225+
lock (sync)
226+
{
227+
var info = GetMappedFileById(process);
228+
if (info != null)
229+
{
230+
var module = new EnumerateRemoteModuleData
231+
{
232+
BaseAddress = IntPtr.Zero,
233+
Path = info.Path,
234+
Size = (IntPtr)info.Size
235+
};
236+
callbackModule(ref module);
237+
238+
var section = new EnumerateRemoteSectionData
239+
{
240+
BaseAddress = IntPtr.Zero,
241+
Size = (IntPtr)info.Size,
242+
Type = SectionType.Image,
243+
Category = SectionCategory.Unknown,
244+
ModulePath = info.Path,
245+
Name = string.Empty,
246+
Protection = SectionProtection.Read
247+
};
248+
callbackSection(ref section);
249+
}
250+
}
216251
}
217252

218253
public void ControlRemoteProcess(IntPtr process, ControlRemoteProcessAction action)

Diff for: MemoryMappedFileInfo.cs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System.Diagnostics.Contracts;
2+
using System.IO.MemoryMappedFiles;
3+
4+
namespace LoadBinaryPlugin
5+
{
6+
internal class MemoryMappedFileInfo
7+
{
8+
public string Path { get; }
9+
public int Size { get; }
10+
public MemoryMappedFile File { get; }
11+
12+
public MemoryMappedFileInfo(string path, int size, MemoryMappedFile file)
13+
{
14+
Contract.Requires(path != null);
15+
Contract.Requires(file != null);
16+
17+
Path = path;
18+
Size = size;
19+
File = file;
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)