46

I have an unmanaged C++ DLL which merely exports a single class (not COM...it's just a simple C++ class) as its interface. I want to use this class in C# but am told that it cannot merely be imported into C#.

What is the right way to use this class in my C# application?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
cjserio
  • 2,857
  • 7
  • 29
  • 29
  • 1
    Sorry i should have been more specific. This is NOT a COM class. It's just a plain old C++ class. – cjserio Feb 20 '09 at 13:47

5 Answers5

44

Simple way assuming class Foo:

  1. Create a C++/CLI project, call this FooWrapper.
  2. Make FooWrapper depend on the unmanaged dll (however you normally would).
  3. Create a managed class ManagedFoo which contains a single private instance field of type Foo*.
  4. provide public wrapping functions in ManagedFoo which forward on to the underlying instance field.
  5. Optionally (though recommended):
    • convert parameters from .net idioms (strings and the like) to C++ idioms (std::string or char*)
    • catch unmanaged exceptions and throw managed ones instead

Then you make your c# code depend on the FooWrapper project/dll and ensure that the unmanaged dll is properly deployed with it, how that is done depends on the unmanaged dll but in the same directory is normally sufficient.

If the functions do not rely on instances of the class then even simpler is P/Invoke

Draken
  • 3,134
  • 13
  • 34
  • 54
ShuggyCoUk
  • 36,004
  • 6
  • 77
  • 101
  • I wouldn't call (5) optional if the OP is dealing with strings or exceptions, though. – Timo Geusch Feb 20 '09 at 13:55
  • What would i do if i wanted to get rid of the C++ Dll layer and just create a C++/CLI project that is both the DLL and the wrapper in one? More specifically, my current C++ DLL just links a bunch of static libs together into a nice interface. Can i do all of this work in a C++/CLI project? – cjserio Feb 20 '09 at 14:05
  • Yes you can do all in this C++/CLI – ShuggyCoUk Feb 20 '09 at 14:17
  • I put optional because the api might not have any strings. converting SEH to .Net exception objects is also not mandatory (though recommended). have altered text to make this clear – ShuggyCoUk Feb 20 '09 at 14:18
  • specifically C++/CLI projects can contain both managed and unmanaged classes. – ShuggyCoUk Feb 20 '09 at 14:29
8

This answer might be overkill for a single class library, but SWIG is a good solution for "wrapping" C/C++ classes for use from other languages. It works well with C#.

See http://www.swig.org/.

Stewart
  • 4,223
  • 6
  • 31
  • 39
3

DllImport is your best bet. There is a bit of data type massaging, especially if you are passing structs, but you can do almost anything with it.

Steve
  • 11,763
  • 15
  • 70
  • 103
2

You need an proxy (GoF pattern) intermediary to bridge the managed/unmanaged boundary.

Two options:

  • A C++/CLI wrapper
  • A COM wrapper.

The former will be more direct, and latter has two steps pure C++ -> COM -> .NET.

Richard
  • 106,783
  • 21
  • 203
  • 265
  • could you please provide some links/info for the second option.thanks – rsjethani May 07 '12 at 14:34
  • @rsjethani: I don't have any to hand: creating a COM wrapper means writing (native) COM. Then the using `implib` to create the wrapper for .NET. – Richard May 08 '12 at 07:03
2

Sometimes, it is easier to provide your own C interface. SWIG is non-trivial to setup. I have use managed C++ and C++/CLI and they are fine. The easiest was just doing a C wrapper (and can be used by about any other language since most have a way to call a C function).

chrish
  • 2,352
  • 1
  • 17
  • 32