[英]Calling C# from C++, Reverse P/Invoke, Mixed Mode DLLs and C++/CLI
As I understand it I can use reverse P/Invoke to call C# from C++. 据我所知,我可以使用反向P / Invoke从C ++调用C#。 Reverse P/Invoke is simply a case of:
反向P / Invoke只是一个例子:
Questions: 问题:
Thanks in advance 提前致谢
Here are the answers to the best of my knowledge: 以下是我所知的答案:
Note, you can also do a IL roundtrip of the C# dll and export static methods, which work basically the same as the exports in C++/CLI. 注意,您还可以执行IL往返C#dll和导出静态方法,这些方法与C ++ / CLI中的导出基本相同。 However, this is always a post-compile step, and it does have some caveats (which your C++/CLI export have too, btw.).
但是,这总是一个后编译步骤,它确实有一些警告 (你的C ++ / CLI导出也是如此,顺便说一句。)。
You can ILDASM both the C# and the C++/CLI DLLs to see how exports are don; 您可以ILDASM C#和C ++ / CLI DLL来查看导出的方式; it is something like this (from a sample on the net ):
它是这样的(来自网上的样本 ):
// unmexports.il
// Compile with : ilasm unmexports.il /dll
assembly extern mscorlib {}
..assembly UnmExports {}
..module UnmExports.dll
// This flag is important
..corflags 0x00000002
// This instructs the CLR to create a marshaling thunk for the unmanaged caller
..vtfixup [1] int32 fromunmanaged at VT_01
..data VT_01 = int32(0)
..method public static void foo()
{
..vtentry 1:1
..export [1] as foo
ldstr "Hello from managed world"
call void [mscorlib]System.Console::WriteLine(string)
ret
}
With ilasm 2.0 you need less code because it can generate most of the gunk by itself. 使用ilasm 2.0,您需要更少的代码,因为它可以自己生成大部分的垃圾。 Only the .export directive is really needed now.
现在真的只需要.export指令。
// unmexports.il
// Compile with : ilasm unmexports.il /dll
.assembly extern mscorlib { auto }
.assembly UnmExports {}
.module UnmExports.dll
.method public static void foo()
{
.export [1] as foo
ldstr "Hello from managed world"
call void [mscorlib]System.Console::WriteLine(string)
ret
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.