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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1068762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 07:29:12  来源:igfitidea点击:

Calling C# from C++, Reverse P/Invoke, Mixed Mode DLLs and C++/CLI

c#c++clrc++-cli

提问by ng5000

As I understand it I can use reverse P/Invoke to call C# from C++. Reverse P/Invoke is simply a case of:

据我了解,我可以使用反向 P/Invoke 从 C++ 调用 C#。反向 P/Invoke 只是一种情况:

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

Questions:

问题:

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

Thanks in advance

提前致谢

采纳答案by Aamir

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.
  3. C++/CLI and Managed C++ both represent same thing. The only difference is that in the older version till Visual Studio 2003, it was termed as Managed C++. Later on, the syntax was changed quite a lot and it was renamed as C++/CLI. Have a look at this linkfor details.
  4. Yes
  5. CLR will be used whenever a call to the managed DLL is made.
  1. 是的
  2. 是的,它是一个混合模式 DLL(实际上,您可以管理本机 C++ 项目的一个文件,并在该文件中创建此 C++/CLI 类并直接从该文件调用代码。您甚至不需要单独的DLL 来实现这一点。
  3. C++/CLI 和托管 C++ 都代表相同的东西。唯一的区别是在 Visual Studio 2003 之前的旧版本中,它被称为托管 C++。后来,语法发生了很大变化,并更名为 C++/CLI。请查看此链接以了解详细信息。
  4. 是的
  5. 每当调用托管 DLL 时都将使用 CLR。

回答by Lucero

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. However, this is always a post-compile step, and it does have some caveats(which your C++/CLI export have too, btw.).

请注意,您还可以执行 C# dll 的 IL 往返并导出静态方法,其工作方式与 C++/CLI 中的导出基本相同。但是,这始终是编译后步骤,并且确实有一些注意事项(顺便说一句,您的 C++/CLI 导出也有这些注意事项)。

You can ILDASM both the C# and the C++/CLI DLLs to see how exports are don; it is something like this (from a sample on the net):

您可以 ILDASM C# 和 C++/CLI DLL 来查看导出是如何进行的;它是这样的(来自网络上的样本):

// 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
}

回答by Larrys S. Smith

With ilasm 2.0 you need less code because it can generate most of the gunk by itself. Only the .export directive is really needed now.

使用 ilasm 2.0,您需要的代码更少,因为它可以自行生成大部分垃圾。现在只需要 .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
}