@@ -308,7 +308,11 @@ private void CloseAssembly(IntPtr assembly)
308
308
309
309
private IntPtr Execute ( IntPtr address , params IntPtr [ ] args )
310
310
{
311
- byte [ ] code = Assemble ( address , args ) ;
311
+ IntPtr retValPtr = Is64Bit
312
+ ? _memory . AllocateAndWrite ( ( long ) 0 )
313
+ : _memory . AllocateAndWrite ( 0 ) ;
314
+
315
+ byte [ ] code = Assemble ( address , retValPtr , args ) ;
312
316
IntPtr alloc = _memory . AllocateAndWrite ( code ) ;
313
317
314
318
IntPtr thread = Native . CreateRemoteThread (
@@ -322,23 +326,24 @@ private IntPtr Execute(IntPtr address, params IntPtr[] args)
322
326
if ( result == WaitResult . WAIT_FAILED )
323
327
throw new InjectorException ( "Failed to wait for a remote thread" , new Win32Exception ( Marshal . GetLastWin32Error ( ) ) ) ;
324
328
325
- if ( ! Native . GetExitCodeThread ( thread , out IntPtr exitCode ) )
326
- throw new InjectorException ( "Failed to get the exit code of a remote thread" , new Win32Exception ( Marshal . GetLastWin32Error ( ) ) ) ;
329
+ IntPtr ret = Is64Bit
330
+ ? ( IntPtr ) _memory . ReadLong ( retValPtr )
331
+ : ( IntPtr ) _memory . ReadInt ( retValPtr ) ;
327
332
328
- if ( ( long ) exitCode == 0x00000000C0000005 )
333
+ if ( ( long ) ret == 0x00000000C0000005 )
329
334
throw new InjectorException ( $ "An access violation occurred while executing { Exports . First ( e => e . Value == address ) . Key } ()") ;
330
335
331
- return exitCode ;
336
+ return ret ;
332
337
}
333
338
334
- private byte [ ] Assemble ( IntPtr address , IntPtr [ ] args )
339
+ private byte [ ] Assemble ( IntPtr functionPtr , IntPtr retValPtr , IntPtr [ ] args )
335
340
{
336
341
return Is64Bit
337
- ? Assemble64 ( address , args )
338
- : Assemble86 ( address , args ) ;
342
+ ? Assemble64 ( functionPtr , retValPtr , args )
343
+ : Assemble86 ( functionPtr , retValPtr , args ) ;
339
344
}
340
345
341
- private byte [ ] Assemble86 ( IntPtr address , IntPtr [ ] args )
346
+ private byte [ ] Assemble86 ( IntPtr functionPtr , IntPtr retValPtr , IntPtr [ ] args )
342
347
{
343
348
Assembler asm = new Assembler ( ) ;
344
349
@@ -352,15 +357,16 @@ private byte[] Assemble86(IntPtr address, IntPtr[] args)
352
357
for ( int i = args . Length - 1 ; i >= 0 ; i -- )
353
358
asm . Push ( args [ i ] ) ;
354
359
355
- asm . MovEax ( address ) ;
360
+ asm . MovEax ( functionPtr ) ;
356
361
asm . CallEax ( ) ;
357
362
asm . AddEsp ( ( byte ) ( args . Length * 4 ) ) ;
363
+ asm . MovEaxTo ( retValPtr ) ;
358
364
asm . Return ( ) ;
359
365
360
366
return asm . ToByteArray ( ) ;
361
367
}
362
368
363
- private byte [ ] Assemble64 ( IntPtr address , IntPtr [ ] args )
369
+ private byte [ ] Assemble64 ( IntPtr functionPtr , IntPtr retValPtr , IntPtr [ ] args )
364
370
{
365
371
Assembler asm = new Assembler ( ) ;
366
372
@@ -372,7 +378,7 @@ private byte[] Assemble64(IntPtr address, IntPtr[] args)
372
378
asm . CallRax ( ) ;
373
379
}
374
380
375
- asm . MovRax ( address ) ;
381
+ asm . MovRax ( functionPtr ) ;
376
382
377
383
for ( int i = 0 ; i < args . Length ; i ++ ) {
378
384
switch ( i ) {
@@ -393,6 +399,7 @@ private byte[] Assemble64(IntPtr address, IntPtr[] args)
393
399
394
400
asm . CallRax ( ) ;
395
401
asm . AddRsp ( 40 ) ;
402
+ asm . MovRaxTo ( retValPtr ) ;
396
403
asm . Return ( ) ;
397
404
398
405
return asm . ToByteArray ( ) ;
0 commit comments