warbler / SharpMonoInjector

A tool for injecting assemblies into Mono embedded applications, commonly Unity Engine based games
MIT License
310 stars 108 forks source link

How to run mono_runtime_invoke with parameters? #10

Closed hongde88 closed 5 years ago

hongde88 commented 5 years ago

I've noticed the library only supports injecting with no parameters in Load method. However, I'm trying to pass a string to method Load in my Loader. How do I achieve this with some modification of the library to accept a method with parameters? Thank you!

warbler commented 5 years ago

In order to do this you would need to do two things.

Firstly, the last parameter passed to mono_class_get_method_from_name is the parameter count of the method. The library currently passes 0, so you would have to pass 1 instead if your method takes one parameter.

Secondly, you need to create a MonoString in the target process and place it in an array. That array would then be passed to mono_runtime_invoke (the third parameter).

These are the steps:

  1. Create a MonoString in the target process by calling mono_string_new
  2. Allocate enough bytes to store a pointer in the target process
  3. Write the address of the MonoString to the allocated memory
  4. Pass the address of the allocated memory to mono_runtime_invoke

If you have trouble getting it to work I'd be happy to help you out further, so let me know how it goes.

hongde88 commented 5 years ago

Hi warbler, thank you so much for your quick reply. I was able to run mono_runtime_invoke with parameters following your instructions 💯 However, I came across another issue that I need some help from you. So basically, after injecting and ejecting a mono hack DLL, I want to re-inject with a new version of the hack (modifying some stuff in the mono script) and it seems like mono keeps using the old version. In order for the game to pick up my new version, I need to restart it and inject again. Do you know if there's a way to re-inject a new version of a hack without restarting the game? Thank you in advance!

hongde88 commented 5 years ago

@warbler I just asked this question in mono github issues and they said reloading is currently not supported by mono. So I'm gonna close this issue. However, if you ever find a workaround for this, please let me know. Thank you so much for your time and help!