公开 ISO C++类到 C#
我需要向 C# 公开一些 C++ 类(我在 Linux 上构建,使用 mono,所以 COM 不是一个选项)
到目前为止我收集的证据表明,解决此问题的最佳方法是:
- 编写一个包装器 C++.Net 类围绕 ISO C++ 类
- 使用 C# 中的 C++.Net 类
我有以下问题:
- 首先,这是实现将 ISO C++ 类公开给 C# 的目标的“最佳”方法吗?
- 但到目前为止,我还没有看到任何实际展示如何执行此操作的示例 - 谁能建议一些链接或代码片段来展示如何为虚拟类完成此操作?
- 如何将异步消息通知从 C++ 代码发送到 C# 代码?理想情况下,我想让 C# 类引发一个事件,并将接收到的数据作为参数
I need to expose some C++ classes to C# (I am building on Linux, using mono, so COM is not an option)
The evidence I have gathered so far suggests that the best way to approach this is:
- Write a wrapper C++.Net class around the ISO C++ class
- Consume the C++.Net classes from C#
I have the following questions:
- First, is this the "best" way of achieving the goal of exposing ISO C++ classes to C# ?
- So far though, I have not seen any examples that actually show how to do this - can anyone suggest some links, or a code snippet to show how this is done for a dummy class?
- How may I send asynchronous message notifications from the C++ code to the C# code ?. Ideally, I would like to cause the C# class to raise an event, with the received data as an argument
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法使用 Mono 在 Linux 上执行 C++/.NET 类。 Mono 不支持托管 C++ 或 C++/CLI,因此无法“围绕 ISO C++ 类编写包装器 C++.Net 类”。
为此,最佳选择是为 C++ 类生成 C API,可以通过 平台调用。
话虽如此,缓解此问题的一种选择是使用 SWIG 为您生成包装器。它支持从 C++ 类生成 C# 包装器(以及其他语言的包装器),并且在 Linux/Mono 上运行良好。
编辑:
有关官方的“Mono 不支持混合模式 C++/CLI”,请参阅语言页面:
用于本机互操作的 C++/CLI 需要非纯 IL,因此它无法在 Mono 上运行。
You can't do C++/.NET classes on Linux using Mono. Mono doesn't support Managed C++ or C++/CLI, so there is no way to "Write a wrapper C++.Net class around the ISO C++ class".
Your best option for this is to generate a C API for your C++ class, which can be accessed via Platform Invoke.
That being said, one option for easing this is to use SWIG to generate the wrappers for you. It supports generation of C# wrappers from C++ classes (as well as wrappers to other languages), and works well on Linux/Mono.
Edit:
For an official "Mono doesn't support mixed mode C++/CLI", see the Languages page:
C++/CLI for native interop requires non-pure IL, so it will not work on Mono.
Mono 最近在 CXXI(发音很性感)中在 C++ 互操作性方面取得了一些相当大的进步。
从这篇文章来看,新的 CXXI 技术允许 C# /.NET 开发人员可以:
语言
库)
CXXI 是 Google 代码之夏两个夏天的工作成果,旨在提高 Mono 与 C++ 语言的互操作性。
Mono has recently made some pretty big strides with C++ interoperability in CXXI (pronounced sexy).
From this posting, the short story is that the new CXXI technology allows C#/.NET developers to:
language
library)
CXXI is the result of two summers of work from Google's Summer of Code towards improving the interoperability of Mono with the C++ language.
你必须像这样围绕你的 ISO C++ 类编写一个 C++ 托管包装器
我希望这会有所帮助,
问候
You have to write a C++ managed wrapper around your ISO C++ class like this
I hope this helps,
Regards