Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto wrap c++ dll into c#

Tags:

c++

c#

dll

wrapper

I want to use c++ library in a c# project. Is there any wrapper tool to import all classes automatically?

like image 338
Baran Avatar asked Feb 21 '11 17:02

Baran


2 Answers

SWIG can help create a wrapper consisting of two parts, one C++ sided, and one C# sided.
It needs a bit of work to set up the correct generation files though.

An alternative that requires more manual coding is C++/CLI.

For pure c apis I prefer p/invoke over either of them. There is a program to automate conversion of c headers. If I recall correctly it's called something like "P/Invoke Interop Assistant" or "Interop Signature Toolkit".

There is also mono/cxxi which looks pretty cool.

like image 56
CodesInChaos Avatar answered Nov 02 '22 23:11

CodesInChaos


The procedure of using native .dll's in .Net is called P/Invoke. Look at http://pinvoke.net/ for some examples.

Note that you must match the build target with the version of the .dll. So for x86 .dll's you need to lock your project to x86, same with x64.

Note2 that you only need to lock the executing project (.EXE), not any additional projects loaded from the .EXE. .Net will automatically match .Net .dll's to CPU target type if they are set to ANY.

From http://social.msdn.microsoft.com/forums/en-US/clr/thread/c957959e-0f0c-422e-a5be-4ccfdd12e63d: You can use "dumpbin /exports <name_of_your_dll>" or dependency walker (depends.exe) to look at the exported symbols. They are both included in Visual Studio.

Additional comment on C++: While it is relatively simple to use native .dll's written in C from .Net, using C++ binaries that make use of objects is not as trivial. One way to solve that is to use a C++ CLI project a binding between managed .Net code and unmanaged C++ library.

like image 40
Tedd Hansen Avatar answered Nov 03 '22 00:11

Tedd Hansen