Using C ++ API in C #

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

+3
source share
2 answers

The easiest way is to write the 'shim' assembly in C ++ / CLI . This allows you to mix the unmanaged interface and the managed code together so that you can "translate" the unmanaged API into the part that is consumed in the managed code.

There are many resources here, for a detailed overview see C ++ / CLI Justification , and for a discussion of interaction issues, try this post on .NET to C ++ Bridge .

+5
source

There are several options for this.

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

However, there are other options.

  • You can use SWIG to create C # wrappers for the C ++ API. This works pretty well, but it's a more complicated option. (It’s good if you also want to make other language shells for your C ++ API, however, since you can do C # /. NET, python, Java, etc. with one set of APIs.)

  • Platform Invoke (P/Invoke) ++ DLL. , C API ++ DLL, .

+2

Source: https://habr.com/ru/post/1722151/


All Articles