Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 4.0 Can';t hook ICorJitCompiler:从托管代码中使用EasyHook进行编译的方法_C# 4.0_Clr_Pinvoke_Jit_Easyhook - Fatal编程技术网

C# 4.0 Can';t hook ICorJitCompiler:从托管代码中使用EasyHook进行编译的方法

C# 4.0 Can';t hook ICorJitCompiler:从托管代码中使用EasyHook进行编译的方法,c#-4.0,clr,pinvoke,jit,easyhook,C# 4.0,Clr,Pinvoke,Jit,Easyhook,我一直在尝试使用EasyHook LocalHook.Create从v4.0中的托管代码中钩住ICorJitCompiler:compileMethod。我从如下结构的解组中获得了函数指针: public static class NativeJitInterop { [DllImport("clrjit.dll", CharSet=CharSet.None, SetLastError = true)] private static extern IntPtr getJit();

我一直在尝试使用EasyHook LocalHook.Create从v4.0中的托管代码中钩住
ICorJitCompiler:compileMethod
。我从如下结构的解组中获得了函数指针:

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);
public void HookClrJitCompiler()
{
    IntPtr _compileMethodPtr = Marshal.GetFunctionPointerForDelegate(_compileMethodDelegate);

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

    _localJitHook.ThreadACL.SetExclusiveACL(new Int32[] { 0 });
}
一切正常,结构似乎没有问题,并且包含的委托的_methodPtr和_methodPtrAux字段填充了一些指针值

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

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);
public void HookClrJitCompiler()
{
    IntPtr _compileMethodPtr = Marshal.GetFunctionPointerForDelegate(_compileMethodDelegate);

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

    _localJitHook.ThreadACL.SetExclusiveACL(new Int32[] { 0 });
}
我获取并访问ViolationException

我解决了这个问题,并将
\u compileMethodPtr
变量设置为委托的
\u methodPtr
。我在上钩时没有例外,但钩子也不起作用

我做错了什么