tags:

views:

44

answers:

1

I have a few native C++ libraries and such that I need to expose to managed (C#, in this case) code. I need to get as much speed out of it as possible.

I would like to use some classes to simplify the interaction, which means mixed code, but that's not a requirement.

What is a requirement is that it be cross-platform compatible, to Windows and Linux at worst. Thus, standard /clr is out of the question.

My options are either /clr:pure or /clr:safe, or embedding Mono into a go-between layer. My question is which will be better (best performance and ease of development and later use).

The libraries I need to work with make heavy use of pointers and occasionally shared pointers, which made me think the C++/CLI layer would be easier. I wrote a simple app testing use of some objects and compiled it with /clr:pure, it didn't link into the native module but did run under both Windows and Linux.

A: 

Use P/Invoke.

Using 'pure' will make it a lot slower from what I have seen, especially number crunching stuff (encryption).

leppie
Pure generates p/invoke calls automatically, so I don't see why it would be slower. Just recognize that the interface is expensive and don't make it chatty (minimize the number of calls, use primitive types where possible).
Ben Voigt
Pure definitely shouldn't be slower than doing it in C#, so it's not really an issue. My main interest is C++'s management of pointers and any benefits that might provide for an interface layer. Any data that does go between will be pointers in 90% of the functions (and so blittable and little marshaling cost), but I need to work with some unmanaged classes on occasion. That's why /clr:pure was a thought, as most docs suggest it allows limited use of unmanaged libraries and classes.
peachykeen
C++/CLI in /clr:pure mode handles pointers the same way C# in /unsafe mode does. Of course, it's still much easier in C++/CLI because you can just #include the native struct definition -- in C# you have to recreate the structure definition with explicit layout attributes. Ditto for functions, in C++/CLI just #include the header file, in C# you have to write p/invoke declarations and annotate them properly with charset attributes etc. But the resulting MSIL shouldn't be much different.
Ben Voigt