简体   繁体   English

从C ++,反向P /调用,混合模式DLL和C ++ / CLI调用C#

[英]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只是一个例子:

  1. Create you managed (c#) class. 创建托管(c#)类。
  2. Create a c++/cli (formerly managed c++) class library project. 创建一个c ++ / cli(以前管理的c ++)类库项目。 Use this to call the managed c# class (presumably via a reference). 使用它来调用托管c#类(可能通过引用)。
  3. Call the c++/cli code from native c++. 从本机c ++调用c ++ / cli代码。

Questions: 问题:

  1. Is this correct? 它是否正确?
  2. Is the DLL created at step 2 known as a mixed mode DLL? 在步骤2中创建的DLL是否称为混合模式DLL?
  3. Has C++/CLI completely superseded Managed C++ as far as MS are concerned? 就MS而言,C ++ / CLI是否完全取代了托管C ++?
  4. Is COM completely avoided using this approach? COM完全避免使用这种方法吗?
  5. At what point would the CLR be created and run, and by whom? 在什么时候创建和运行CLR,由谁?

Thanks in advance 提前致谢

Here are the answers to the best of my knowledge: 以下是我所知的答案:

  1. Yes
  2. Yes, it is a mixed mode DLL (In fact, you can make one file of your native C++ project managed and create this C++/CLI class in that file and call the code directly from that file. You don't even need a separate DLL to accomplish this. 是的,它是一个混合模式DLL(事实上,你可以管理你的本机C ++项目的一个文件,并在该文件中创建这个C ++ / CLI类,并直接从该文件调用代码。你甚至不需要单独的DLL来完成这个。
  3. C++/CLI and Managed C++ both represent same thing. C ++ / CLI和托管C ++都代表相同的东西。 The only difference is that in the older version till Visual Studio 2003, it was termed as Managed C++. 唯一的区别是在旧版本中直到Visual Studio 2003,它被称为托管C ++。 Later on, the syntax was changed quite a lot and it was renamed as C++/CLI. 后来,语法发生了很大变化,并重命名为C ++ / CLI。 Have a look at this link for details. 有关详细信息,请查看此链接
  4. Yes
  5. CLR will be used whenever a call to the managed DLL is made. 只要调用托管DLL,就会使用CLR。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM