0

Does anybody know a hooking/detours library that can hook Dotnet Methods? I know that moles can do it in the context of running unit tests. However it uses the profiler and really isn't suitable or designed to use in your own library, plus you can't hook internal methods or static with it.

klumsy
  • 4,081
  • 5
  • 32
  • 42
  • https://stackoverflow.com/questions/7299097/dynamically-replace-the-contents-of-a-c-sharp-method/42043003#42043003 – Sardelka Jun 28 '23 at 10:22

2 Answers2

1

Non that I know off. In my company i created one myself. You have to use a profiler to do that. Also, please note that if a native dll exists you won't be able to instrument the function, you have to otherwise delete it or do something else like block it with detours using loadlibrary hook.

Finally, the current method of doing things is to edit the function yourself or replace it altogether. It is a very dangerous thing to do for stability reasons. There are more sophisticated ways but first you have to start with the basics. IIRC i used this one to learn about profiling when i started: http://msdn.microsoft.com/en-us/magazine/cc188743.aspx

The bit about the native vs byte code is what i was missing when i learned about this so you are saved against this particular headache.

Tzahi Fadida
  • 198
  • 1
  • 7
  • i was trying to avoid having to use a profiler or debugger. Great link though. is the profiler API always there, or only if a profiler is installed. I want something that i can put in deployable code. – klumsy Jun 14 '12 at 16:25
  • It is there since 1.1 which is what i use to compile it since you need it supported on all versions. I currently have a product deployed in this way to major customers. I had the time and resources to do so, it can take some time to setup depending on your particular needs. I had to instrument a microsoft owned dll which is signed and thus cannot be edited. You can only use a profiler to instrument it. – Tzahi Fadida Jun 14 '12 at 16:38
1

Today at least these two frameworks exists that does not use the profiling api (of course they mock around with .NET internals which might be quite fragile)

  • Harmony: MIT licensed. Seems to actually have been used sucessfully in a few game mods, supports both .NET and Mono.
  • Deviare In Process Instrumentation Engine: GPLv3 and Commercial. .NET support currently marked as experimental, but on the other hand has the benefit of being commercially backed.
poizan42
  • 1,461
  • 17
  • 22