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

时间:2022-06-28 05:27:02

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. 创建托管(c#)类。

  3. Create a c++/cli (formerly managed c++) class library project. Use this to call the managed c# class (presumably via a reference).
  4. 创建一个c ++ / cli(以前管理的c ++)类库项目。使用它来调用托管c#类(可能通过引用)。

  5. Call the c++/cli code from native c++.
  6. 从本机c ++调用c ++ / cli代码。

Questions:

  1. Is this correct?
  2. 它是否正确?

  3. Is the DLL created at step 2 known as a mixed mode DLL?
  4. 在步骤2中创建的DLL是否称为混合模式DLL?

  5. Has C++/CLI completely superseded Managed C++ as far as MS are concerned?
  6. 就MS而言,C ++ / CLI是否完全取代了托管C ++?

  7. Is COM completely avoided using this approach?
  8. COM完全避免使用这种方法吗?

  9. At what point would the CLR be created and run, and by whom?
  10. 在什么时候创建和运行CLR,由谁?

Thanks in advance

提前致谢

3 个解决方案

#1


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. 是的,它是一个混合模式DLL(事实上,你可以管理你的本机C ++项目的一个文件,并在该文件中创建这个C ++ / CLI类,并直接从该文件调用代码。你甚至不需要单独的DLL来完成这个。

  4. 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 link for details.
  5. C ++ / CLI和托管C ++都代表相同的东西。唯一的区别是在旧版本中直到Visual Studio 2003,它被称为托管C ++。后来,语法发生了很大变化,并重命名为C ++ / CLI。有关详细信息,请查看此链接。

  6. Yes
  7. CLR will be used whenever a call to the managed DLL is made.
  8. 只要调用托管DLL,就会使用CLR。

#2


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.).

注意,您还可以执行IL往返C#dll和导出静态方法,这些方法与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
}

#3


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
}

#1


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. 是的,它是一个混合模式DLL(事实上,你可以管理你的本机C ++项目的一个文件,并在该文件中创建这个C ++ / CLI类,并直接从该文件调用代码。你甚至不需要单独的DLL来完成这个。

  4. 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 link for details.
  5. C ++ / CLI和托管C ++都代表相同的东西。唯一的区别是在旧版本中直到Visual Studio 2003,它被称为托管C ++。后来,语法发生了很大变化,并重命名为C ++ / CLI。有关详细信息,请查看此链接。

  6. Yes
  7. CLR will be used whenever a call to the managed DLL is made.
  8. 只要调用托管DLL,就会使用CLR。

#2


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.).

注意,您还可以执行IL往返C#dll和导出静态方法,这些方法与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
}

#3


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
}