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

时间:2022-09-01 16:44:27

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

是否可能在.NET语言(如c#)中调用c++代码(可能编译为代码库文件。dll) ?

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

特别是c++代码,如RakNet网络库。

7 个解决方案

#1


79  

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代码。这种语言基本上是用interop作为其“杀手级应用”来设计的。

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 post for a more explained example.

查看这篇博客文章,获得一个更详细的示例。

#2


7  

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

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

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

#3


7  

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)项目中。

#4


3  

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/

#5


1  

Sure is. This article is 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.

我们使用P/Invoke在我们的Windows Mobile设备上执行c#。

#6


1  

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包装器中。

#7


-1  

Have you considered Apache Thrift?

你考虑过Apache Thrift吗?

http://thrift.apache.org/

http://thrift.apache.org/

It seems like a very very neat solution.

这似乎是一个非常简洁的解决方案。

#1


79  

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代码。这种语言基本上是用interop作为其“杀手级应用”来设计的。

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 post for a more explained example.

查看这篇博客文章,获得一个更详细的示例。

#2


7  

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

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

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

#3


7  

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)项目中。

#4


3  

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/

#5


1  

Sure is. This article is 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.

我们使用P/Invoke在我们的Windows Mobile设备上执行c#。

#6


1  

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包装器中。

#7


-1  

Have you considered Apache Thrift?

你考虑过Apache Thrift吗?

http://thrift.apache.org/

http://thrift.apache.org/

It seems like a very very neat solution.

这似乎是一个非常简洁的解决方案。