Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a C++ API from C#

I have a pretty big system implemented in C++ I need to interact with. The system has a pretty big API, a number of C++ DLLs. These DLLs export C++ classes, as opposed to a nice C style API. and I need to use them from a new C# project.

From what I know .NET has three ways of interacting with native software:

  1. P/Invoke - which works only on C APIs
  2. COM objects
  3. C++/CLI

So the way I understand it, I have three approaches accordingly:

  1. Writing a wrapper in C and calling it with P/Invoke. which seems way too much work.
  2. Writing a wrapper with COM. which I don't know how to do, and unless it's insanely easy i'm reluctant to learn what seems to me - a dying technology .
  3. Writing a wrapper in C++/CLI. which seems the least work, though still a lot.

My question:

  1. First of all I would like to know why doesn't .NET allow me simply to use the C++ classes "as is"? I'm assuming it's a matter of memory management. and if it is I'm more than willing to write finalizers, and implementing IDisposable . From what I know C++ classes are just really fancy structs, and since P/Invoke supports structs, and functions that take structs as the first parameter, why not support classes?

  2. Second, assuming I'm really lazy, and its a lot of boring, tedious, work, What would be the best way to use these DLL's? a possibility to call them directly from C# would be the best. If not then I'd love an automatic tool to produce the wrappers. Also, the DLLs might change, probably, just slightly but still, id rather not be forced to manually re-write wrappers.

For a really good answer, especially on the first part, or a good automatic tool, I'll reward a bounty...

Thank you

like image 550
AK_ Avatar asked Apr 04 '13 22:04

AK_


People also ask

Can you use C in C#?

You can directly call C functions from C# by using P/Invoke. Here's a short how-to on creating a C# lbrary that wraps around a C dll. Now in the Linker branch, go to General, and change Output File to: "$(SolutionDir)Wrapper\$(ProjectName). dll", this will copy the built C DLL to the C# project root.

Can C call C++ library?

Oracle Developer Studio C and C++ use the same C runtime libraries, as noted in the section about compatible compilers. Using Oracle Developer Studio compilers, you can therefore use Standard I/O functions freely in both C and C++ code in the same program.

How do I combine C and C++?

You will need to wrap the C header file inclusion with the extern "C" { } as shown above. Then, you can just try to compile it (only the source file containing the main function) in a C++ compiler, and the rest of the C code with the C compiler, and then link the whole thing together.


1 Answers

If you are willing to go the lazy way I would suggest using a tool to generate C# wrapper for your C++ classes. And of course the tool for generating such a wrapper is SWIG

For more information, see my old answer to similar question

like image 64
user544511 Avatar answered Oct 12 '22 22:10

user544511