Exposing an ISO C++ class to C#

感情迁移 提交于 2019-12-09 14:29:14

问题


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

回答1:


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.




回答2:


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.




回答3:


you had to write a c++ managed wraper 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 help regards



来源:https://stackoverflow.com/questions/2595770/exposing-an-iso-c-class-to-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!