1

I am looking for a way to inject(add/replace) code of methods in 3-d party managed .NET assemblies during run-time. I am doing this using C# in VS2017.

I tried:

  • PlayHooky(https://github.com/wledfor2/PlayHooky), works great, but it does not support original method calls and for project I am working on I want something well tested and robust.
  • Easyhook(https://easyhook.github.io), examples on the web show how to inject managed code into unmanaged APIs, couldn't find information if is is even designed to inject managed into managed.
  • Deviare v.2.0(http://whiteboard.nektra.com/deviare-v-2-0), just didn't work, even following their Quickstart Guide, no info on target managed assemblies support.

Here is EasyHook code that didn't work for me:

TestClass r = new TestClass();
MethodInfo originalMethodInfo = typeof(TestClass).GetMethod(nameof(TestClass.DoSomething));
IntPtr originalMethodInfoPtr = originalMethodInfo.MethodHandle.GetFunctionPointer();

MethodInfo replacementMethodInfo = typeof(Program).GetMethod(nameof(Program.DoSomething));
IntPtr replacementMethodInfoPtr = replacementMethodInfo.MethodHandle.GetFunctionPointer();
//Create hook
using (LocalHook hook = EasyHook.LocalHook.CreateUnmanaged(originalMethodInfoPtr, replacementMethodInfoPtr, IntPtr.Zero))
{
    hook.ThreadACL.SetInclusiveACL(new int[1]);//Enable hook
    r.DoSomething();//Call original method, expecting replacement method to be called
    Console.ReadKey();
}

When I tried using EasyHook.LocalHook.Create I was getting "Managed Debugging Assistant 'Reentrancy' : 'Attempting to call into managed code without transitioning out first..." exception.

I am looking for any ideas how to make EasyHook work or some alternative ways to achieve managed hooking on the fly.

  • 2
    I have used https://github.com/pardeike/Harmony/wiki before and found to be a powerful and easy to use tool for managed-to-managed injections. – rs232 Jul 05 '19 at 13:19

0 Answers0