Using C++ unmanaged objects from DLL in C#

Started by
4 comments, last by Matt-D 12 years, 2 months ago
As the title states. The only solution I have come across is to create a wrapper class in C++ managed of the dll object. But honestly I dont know what that means(I do, but not technically). Could someone explain the conecpt in the thread title to me? Thank you.
Advertisement
If you're passing C++ classes around across DLL boundaries without using interfaces then you're doing it wrong, regardless of the managed/unmanaged setup.

Other than that, if you wish to use a "C++ class" or an unmanaged interface from .net you will have to use C++/CLI to wrap it. Unless it's a COM object, in which case .net can generate a wrapper for it.

If we're talking non-member functions and structs, then you can use pinvoke.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Alternatively to C++/CLI or COM objects you can use CXXI: https://github.com/mono/cxxi (I'm not sure how production-quality ready it is currently).

If you're passing C++ classes around across DLL boundaries without using interfaces then you're doing it wrong, regardless of the managed/unmanaged setup.


Could you please explain why? As long as I am implicitly linking to my dll's, they all share the same heap, so what is the issue?

Also, I was orginally wanting to implement my game engine(via dll's) in a windows form application(map editor), but then I realized there is c++ windows forms. So if that simplifies the answer at all, great!

If you're passing C++ classes around across DLL boundaries without using interfaces then you're doing it wrong, regardless of the managed/unmanaged setup.


There are no interfaces in unmanaged c++, unless by interface you mean an ADT, which is purely situational in unmanaged code.

Could you please explain why? As long as I am implicitly linking to my dll's, they all share the same heap, so what is the issue?

Also, I was originally wanting to implement my game engine(via dll's) in a windows form application(map editor), but then I realized there is c++ windows forms. So if that simplifies the answer at all, great!

[quote name='Washu' timestamp='1328245767' post='4909010']
If you're passing C++ classes around across DLL boundaries without using interfaces then you're doing it wrong, regardless of the managed/unmanaged setup.


There are no interfaces in unmanaged c++, unless by interface you mean an ADT, which is purely situational in unmanaged code.

Could you please explain why? As long as I am implicitly linking to my dll's, they all share the same heap, so what is the issue?

Also, I was originally wanting to implement my game engine(via dll's) in a windows form application(map editor), but then I realized there is c++ windows forms. So if that simplifies the answer at all, great!
[/quote]
By interface I mean a pure abstract class. Which is an interface. I.e. IUnknown.

The reason passing classes around between DLLs and executables without using interface is "bad" is simply this:
Its highly compiler dependent. If your DLLs are always built with the same compiler using the same build settings (release/debug, etc), then it should be all fine and dandy. But that means your DLLs are pretty much useless outside of that scenario. This is because name mangling, object layout, heck even packing differs from compiler to compiler and even from version to version.

The advantage of interfaces is that they are typically all implemented in an identical fashion. While C++ doesn't have an ABI, COM does enforce an ABI on COM interfaces, and almost every C++ compiler on the planet can produce and consume COM interfaces.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.


As the title states. The only solution I have come across is to create a wrapper class in C++ managed of the dll object. But honestly I dont know what that means(I do, but not technically). Could someone explain the conecpt in the thread title to me? Thank you.


I can recommend this three-parter, it's relatively recent so you can also try contacting the author if you have any questions:
http://blogs.microsoft.co.il/blogs/sasha/archive/2012/01/24/managed-unmanaged-interoperability-in-the-2010s-part-1-p-invoke-managed-to-unmanaged.aspx
http://blogs.microsoft.co.il/blogs/sasha/archive/2012/01/28/managed-unmanaged-interoperability-in-the-2010s-part-2-p-invoke-unmanaged-to-managed.aspx
http://blogs.microsoft.co.il/blogs/sasha/archive/2012/01/30/managed-unmanaged-interoperability-in-the-2010s-part-3-c-cli-and-other-options.aspx

This topic is closed to new replies.

Advertisement