tags:

views:

75

answers:

2

I would like to use an API that is written in c++. It involves including header files and libraries. How can I use this API in a C# application?

+4  A: 

The easiest way is to write a 'shim' assembly in C++/CLI. This lets you mix the unmanaged interface and managed code together so you can 'translate' the unmanaged API into some that is consumable in managed code.

There are lots of resources out there, for an in depth overview see C++/CLI Rationale and for a discussion of the interop issues try this post on a .NET to C++ Bridge.

Rob Walker
+1 this is exactly what i've done in the past. it made my life that much easier, especially since managed C++ is not that difficult.
Stan R.
+1  A: 

There are a few options to do this.

The easiest, as Rob Walker mentioned, is to use C++/CLI to make a managed wrapper.

However, there are other options.

  1. You can use SWIG to generate C# wrappers for the C++ API. This works reasonably well, but is a more difficult option. (It's nice if you want to also make other language wrappers for your C++ API, though, since you can do C#/.NET, python, Java, etc, with one set of APIs.)

  2. You can use Platform Invoke (P/Invoke) to access your C++ written DLL. This is much easier if you have a C API for your C++ DLL, but is possible otherwise.

Reed Copsey