无法从EasyHook的托管代码中挂起ICorJitCompiler:compileMethod

时间:2012-10-18 21:00:15

标签: c#-4.0 clr pinvoke jit easyhook

我一直在尝试使用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。在创建钩子时我没有例外,但钩子也不起作用。

我做错了什么?

0 个答案:

没有答案