在C#中在运行时挂钩托管方法

5
我有一个封装类,里面有一个公共方法,在程序集中。我想添加一个日志系统,但不幸的是,我没有源代码。于是我试图在特定的日志方法上重载这个方法,并在退出时调用原始方法。钩子函数已经正确地运作,但我无法获得任何类型的参数,或者至少我拿到了完全错误的东西。
我不能使用任何类型的注入或像PostSharp这样的库,所以我想知道这种事情是否可以在运行时以某种方式实现,还是我应该放弃?
为了给你更多细节,我将在下面粘贴一些代码片段:
public Hook(Delegate target, Delegate hook)
{
  this.target = Marshal.GetFunctionPointerForDelegate(target);
  targetDelegate = target;
  this.hook = Marshal.GetFunctionPointerForDelegate(hook);

  originalBytes = new byte[6];
  Marshal.Copy(this.target, originalBytes, 0, 6);

  byte[] hookPointerBytes = BitConverter.GetBytes(this.hook.ToInt32());
  // Jump
  newBytes = new byte[]
  {
    0x68, hookPointerBytes[0], hookPointerBytes[1], hookPointerBytes[2], hookPointerBytes[3], 0xC3
  };
}

public object CallOriginal(params object[] args)
{
  // Remove the patch
  Uninstall();
  // Invoke the original method
  object ret = targetDelegate.DynamicInvoke(args);
  // Re-apply the patch
  Install();
  return ret;
}


public sealed class Foo
{
    public void DoSomething(Int32 value1)
    {
      // and here I am getting value1 = -1919988997
      Console.WriteLine(value1);
    }
}

class Program
{
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    public delegate void DoSomethingDelegate(Int32 value1);

    private static DoSomethingDelegate Original { get; set; }
    private static DoSomethingDelegate Hooked { get; set; }

    private static HookManager _hookManager;

    public static void DoSomething(Int32 value1)
    {
      // This is called as well after foo.DoSomething but value1 is 251934152
      Console.WriteLine("Hooked DoSomething: " + value1) ;
      var hook = _hook["DoSomethingHook"];

      // Call the original Foo.DoSomething
      hook.CallOriginal(value1);
    }

    static void Main(string[] args)
    {
      RuntimeHelpers.PrepareMethod(typeof(Foo).GetMethod("DoSomething").MethodHandle);
      _hookManager = new HookManager();
      var originalPointer = typeof(Foo).GetMethod("DoSomething").MethodHandle.GetFunctionPointer();
      Original = (DoSomethingDelegate)Marshal.GetDelegateForFunctionPointer(originalPointer, typeof(DoSomethingDelegate));
      Hooked = DoSomething;
      _hookManager.Add(Original, Hooked, "DoSomethingHook");

      // Call Hook method, HookManager it is just an extended dictionary...
      _hookManager.InstallAll();

      var foo = new Foo();

      // Calling the original method here with 1
      foo.DoSomething(1);

      Console.ReadLine();
    }
}

GetMethod会返回一个MethodInfo对象,其中包含有关该方法的所有信息,包括参数类型。 - Gusman
你可能会发现这篇文章有帮助:http://www.codeproject.com/Articles/37549/CLR-Injection-Runtime-Method-Replacer - user47589
谢谢@Gusman,我已经看过了。但是它是用另一个方法替换原始方法,这不是我需要的正确方法。 - Luca Bottani
2
已解决,Marshal.GetFunctionPointerForDelegate:您不能使用此方法从另一个托管委托的函数指针创建委托。 - Luca Bottani
你把完整的代码发布在哪里了吗?我对此非常感兴趣。 - Evengard
1
@Evengard 当然可以...请看这里:https://gitlab.altralogica.it/caos/insider - Luca Bottani
1个回答

3
解决了,Marshal.GetFunctionPointerForDelegate: 我不能使用此方法从一个函数指针创建另一个托管委托。
  this.target = target.Method.MethodHandle.GetFunctionPointer(); //Marshal.GetFunctionPointerForDelegate(target);
  targetDelegate = target;
  this.hook = hook.Method.MethodHandle.GetFunctionPointer(); //Marshal.GetFunctionPointerForDelegate(hook);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接