公开 ISO C++类到 C#

发布于 2024-08-28 05:23:52 字数 390 浏览 6 评论 0原文

我需要向 C# 公开一些 C++ 类(我在 Linux 上构建,使用 mono,所以 COM 不是一个选项)

到目前为止我收集的证据表明,解决此问题的最佳方法是:

  1. 编写一个包装器 C++.Net 类围绕 ISO C++ 类
  2. 使用 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:

  1. Write a wrapper C++.Net class around the ISO C++ class
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

送你一个梦 2024-09-04 05:23:52

您无法使用 Mono 在 Linux 上执行 C++/.NET 类。 Mono 不支持托管 C++ 或 C++/CLI,因此无法“围绕 ISO C++ 类编写包装器 C++.Net 类”。

为此,最佳选择是为 C++ 类生成 C API,可以通过 平台调用

话虽如此,缓解此问题的一种选择是使用 SWIG 为您生成包装器。它支持从 C++ 类生成 C# 包装器(以及其他语言的包装器),并且在 Linux/Mono 上运行良好。


编辑:

有关官方的“Mono 不支持混合模式 C++/CLI”,请参阅语言页面:

值得注意的是,任何编译为纯 IL 的语言都应该在 Mono 下工作。某些语言(例如 Microsoft 的托管 C++)并不总是编译为纯 IL,因此它们并不总是按预期工作,因为它们并不真正独立于平台。

用于本机互操作的 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:

It's important to note that any language that compiles to pure IL should work under Mono. Some languages such as Microsoft's Managed C++ do not always compile to pure IL, so they will not always work as expected, since they are not truly platform independent.

C++/CLI for native interop requires non-pure IL, so it will not work on Mono.

记忆消瘦 2024-09-04 05:23:52

Mono 最近在 CXXI(发音很性感)中在 C++ 互操作性方面取得了一些相当大的进步。

从这篇文章来看,新的 CXXI 技术允许 C# /.NET 开发人员可以:

  • 轻松使用 C# 或任何其他 .NET 中的现有 C++ 类
    语言
  • 从 C# 实例化 C++ 对象
  • 从 C# 代码调用 C++ 类中的 C++ 方法
  • 从 C# 代码调用 C++ 内联方法(前提是您的库是使用 -fkeep-inline-functions 编译的或者您提供了代理项)
    库)
  • 从 C# 子类化 C++ 类
  • 用 C# 方法覆盖 C++ 方法
  • 将 C++ 类或混合 C++/C# 类的实例公开给 C# 代码和 C++,就好像它们是本机代码一样。

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:

  • Easily consume existing C++ classes from C# or any other .NET
    language
  • Instantiate C++ objects from C#
  • Invoke C++ methods in C++ classes from C# code
  • Invoke C++ inline methods from C# code (provided your library is compiled with -fkeep-inline-functions or that you provide a surrogate
    library)
  • Subclass C++ classes from C#
  • Override C++ methods with C# methods
  • Expose instances of C++ classes or mixed C++/C# classes to both C# code and C++ as if they were native code.

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.

2024-09-04 05:23:52

你必须像这样围绕你的 ISO C++ 类编写一个 C++ 托管包装器

public ref class MyWrapper
{
    public:
        MyWrapper (void)
        {
            pObj = new CMyUnmanagedClass();
        }
    
        ~MyWrapper (void)
        {
            delete pObj;
        }
    
        int foo(void)
        {
            //pass through to the unmanaged object
            return pObj->foo();
        }
    private:
        CMyUnmanagedClass* pObj;
};

我希望这会有所帮助,
问候

You have to write a C++ managed wrapper around your ISO C++ class like this

public ref class MyWrapper
{
    public:
        MyWrapper (void)
        {
            pObj = new CMyUnmanagedClass();
        }
    
        ~MyWrapper (void)
        {
            delete pObj;
        }
    
        int foo(void)
        {
            //pass through to the unmanaged object
            return pObj->foo();
        }
    private:
        CMyUnmanagedClass* pObj;
};

I hope this helps,
Regards

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文