Miguel
Miguel

Reputation: 3506

Unmanaged C++ class in C#

Can I instantiate and "work with" an unmanaged C++ class from C# code (say, using DllImport and such)?

Upvotes: 3

Views: 538

Answers (2)

exe
exe

Reputation: 31

You can also use SWIG. It works with Mono on Linux and Mac.

It generates a set of C functions which expose the C++ class, and generates a C# class that calls them with DllImport.

It allows inheriting C++ classes in C#, and with Directors feature C++ code can call back virtual methods overridden in C#.

Upvotes: 0

The Fiddler
The Fiddler

Reputation: 2794

On Windows, you can use COM interop to work with C++. This won't work on Linux/Mac OS X, however.

Another alternative is to expose a C wrapper around the C++ class (extern "C" in C++). This is necessary because DllImport expects a C ABI.

Edit: and a third alternative is C++/CLI, i.e. compile your C++ code for .Net. This is also a Windows-only solution.

Upvotes: 6

Related Questions