在 C# 中调用 C++ 库

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

Call C++ library in C#

c#c++function

提问by Yigang Wu

I have a lot of libraries written in C++. I want to call these libraries from C#, however, I have met many problems. I want to know if there is a book or guideline to tell me how to do that.

我有很多用 C++ 编写的库。我想从 C# 调用这些库,但是,我遇到了很多问题。我想知道是否有一本书或指南告诉我如何做到这一点。

回答by Nick Gunn

I recently had to wrap some c++ code in .NET. Although the c++ code was packaged as a dll, the interface was too unfriendly for P/Invoke, so I decided to write it in managed c++, or C++/CLI as it is apparently known now.

我最近不得不在 .NET 中包装一些 C++ 代码。虽然 c++ 代码被打包成 dll,但接口对 P/Invoke 来说太不友好了,所以我决定用托管 c++ 或 C++/CLI 编写它,因为它现在显然是众所周知的。

I found this tutorialvery useful on the syntax. It's not so easy on the eye, but the content seemed pretty good.

我发现本教程对语法非常有用。看起来不是那么容易,但内容似乎还不错。

回答by 17 of 26

If you google "c++ c# interop", you'll find tons of information on this topic.

如果您在 google 上搜索“c++ c# interop”,您会找到大量关于此主题的信息。

A couple of links:

几个链接:

http://msdn.microsoft.com/en-us/magazine/cc301501.aspx
http://msdn.microsoft.com/en-us/library/ms235281(VS.80).aspx

http://msdn.microsoft.com/en-us/magazine/cc301501.aspx
http://msdn.microsoft.com/en-us/library/ms235281(VS.80).aspx

回答by Andy Dent

I'm a big fan of the book C++/CLI in Actionwhich has a couple of useful sample chapters online, at that address.

我是《C++/CLI in Action》这本书的忠实粉丝,该书在该地址有几个有用的在线示例章节。

This intro on CodeProjectis a good starting point.

CodeProject上的这个介绍是一个很好的起点。

The author of C++/CLI in Action has a number of articles on CodeProject, scroll down to the C++/CLI section on his index.

C++/CLI in Action 的作者在 CodeProject 上有很多文章,向下滚动到他的索引上的 C++/CLI 部分。

The Wikipedia article on P/Invokehas a number of reasons why you might not want to use that approach, with which I agree:

上的P / Invoke维基百科的文章有一些原因,你可能不希望使用这种方法,与我同意:

  • loss of typing support by the compiler
  • possible data type or alignment issues as you have to map types by hand
  • need to pin garbage-collected objects
  • 编译器失去打字支持
  • 可能的数据类型或对齐问题,因为您必须手动映射类型
  • 需要固定垃圾收集的对象

The best starting point on MSDN is the summary article.

MSDN 上最好的起点是摘要文章