0
我有一個公共方法在一個程序集中的密封類,我想添加一個日誌記錄系統,但unfortunatley我沒有源。所以我試圖在特定的日誌記錄方法上繞行此方法,並在退出時調用原始方法。掛鉤工作正常,但我無法獲得任何類型的參數,或者至少我得到了完全錯誤的東西。在C#運行時掛鉤託管的方法#
我不能使用任何類型的注入或類似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會給你所有關於方法的信息,包括參數類型 – Gusman
您可能會發現這篇文章有幫助的一個MethodInfo的:http://www.codeproject.com/Articles/37549/CLR-Injection- Runtime-Method-Replacer – Amy
謝謝@Gusman,我已經看到它了。但它是用另一個替換原來的方法,這是不正確的,我需要 –