33using System . Diagnostics . Contracts ;
44using System . Drawing ;
55using System . IO . MemoryMappedFiles ;
6- using System . Runtime . InteropServices ;
76using System . Windows . Forms ;
7+ using ReClassNET . Core ;
8+ using ReClassNET . Debugger ;
89using ReClassNET . Memory ;
910using ReClassNET . Plugins ;
10- using RGiesecke . DllExport ;
11- using static ReClassNET . Memory . NativeHelper ;
1211
1312namespace LoadBinaryPlugin
1413{
15- public class LoadBinaryPluginExt : Plugin
14+ public class LoadBinaryPluginExt : Plugin , ICoreProcessFunctions
1615 {
17- private static object sync = new object ( ) ;
16+ private object sync = new object ( ) ;
1817
19- private static IPluginHost host ;
18+ private IPluginHost host ;
2019
21- private static string currentFile ;
20+ private string currentFile ;
2221
23- private static Dictionary < IntPtr , MemoryMappedFile > openFiles ;
22+ private Dictionary < IntPtr , MemoryMappedFile > openFiles ;
2423
2524 public override Image Icon => Properties . Resources . icon ;
2625
@@ -35,7 +34,9 @@ public override bool Initialize(IPluginHost host)
3534 throw new ArgumentNullException ( nameof ( host ) ) ;
3635 }
3736
38- LoadBinaryPluginExt . host = host ;
37+ this . host = host ;
38+
39+ host . Process . CoreFunctions . RegisterFunctions ( "Load Binary" , this ) ;
3940
4041 openFiles = new Dictionary < IntPtr , MemoryMappedFile > ( ) ;
4142
@@ -56,7 +57,7 @@ public override void Terminate()
5657 /// <summary>Gets a <see cref="MemoryMappedFile"/> by its plugin internal identifier.</summary>
5758 /// <param name="id">The identifier.</param>
5859 /// <returns>The file or null if the identifier doesn't exist.</returns>
59- private static MemoryMappedFile GetMappedFileById ( IntPtr id )
60+ private MemoryMappedFile GetMappedFileById ( IntPtr id )
6061 {
6162 MemoryMappedFile file ;
6263 openFiles . TryGetValue ( id , out file ) ;
@@ -66,7 +67,7 @@ private static MemoryMappedFile GetMappedFileById(IntPtr id)
6667 /// <summary>Logs the exception and removes the file.</summary>
6768 /// <param name="id">The identifier.</param>
6869 /// <param name="ex">The exception.</param>
69- private static void LogErrorAndRemoveFile ( IntPtr id , Exception ex )
70+ private void LogErrorAndRemoveFile ( IntPtr id , Exception ex )
7071 {
7172 Contract . Requires ( ex != null ) ;
7273
@@ -84,8 +85,7 @@ private static void LogErrorAndRemoveFile(IntPtr id, Exception ex)
8485 /// <summary>Queries if the file is valid.</summary>
8586 /// <param name="process">The file to check.</param>
8687 /// <returns>True if the file is valid, false if not.</returns>
87- [ DllExport ( CallingConvention = CallingConvention . StdCall ) ]
88- public static bool IsProcessValid ( IntPtr process )
88+ public bool IsProcessValid ( IntPtr process )
8989 {
9090 lock ( sync )
9191 {
@@ -97,8 +97,7 @@ public static bool IsProcessValid(IntPtr process)
9797 /// <param name="pid">The file id.</param>
9898 /// <param name="desiredAccess">The desired access. (ignored)</param>
9999 /// <returns>A plugin internal handle to the file.</returns>
100- [ DllExport ( CallingConvention = CallingConvention . StdCall ) ]
101- private static IntPtr OpenRemoteProcess ( IntPtr id , ProcessAccess desiredAccess )
100+ public IntPtr OpenRemoteProcess ( IntPtr id , ProcessAccess desiredAccess )
102101 {
103102 lock ( sync )
104103 {
@@ -126,8 +125,7 @@ private static IntPtr OpenRemoteProcess(IntPtr id, ProcessAccess desiredAccess)
126125
127126 /// <summary>Closes the file.</summary>
128127 /// <param name="process">The file to close.</param>
129- [ DllExport ( CallingConvention = CallingConvention . StdCall ) ]
130- private static void CloseRemoteProcess ( IntPtr process )
128+ public void CloseRemoteProcess ( IntPtr process )
131129 {
132130 lock ( sync )
133131 {
@@ -144,11 +142,11 @@ private static void CloseRemoteProcess(IntPtr process)
144142 /// <summary>Reads memory of the file.</summary>
145143 /// <param name="process">The process to read from.</param>
146144 /// <param name="address">The address to read from.</param>
147- /// <param name="buffer">The buffer to read into.</param>
145+ /// <param name="buffer">[out] The buffer to read into.</param>
146+ /// <param name="offset">The offset into the buffer.</param>
148147 /// <param name="size">The size of the memory to read.</param>
149148 /// <returns>True if it succeeds, false if it fails.</returns>
150- [ DllExport ( CallingConvention = CallingConvention . StdCall ) ]
151- private static bool ReadRemoteMemory ( IntPtr process , IntPtr address , IntPtr buffer , int size )
149+ public bool ReadRemoteMemory ( IntPtr process , IntPtr address , ref byte [ ] buffer , int offset , int size )
152150 {
153151 lock ( sync )
154152 {
@@ -159,10 +157,7 @@ private static bool ReadRemoteMemory(IntPtr process, IntPtr address, IntPtr buff
159157 {
160158 using ( var stream = file . CreateViewStream ( address . ToInt64 ( ) , size ) )
161159 {
162- var tempBuffer = new byte [ size ] ;
163- stream . Read ( tempBuffer , 0 , size ) ;
164-
165- Marshal . Copy ( tempBuffer , 0 , buffer , size ) ;
160+ stream . Read ( buffer , 0 , size ) ;
166161
167162 return true ;
168163 }
@@ -184,21 +179,20 @@ private static bool ReadRemoteMemory(IntPtr process, IntPtr address, IntPtr buff
184179 /// <summary>Not supported.</summary>
185180 /// <param name="process">The file to write to.</param>
186181 /// <param name="address">The address to write to.</param>
187- /// <param name="buffer">The memory to write.</param>
182+ /// <param name="buffer">[in] The memory to write.</param>
183+ /// <param name="offset">The offset into the buffer.</param>
188184 /// <param name="size">The size of the memory to write.</param>
189185 /// <returns>True if it succeeds, false if it fails.</returns>
190- [ DllExport ( CallingConvention = CallingConvention . StdCall ) ]
191- private static bool WriteRemoteMemory ( IntPtr process , IntPtr address , IntPtr buffer , int size )
186+ public bool WriteRemoteMemory ( IntPtr process , IntPtr address , ref byte [ ] buffer , int offset , int size )
192187 {
193- // No write support .
188+ // Not supported .
194189
195190 return false ;
196191 }
197192
198193 /// <summary>Opens a file browser dialog and reports the selected file.</summary>
199194 /// <param name="callbackProcess">The callback which gets called for the selected file.</param>
200- [ DllExport ( CallingConvention = CallingConvention . StdCall ) ]
201- private static void EnumerateProcesses ( EnumerateProcessCallback callbackProcess )
195+ public void EnumerateProcesses ( Action < Tuple < IntPtr , string > > callbackProcess )
202196 {
203197 if ( callbackProcess == null )
204198 {
@@ -213,13 +207,7 @@ private static void EnumerateProcesses(EnumerateProcessCallback callbackProcess)
213207 {
214208 currentFile = ofd . FileName ;
215209
216- var data = new EnumerateProcessData
217- {
218- Id = ( IntPtr ) currentFile . GetHashCode ( ) ,
219- Path = currentFile
220- } ;
221-
222- callbackProcess ( ref data ) ;
210+ callbackProcess ( Tuple . Create ( ( IntPtr ) currentFile . GetHashCode ( ) , currentFile ) ) ;
223211 }
224212 }
225213 }
@@ -228,10 +216,45 @@ private static void EnumerateProcesses(EnumerateProcessCallback callbackProcess)
228216 /// <param name="process">The process.</param>
229217 /// <param name="callbackSection">The callback which gets called for every section.</param>
230218 /// <param name="callbackModule">The callback which gets called for every module.</param>
231- [ DllExport ( CallingConvention = CallingConvention . StdCall ) ]
232- private static void EnumerateRemoteSectionsAndModules ( IntPtr process , EnumerateRemoteSectionCallback callbackSection , EnumerateRemoteModuleCallback callbackModule )
219+ public void EnumerateRemoteSectionsAndModules ( IntPtr process , Action < Section > callbackSection , Action < Module > callbackModule )
220+ {
221+ // Not supported.
222+ }
223+
224+ public void ControlRemoteProcess ( IntPtr process , ControlRemoteProcessAction action )
225+ {
226+ // Not supported.
227+ }
228+
229+ public bool AttachDebuggerToProcess ( IntPtr id )
230+ {
231+ // Not supported.
232+
233+ return false ;
234+ }
235+
236+ public void DetachDebuggerFromProcess ( IntPtr id )
237+ {
238+ // Not supported.
239+ }
240+
241+ public bool AwaitDebugEvent ( ref DebugEvent evt , int timeoutInMilliseconds )
242+ {
243+ // Not supported.
244+
245+ return false ;
246+ }
247+
248+ public void HandleDebugEvent ( ref DebugEvent evt )
233249 {
234- // No modules and sections here.
250+ // Not supported.
251+ }
252+
253+ public bool SetHardwareBreakpoint ( IntPtr id , IntPtr address , HardwareBreakpointRegister register , HardwareBreakpointTrigger trigger , HardwareBreakpointSize size , bool set )
254+ {
255+ // Not supported.
256+
257+ return false ;
235258 }
236259 }
237260}
0 commit comments