3

I have been provided with an unmanaged C++ DLL that contains several classes.

I need to be able to use some of these classes in C#. Based on my research so far, it sounds like I need to create a C++/CLI wrapper DLL that would handle converting between managed and unmanaged types. I have seen some examples where someone would basically create a C++/CLI version of each class and it would contain an instance of the unmanaged C++ type. What is the best way to implement what I am trying to do here? There are maybe 10-15 classes provided in the unmanaged DLL. Right now I only need to use a few of them but may need to use more in the future. Thanks!

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
user1040229
  • 491
  • 6
  • 18
  • if the DllMain is just that, you can as well throw it away since it does nothing. Apart from that, Alex is right. Another option might be swig (http://www.swig.org/), but the time it takes to learn how it works and fix all the caveats will be likely longer than the time to write 10 wrappers, and the resulting code will be ugly and possibly hard to read. – stijn Nov 15 '11 at 16:19
  • possible duplicate of [C++/CLI Mixed Mode DLL Creation](http://stackoverflow.com/questions/2691325/c-cli-mixed-mode-dll-creation) – Hans Passant Nov 15 '11 at 16:26

1 Answers1

5

Yes, you need to create C++/CLI wrapper for this library, and use unmanaged classes internally in this wrapper. For unmanaged library which exposes C-style API, there is also PInvoke option, but it doesn't work for C++ classes.

You can also think about making COM wrapper, it you want to use this library both in native COM clients and .NET. But making C++/CLI wrapper is more simple task, I think.

Alex F
  • 42,307
  • 41
  • 144
  • 212
  • Personally I'd go for the C++/CLI wrapper. Like stijn, I'd also love to know if it's easy to use swig for this purpose. It's something we may wish to do later. In the future, it might also be interesting to see if swig could be used to automatically generate WinRT classes, too. – James Johnston Nov 15 '11 at 16:26