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
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Stick it to THE MAN
  • 5,621
  • 17
  • 77
  • 93
  • I agree with Reed Copsey. Basically write a C wrapper around the C++ library and then use C# to interface with the C wrapper using P/Invoke. I find the following reference extremely useful when inter-operating with native libraries : http://www.mono-project.com/Interop_with_Native_Libraries – Frank Hale Apr 07 '10 at 21:03

3 Answers3

8

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.

roalz
  • 2,699
  • 3
  • 25
  • 42
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • @Reed: are you sure about not being able to use C++/.Net? Could you send me the link please - I need to show it to someone who insists that it is possible – Stick it to THE MAN Apr 07 '10 at 21:14
  • @Stick it to THE MAN: Here's a SO answer from a Mono dev: http://stackoverflow.com/questions/183377/does-mono-net-support-and-compile-c-cli I'll see if I can find a more "offical" source, but it isn't supported. – Reed Copsey Apr 07 '10 at 21:16
  • @Stick it to THE MAN: I edited my answer to show you more details - Mono's language page discusses this, right at the top. – Reed Copsey Apr 07 '10 at 21:18
  • Thank you, thank you, thank you!. You probaly saved me a whole lot of grief going down that path. ok, I'm off to swot over the SWIG and P/Invoke documentation (but not before I have accepted your answer!) – Stick it to THE MAN Apr 07 '10 at 21:37
  • 1
    This is [now possible](http://stackoverflow.com/a/9247254/332026) on Mono. Mixed-mode C++/CLI is still not supported but they have their own way of integrating with native C++. – Justin Feb 12 '12 at 07:48
  • 2
    I agree with @Justin that Mono's CXXI is even more suitable for inter-operation between the two worlds, as it does not require a strange C++ alike language extension. – Lex Li Feb 12 '12 at 09:21
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.

Justin
  • 8,853
  • 4
  • 42
  • 42
0

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

Ajit Vaze
  • 2,686
  • 2
  • 20
  • 24