C# 运行时热更新替换函数

必须是JIT过的函数,也就是进行过编译;(为什么ios不能用,编译器不同啦)

RuntimeMethodHandle.GetFunctionPointer 方法

NativeAPI.VirtualProtect

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Runtime.CompilerServices;

namespace MethodHookSample
{
    class Program
    {
        static void Main(string[] args)
        {
            Target target = new Target();
            var methodAdd = typeof(Target).GetMethod("Add", BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
            var methodMinus = typeof(Target).GetMethod("Minus", BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
            MethodHooker.HookMethod(methodAdd, methodMinus);
            var result = target.Add(5, 4);
            Console.Write("5 + 4 = " + result);
            Console.Read();
        }
    }

    public class Target
    {
        [MethodImpl(MethodImplOptions.NoInlining)]
        public int Add(int a, int b)
        {
            return a + b;
        }

        [MethodImpl(MethodImplOptions.NoInlining)]
        public int Minus(int a, int b)
        {
            return a - b;
        }
    }

    public class MethodHooker
    {
        public static void HookMethod(MethodInfo sourceMethod, MethodInfo destinationMethod)
        {
            RuntimeHelpers.PrepareMethod(sourceMethod.MethodHandle);
            RuntimeHelpers.PrepareMethod(destinationMethod.MethodHandle);

            if (sourceMethod.IsVirtual)
            {
                HookVirtualMethod(sourceMethod, destinationMethod);
                return;
            }

            unsafe
            {
                if (IntPtr.Size == 4)
                {
                    int* inj = (int*)destinationMethod.MethodHandle.Value.ToPointer() + 2;
                    int* tar = (int*)sourceMethod.MethodHandle.Value.ToPointer() + 2;
#if DEBUG
                    Console.WriteLine("\nVersion x86 Debug\n");

                    byte* injInst = (byte*)*inj;
                    byte* tarInst = (byte*)*tar;

                    int* injSrc = (int*)(injInst + 1);
                    int* tarSrc = (int*)(tarInst + 1);

                    *tarSrc = (((int)injInst + 5) + *injSrc) - ((int)tarInst + 5);
#else
                    Console.WriteLine("\nVersion x86 Release\n");
                    *tar = *inj;
#endif
                }
                else
                {
                    long* inj = (long*)destinationMethod.MethodHandle.Value.ToPointer() + 1;
                    long* tar = (long*)sourceMethod.MethodHandle.Value.ToPointer() + 1;
                    Console.WriteLine("\nVersion x64 Release\n");
                    *tar = *inj;
                }
            }
        }

        static void HookVirtualMethod(MethodInfo sourceMethod, MethodInfo destinationMethod)
        {
            unsafe
            {
                UInt64* methodDesc = (UInt64*)(sourceMethod.MethodHandle.Value.ToPointer());
                int index = (int)(((*methodDesc) >> 32) & 0xFF);
                if (IntPtr.Size == 4)
                {
                    uint* classStart = (uint*)sourceMethod.DeclaringType.TypeHandle.Value.ToPointer();
                    classStart += 10;
                    classStart = (uint*)*classStart;
                    uint* tar = classStart + index;
                    uint* inj = (uint*)destinationMethod.MethodHandle.Value.ToPointer() + 2;
                    *tar = *inj;
                }
                else
                {
                    ulong* classStart = (ulong*)sourceMethod.DeclaringType.TypeHandle.Value.ToPointer();
                    classStart += 8;
                    classStart = (ulong*)*classStart;
                    ulong* tar = classStart + index;
                    ulong* inj = (ulong*)destinationMethod.MethodHandle.Value.ToPointer() + 1;
                    *tar = *inj;
                }
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将C++中的指针函数传递给C#中的委托,你需要进行一些额外的步骤。下面是一个示例代码,演示了如何在C++多线程中将指针函数传递给C#委托: 首先,定义一个C++的指针函数,用于在多线程中执行任务: ```cpp // C++头文件 typedef void(__stdcall *CallbackFunction)(int); extern "C" __declspec(dllexport) void __stdcall DoWork(CallbackFunction callback); ``` ```cpp // C++源文件 void LongRunningTask(CallbackFunction callback) { for (int i = 0; i <= 100; i++) { // 模拟长时间运行的任务 // ... // 调用委托函数传递进度 if (callback != nullptr) { callback(i); } } } extern "C" __declspec(dllexport) void __stdcall DoWork(CallbackFunction callback) { // 在新线程中执行任务 std::thread t(LongRunningTask, callback); t.detach(); } ``` 接下来,在C#中定义一个对应的委托类型,并导入C++ DLL中的函数: ```csharp using System; using System.Runtime.InteropServices; namespace YourNamespace { public delegate void CallbackFunction(int progress); public static class YourClass { [DllImport("YourCppDll.dll")] public static extern void DoWork(CallbackFunction callback); public static void ProgressCallback(int progress) { // 在这里处理传递过来的进度 // ... } } } ``` 在C#中,你可以使用`CallbackFunction`委托类型来定义一个对应的回调函数。在`YourClass`类中,我们导入了C++ DLL中的`DoWork`函数,并定义了一个`ProgressCallback`方法来处理传递过来的进度。 最后,你可以调用`DoWork`函数来在C++多线程中执行任务,并将C#中的回调函数传递给它: ```csharp using YourNamespace; public class YourClass { public static void Main() { CallbackFunction callback = new CallbackFunction(YourClass.ProgressCallback); YourClass.DoWork(callback); } public static void ProgressCallback(int progress) { // 在这里处理传递过来的进度 // ... } } ``` 在这个示例代码中,我们创建了一个委托实例`callback`,并将`YourClass.ProgressCallback`作为回调函数传递给了`DoWork`函数。 请确保替换示例代码中的"C:\YourCppDll.dll"为你实际的C++ DLL文件路径,并将命名空间、类名和函数名等替换为你自己的实际情况。 希望这个示例能对你有所帮助!如有任何问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值