可以从 C# 调用 C++ 代码吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/935664/
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 03:41:31  来源:igfitidea点击:

Possible to call C++ code from C#?

c#.netc++unmanagedmanaged

提问by

Is it possible to call C++ code, possibly compiled as a code library file (.dll), from within a .NET language such as C#?

是否可以从 C# 等 .NET 语言中调用 C++ 代码,可能编译为代码库文件 (.dll)?

Specifically, C++ code such as the RakNet networking library.

具体来说,C++ 代码,例如 RakNet 网络库。

采纳答案by Eclipse

One easy way to call into C++ is to create a wrapper assembly in C++/CLI. In C++/CLI you can call into unmanaged code as if you were writing native code, but you can call into C++/CLI code from C# as if it were written in C#. The language was basically designed with interop into existing libraries as its "killer app".

调用 C++ 的一种简单方法是在 C++/CLI 中创建包装程序集。在 C++/CLI 中,您可以像编写本机代码一样调用非托管代码,但您可以从 C# 调用 C++/CLI 代码,就像它是用 C# 编写的一样。该语言基本上被设计为与现有库互操作,作为它的“杀手级应用程序”。

For example - compile this with the /clr switch

例如 - 使用 /clr 开关编译它

#include "NativeType.h"

public ref class ManagedType
{
     NativeType*   NativePtr; 

public:
     ManagedType() : NativePtr(new NativeType()) {}
     ~ManagedType() { delete NativePtr; }

     void ManagedMethod()
      { NativePtr->NativeMethod(); } 
}; 

Then in C#, add a reference to your ManagedType assembly, and use it like so:

然后在 C# 中,添加对 ManagedType 程序集的引用,并像这样使用它:

ManagedType mt = new ManagedType();
mt.ManagedMethod();

Check out this blog postfor a more explained example.

查看此博客文章以获得更详细的示例。

回答by Mat Nadrofsky

Sure is. This articleis a good example of something you can do to get started on this.

当然是。这篇文章是一个很好的例子,你可以做些什么来开始这个。

We do this from C# on our Windows Mobile devices using P/Invoke.

我们在 Windows Mobile 设备上使用P/Invoke从 C# 执行此操作。

回答by Mehrdad Afshari

I'm not familiar with the library you mentioned, but in general there are a couple ways to do so:

我不熟悉你提到的图书馆,但总的来说,有几种方法可以做到:

  • P/Invoketo exported library functions
  • Adding a reference to the COM type library (in case you're dealing with COM objects).
  • P/调用到导出的库函数
  • 添加对 COM 类型库的引用(以防您处理 COM 对象)。

回答by Frank Schwieterman

The technology used to do this is called P/Invoke; you can search for articles on the subject. Note that it is for calling C from C#, not C++ so much. So you'll need to wrap your C++ code in a C wrapper that your DLL exports.

用于执行此操作的技术称为P/Invoke;您可以搜索有关该主题的文章。请注意,它用于从 C# 调用 C,而不是 C++。因此,您需要将 C++ 代码包装在 DLL 导出的 C 包装器中。

回答by Dana Holt

Yes, it is called P/Invoke.

是的,它被称为P/Invoke

Here's a great resource site for using it with the Win32 API:

这是一个很好的资源站点,用于将它与 Win32 API 一起使用:

http://www.pinvoke.net/

http://www.pinvoke.net/

回答by plinth

P/Invoke is a nice technology, and it works fairly well, except for issues in loading the target DLL file. We've found that the best way to do things is to create a static library of native functions and link that into a Managed C++ (or C++/CLI) project that depends upon it.

P/Invoke 是一项不错的技术,它运行得相当好,除了加载目标 DLL 文件时出现问题。我们发现,最好的方法是创建一个本地函数的静态库,并将其链接到依赖它的托管 C++(或 C++/CLI)项目。

回答by Soumendra

Have you considered Apache Thrift?

你考虑过 Apache Thrift 吗?

http://thrift.apache.org/

http://thrift.apache.org/

It seems like a very very neat solution.

这似乎是一个非常非常巧妙的解决方案。