嵌入标准 C++到 .NET 应用程序中
我想用标准的、独立于平台的 C++ 编写一个算法库。
然后我可以采用相同的 C++ 代码并将其编译为 .NET,以便 C# 应用程序可以使用该库吗? 如果没有,实现这种互操作的最简单方法是什么?
I want to write an algorithmic library in standard, platform-independent C++.
Can I then take the same C++ code and compile it for .NET so that a C# application can use the library?
If not, what is the easiest way to achieve this kind of interop?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法直接在 C# 和标准 C++ 之间进行交互。但是,您可以在 C++/CLI 包装器中使用 C++ 库,这使得该功能在 .NET 托管代码(以及 C#)中可用。
只需将包装器编写为 C++/CLI 库,该库公开 .NET API 并包含标准 C++ 库中的代码(但不幸的是,据我所知,这不能静态链接)。
You cannot directly interface between C# and standard C++. However, you can use your C++ library inside a C++/CLI wrapper which makes the functionality available in .NET managed code (and thus C#).
Simply write the wrapper as a C++/CLI library that exposes a .NET API and includes the code from your standard C++ library (but unfortunately this cannot be statically linked, as far as I know).
您可以通过三种方式公开您的库,以便从托管代码中轻松调用:
public ref class
的 C++/CLI 包装器 - C# 代码将调用此类的方法,这将调用库的本机方法第三个是 COM,我添加它只是为了完整性。这不会是我的第一选择。或者我的第三个。 COM 组件是否存在是一回事,为此编写它又是另一回事。
在前两者之间,问题在于还有谁可以使用这个库。如果 C# 代码是唯一使用它的代码,请执行您喜欢的操作。但例如,您可能有 15 或 20 个设置各种属性的本机方法,后面跟着执行实际工作的
go
或execute
方法。由于托管本机互操作有成本,因此我会告诉您编写一个带有 20 个参数的 C++/CLI 函数,调用各种本机函数,然后调用go
或execute
。这为您提供了一个更简洁的界面,而不是冗长的界面,并降低了互操作成本。You have three ways to expose your library to easily be called from managed code:
public ref class
- the C# code will call methods of this class, which will call native methods of your libraryThat third one, COM, I include only for completeness. It wouldn't be my first choice. Or my third. It's one thing if the COM component exists, it's another to write it just for this.
Between the first two it's a matter of who else might use this library. If the C# code is the only one using it, do what you like. But for example you might have 15 or 20 native methods that set various properties, followed by a
go
orexecute
method that does the actual work. Because managed-native interop has a cost, I would tell you to write one C++/CLI function that takes 20 parameters, calls the various native functions and then calls thego
orexecute
. This gives you a chunkier interface instead of chatty and reduces your interop costs.您可以使用 pinvoke 从任何 .NET 语言调用本机 C/C++ 库。以下是来自 MSDN 的快速教程。您还可以在 C++ 中创建 COM dll,您可以使用 COM Interop 从 C# 中直接引用它。除非您需要 COM 的优势,否则 pinvoke 是一条更容易的路线。
当然,正如 Konrad 所说,您可以使用托管 C++ 解决方案包装 C++ dll。
作为示例,请查看 http://www.pinvoke.net/ 以获取有关如何调用 win32 api 的示例来自 C#。
来自上面的 MSDN 链接(根据下面的评论进行修改):
You can call native C/C++ libraries from any .NET language using pinvoke. Here's a quick tutorial from MSDN. You can also create a COM dll in C++ that you can reference directly from within C# using COM Interop. Unless you need the advantages of COM, pinvoke is a much easier route to go.
And of course, as Konrad stated, you wrap the C++ dll using a managed C++ solution.
As an example, check out http://www.pinvoke.net/ for samples on how you call win32 api's from C#.
From the MSDN link above (modified per the comment below):