C ++ / CLI or C # P / Invoke for a large C library calling an extern function

Now I know that there are many questions regarding this, but in general they relate to C ++ libraries.

I have a pretty important C dll (about 2800 functions), and I need to make an API for about 70 of them called in C #. I started using P / Invoke, but this is pretty tedious because I have complex structures for passing and returning from C code (structures with pointers to others, double pointers, etc.), and it makes me a lot of time to get what i need.

Now I don’t know C ++ / CLI at all, but I have a little experience with C ++ (not so much).

I wonder if it's worth the effort to find out for my project. Will using a C ++ shell allow me not to struggle with sorting structures, allocating pointers in the global heap, etc. Or not?....

Thank you very much and great ups to this wonderful community.

+4
source share
2 answers

It is hard to deal with native structures from C #. Therefore, writing a C ++ / CLI shell is easier. You can convert / wrap these structures in ref classes without any problems and use them in C #.

 void some_function(Some_Struct * ss) { //... } struct Some_Struct { int intVal; double dblVal; } //C++/ClI public ref class Wrapper { public: void CallSomeFunction(int intval, double doubleval) { Some_Struct * ss = new Some_Struct(); // maybe malloc is more appropriate. ss->intVal = intval; ss->dblVal = doubleval; some_function(ss); delete ss; // or Free(ss); } } //C# Wrapper wrapper = new Wrapper(); wrapper.CallSomeFunction(10,23.3); 
+3
source

This is a fairly large surface area. I think C ++ / CLI will be simpler than P / invoke. You do not need to use any features of C ++, you can write what is essentially C and compile and export it using C ++ / CLI.

+6
source

All Articles