C# 调用原始函数时发生EasyHook访问冲突

C# 调用原始函数时发生EasyHook访问冲突,c#,hook,dll-injection,il2cpp,easyhook,C#,Hook,Dll Injection,Il2cpp,Easyhook,我已经浏览了一堆关于我的问题的SO帖子和其他论坛帖子,但我似乎找不到它 我正在尝试为使用Il2Cpp编译的Unity游戏编写一个DLL,这样我就可以检索有关游戏的运行时信息,并将其提供给discord机器人 我正在使用EasyHook进行DLL注入和函数挂钩 我在网上找到了一个用于EasyHook.LocalHook的包装器,我将在这个问题的末尾添加它 以下是首次注入DLL时运行的代码: [UnmanagedFunctionPointer(CallingConvention.ThisCall,

我已经浏览了一堆关于我的问题的SO帖子和其他论坛帖子,但我似乎找不到它

我正在尝试为使用Il2Cpp编译的Unity游戏编写一个DLL,这样我就可以检索有关游戏的运行时信息,并将其提供给discord机器人

我正在使用EasyHook进行DLL注入和函数挂钩

我在网上找到了一个用于
EasyHook.LocalHook
的包装器,我将在这个问题的末尾添加它

以下是首次注入DLL时运行的代码:

[UnmanagedFunctionPointer(CallingConvention.ThisCall, SetLastError = true)]
public delegate void PlayerControl_AwakeDelegate(IntPtr playerControl);

public GameHook()
{
    CreateFunctionHooks();
}

public void CreateFunctionHooks()
{
    m_awakeHook = new Hook<PlayerControl_AwakeDelegate>(Externs.GetModuleHandle("GameAssembly.dll") + RVA.PlayerControl.Awake, new PlayerControl_AwakeDelegate(PlayerControl_Awake), this);
    m_awakeHook.Activate();
}

public void PlayerControl_Awake(IntPtr playerControl)
{
    Process.Start("cmd.exe");
    m_awakeHook.Original(playerControl);
}
public class Hook<T> : Hook
where T : Delegate
{
    /// <summary>
    /// When called from within the <see cref="Hook.NewFunc"/> delegate this will call the original function at <see cref="Hook.FuncToHook"/>.
    /// </summary>
    public T Original { get; private set; }

    /// <summary>
    /// Creates a new hook at <paramref name="funcToHook"/> redirecting to <paramref name="newFunc"/>. The hook starts inactive so a call to <see cref="Activate"/> is required to enable the hook.
    /// </summary>
    /// <param name="funcToHook">A pointer to the location to insert the hook</param>
    /// <param name="newFunc">The delegate to call from the hooked location</param>
    /// <param name="owner">The object to assign as the "callback" object within the <see cref="EasyHook.LocalHook"/> instance.</param>
    public Hook(IntPtr funcToHook, Delegate newFunc, object owner)
        : base(funcToHook, newFunc, owner)
    {
        // Debug assertion that T is a Delegate type
        System.Diagnostics.Debug.Assert(typeof(Delegate).IsAssignableFrom(typeof(T)));

        Original = Marshal.GetDelegateForFunctionPointer<T>(funcToHook);
    }
}

public class Hook : IDisposable
{
    /// <summary>
    /// The hooked function location
    /// </summary>
    public IntPtr FuncToHook { get; private set; }

    /// <summary>
    /// The replacement delegate
    /// </summary>
    public Delegate NewFunc { get; private set; }

    /// <summary>
    /// The callback object passed to LocalHook constructor
    /// </summary>
    public object Owner { get; private set; }

    /// <summary>
    /// The <see cref="EasyHook.LocalHook"/> instance
    /// </summary>
    public LocalHook LocalHook { get; private set; }

    /// <summary>
    /// Indicates whether the hook is currently active
    /// </summary>
    public bool IsActive { get; private set; }

    /// <summary>
    /// Creates a new hook at <paramref name="funcToHook"/> redirecting to <paramref name="newFunc"/>. The hook starts inactive so a call to <see cref="Activate"/> is required to enable the hook.
    /// </summary>
    /// <param name="funcToHook">A pointer to the location to insert the hook</param>
    /// <param name="newFunc">The delegate to call from the hooked location</param>
    /// <param name="owner">The object to assign as the "callback" object within the <see cref="EasyHook.LocalHook"/> instance.</param>
    public Hook(IntPtr funcToHook, Delegate newFunc, object owner)
    {
        this.FuncToHook = funcToHook;
        this.NewFunc = newFunc;
        this.Owner = owner;

        CreateHook();
    }

    ~Hook()
    {
        Dispose(false);
    }

    protected void CreateHook()
    {
        if (LocalHook != null) return;

        this.LocalHook = LocalHook.Create(FuncToHook, NewFunc, Owner);
    }

    protected void UnHook()
    {
        if (this.IsActive)
            Deactivate();

        if (this.LocalHook != null)
        {
            this.LocalHook.Dispose();
            this.LocalHook = null;
        }
    }

    /// <summary>
    /// Activates the hook
    /// </summary>
    public void Activate()
    {
        if (this.LocalHook == null)
            CreateHook();

        if (this.IsActive) return;

        this.IsActive = true;
        this.LocalHook.ThreadACL.SetExclusiveACL(new Int32[] { 0 });
    }

    /// <summary>
    /// Deactivates the hook
    /// </summary>
    public void Deactivate()
    {
        if (!this.IsActive) return;

        this.IsActive = false;
        this.LocalHook.ThreadACL.SetInclusiveACL(new Int32[] { 0 });
    }


    public void Dispose()
    {
        Dispose(true);
    }

    protected virtual void Dispose(bool disposeManagedObjects)
    {
        // Only clean up managed objects if disposing (i.e. not called from destructor)
        if (disposeManagedObjects)
        {
            UnHook();
        }
    }
}