简体   繁体   English

在C ++中使用Very C#DLL

[英]Using a Very C# DLL in C++

I am trying to use a C# DLL which has multiple references to .NET classes and C# Classes in Embarcadero C++ Builder. 我正在尝试使用C#DLL,该DLL对Embarcadero C ++ Builder中的.NET类和C#类具有多个引用。 Things like the Point class and String class as well as Delegates. 诸如Point类和String类以及Delegates之类的东西。

I am wondering if the >NET references or C# ones will mess me up somehow. 我想知道> NET引用还是C#引用会以某种方式弄乱我。 I am just about to getting it hooked up, but I am wondering if some of the problems I am having could be caused by C++ not wanting to play nice. 我只是想把它连接起来,但是我想知道我遇到的一些问题是否可能是C ++不想玩的好。

I gave a similar answer to your problem in this question . 这个问题上,我对您的问题也给出了类似的答案

You basically want a C++/CLI interface to your C# code. 基本上,您希望将C ++ / CLI接口连接到C#代码。

If you want to pass a C# delegate to C++ code, you can translate it using Marshal::GetFunctionPointerForDelegate() ( MSDN ). 如果要将C#委托传递给C ++代码,则可以使用Marshal::GetFunctionPointerForDelegate()MSDN )对其进行翻译。 That gives you a IntPtr that you can call ToPointer() on to pass in as a function pointer. 这为您提供了一个IntPtr ,您可以调用ToPointer()作为函数指针进行传递。

You will need to use C++/CLI to use any .NET content in C++. 您将需要使用C ++ / CLI来使用C ++中的任何.NET内容。 You might also be able to set up your own AppDomain, but those are the only two choices, as C++ has no native ability to interact with .NET, at all. 您也许也可以设置自己的AppDomain,但这是仅有的两个选择,因为C ++根本没有与.NET进行交互的本机能力。

Or you can use the mono CLR embedder 或者您可以使用单声道CLR嵌入器

Managed code can invoke unmanaged code in two ways, [using P/Invoke or] using the low-level Mono embedding API . 托管代码可以通过两种方式调用非托管代码,[使用P / Invoke或] 使用低级Mono嵌入API

This works a lot like the oldfashioned embedding of a Perl, Python or Ruby 'interpreter' (actually, virtual machines) in your C/C++ executable. 这就像在C / C ++可执行文件中老式嵌入Perl,Python或Ruby'解释器'(实际上是虚拟机)一样。 I don't think there is actually such a thing as a Swig(++) wrapper generator for this (yet), but here is a snippet of what a call into CIL code looks like: 我认为实际上并没有Swig(++)包装器生成器之类的东西,但是下面是CIL代码调用的摘要:

 class MyClass {
    static void Foo (int value) {
      ...
    }

    int Bar (string name) {
      ...
    }
  }

assuming you got the corresponding MonoMethod* in foo_method and bar_method and this_arg is a MonoObject* of type MyClass, you simply execute: 假设您在foo_method和bar_method中获得了相应的MonoMethod *,并且this_arg是MyClass类型的MonoObject *,则只需执行以下命令:

  /* we execute methods that take one argument */
  void *args [1];
  int val = 10;
  /* Note we put the address of the value type in the args array */
  args [0] = &val;

  /* execute Foo (10);
   * it's a static method, so use NULL as the second argument.
   */
  mono_runtime_invoke (foo_method, NULL, args, NULL);

  /* a string is a reference, so we put it directly in the args array */
  args [0] = mono_string_new (domain, "Hello");
  /* execute my_class_instance.Bar ("Hello");
   * See the Creating Objects section to learn how to get this_arg.
   */
  MonoObject *result = mono_runtime_invoke (bar_method, this_arg, args, NULL);
  /* we always get a MonoObject* from mono_runtime_invoke (), so to get
   * the integer value we need to unbox (which returns a pointer to
   * the value stored in the object) and dereference.
   */
  int int_result = *(int*)mono_object_unbox (result);

For extra entertainment value : if you AOT-compile all of your CIL code, you'll be able to statically link your assembly into your native binary (effectively doing what Managed C++ (c++-cli) calls mixed mode assemblies). 为了获得更多娱乐价值 :如果您对所有CIL代码进行AOT编译,则可以将程序集静态链接本机二进制文件(有效地执行Managed C ++(c ++-cli)称为混合模式程序集的操作)。 Look at 看着

 mono --aot=static myassembly.dll

and

 mkbundle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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