【发布时间】:2013-03-03 15:06:56
【问题描述】:
我想使用calli 调用 .NET 对象的成员函数。我可以使用以下代码调用一个接受 int 并返回 int 的静态函数:
// push the int argument
// push the address obtained using Ldftn
ilg.EmitCalli (OpCodes.Calli, System.Runtime.InteropServices.CallingConvention.StdCall, typeof<int>, [|typeof<int>|])
// correct result now on stack
我希望我可以使用类似下面的方法来调用一个成员函数,该函数也接受一个 int 并返回一个 int。 Jason Bock 的“CIL Programming”建议它应该可以工作,尽管我可能读错了。 Reflector 还认为有些地方不太正确,并声称该调用被混淆了。它的最佳猜测是“return (int) *ptr1(num1);”,这与静态调用相同:
ilg.Emit (OpCodes.Ldarg_0)
// push the int argument
// push the address
ilg.EmitCalli (OpCodes.Calli, System.Runtime.InteropServices.CallingConvention.StdCall, typeof<int>, [|typeof<int>|])
当我调用第二个片段时,我收到以下消息。不用说,我没有使用编组或类似的东西。
运行时遇到致命错误。错误的地址 位于线程 0x20e50 上的 0xed470d3f。错误代码为 0xc0000005。 此错误可能是 CLR 中的错误或不安全或不可验证的错误 部分用户代码。此错误的常见来源包括用户 COM-interop 或 PInvoke 的封送处理错误,这可能会损坏 堆栈。
请问我做错了什么?
【问题讨论】:
标签: .net reflection cil reflection.emit