C++ and C# interoperability : P/Invoke vs C++/CLI
Asked Answered
D

4

13

In the course of finding a way to interoperate between C# and C++ I found this article that explains about P/Invoke.

And I read a lot of articles claiming that C++/CLI is not exact C++ and requires some effort to modify from original C++ code.

I want to ask what would be the optimal way when I have some C++ objects (code/data) that I want to use from C# objects.

  • It looks like that in order to use P/Invoke, I should provide C style API. Is it true? I mean, is there a way to export C++ object to C# like SWIG with P/Invoke? Or, do I have to use SWIG for this purpose?
  • How hard is it to change C++ to C++/CLI? Is it worth trying compared to rewrite the C++ to C#? The C++ is well designed, so implementing it to C# is not great deal.
  • (Off the topic question) Is there the other way round? I mean, if I want to use C# code from C++, is there any way to do so?
Donate answered 30/6, 2010 at 14:58 Comment(0)
M
19

I would not recommend rewritng your C++ library into C++/CLI. Instead, I would write a C++/CLI wrapper that you can call from C#. This would consist of some public ref class classes, each of which probably just manages an instance of the native class. Your C++/CLI wrapper just "include the header, link to the lib" to use the native library. Because you have written public ref class classes, your C# code just adds a .NET reference. And all you do inside each public ref class is use C++ Interop (aka It Just Works interop) to call the native code. You can apply a facade while you're at it if you like.

Messenger answered 30/6, 2010 at 15:2 Comment(1)
+1 absolutely. It typically isn't necessary to modify the existing C++ code at all.Pulsatile
C
5

The C++/CLI syntax is awkward, but it does allow you to expose managed objects from C++ code. This can be convenient if you have a large C++ API and can abstract some of that functionality to a simpler interface for consumption by your managed code. I've done this successfully for integration with tools like the Matrox vision library, where I could write my vision analysis code in C++ and then use the C++/CLI extensions to expose high-level managed classes. The most painful part is probably string and array inter-op, but strings have always been painful, even in plain C++.

C++/CLI can definitely consume any .NET managed objects, but you have to take special care if you're using any pointers to managed memory (you'll need to use pinning in this case.) If I need to pass pointers to an API, I usually prefer to keep that memory unmanaged and then create managed wrapper classes for manipulating the unmanaged memory.

Chausses answered 30/6, 2010 at 15:5 Comment(0)
P
3

Further to Kate Gregory's answer, to call C# code from C++, using C++/CLI is the obvious way to do it, as it makes it extremely simple and seamless.

Pulsatile answered 30/6, 2010 at 15:5 Comment(0)
R
0

Wrap your C++ native classes, as pointer to implementation, into C++/CLI classes, then call & use these CLR-based classes by 'using'ing them in your .net application. This is my recommended way. DO NOT REWRITE your existing classes in C++/CLI.

Rene answered 10/7, 2017 at 5:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.