3

我一直在尝试ICorJitCompiler:compileMethod从 v4.0 中的托管代码中使用EasyHook LocalHook.Create. 我通过解组这样的结构获得了函数指针:

public static class NativeJitInterop
{
    [DllImport("clrjit.dll", CharSet=CharSet.None, SetLastError = true)]
    private static extern IntPtr getJit();

public static ClrJitCompilerHook GetManagedJitCompiler()
{
    ClrJitCompilerHook clrJitCompiler = null;

    IntPtr _clrJitPtr = getJit();

    ICorJitCompiler _corJitCompiler = (ICorJitCompiler)  Marshal.PtrToStructure(_clrJitPtr, typeof(ICorJitCompiler));
    clrJitCompiler = new ClrJitCompilerHook(_clrJitPtr, _corJitCompiler.compileMethod);

    return clrJitCompiler;
}    


[StructLayout(LayoutKind.Sequential)]
internal struct ICorJitCompiler
{
    [MarshalAs(UnmanagedType.FunctionPtr)]
    public CompileMethodSig compileMethod;

}

[UnmanagedFunctionPointer(CallingConvention.StdCall, SetLastError = true)]
internal delegate Int32 CompileMethodSig(IntPtr thisPtr, IntPtr corJitInfo, IntPtr methodInfo, UInt32 flags, [Out] IntPtr ILCode, [Out] UInt64 ILCodeSize);

一切正常,结构似乎没有问题地解组,并且包含的​​委托的 _methodPtr 和 _methodPtrAux 字段填充了一些指针值。

当我尝试像这样设置钩子时出现问题:

public void HookClrJitCompiler()
{
    IntPtr _compileMethodPtr = Marshal.GetFunctionPointerForDelegate(_compileMethodDelegate);

    _localJitHook = LocalHook.Create(_compileMethodPtr, new CompileMethodSig(ClrJitCompilerCalled), this);

    _localJitHook.ThreadACL.SetExclusiveACL(new Int32[] { 0 });
}

我获得了 AccessViolationException

我解决了这个问题并将 _compileMethodPtr变量设置为委托的_methodPtr. 创建钩子时我没有例外,但钩子也不起作用。

我做错了什么?

4

0 回答 0