Jump to content



Using C++ unmanaged objects from DLL in C#

  • You cannot reply to this topic
5 replies to this topic

#1 Slig Commando   Members   -  Reputation: 108

Like
0Likes
Like

Posted 02 February 2012 - 08:43 PM

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.

Ad:

#2 Washu   Senior Moderators   -  Reputation: 2447

Like
0Likes
Like

Posted 02 February 2012 - 11:09 PM

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.
ScapeCode - Blog | SlimDX

#3 Martins Mozeiko   Members   -  Reputation: 696

Like
0Likes
Like

Posted 03 February 2012 - 01:54 AM

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).

#4 Slig Commando   Members   -  Reputation: 108

Like
0Likes
Like

Posted 03 February 2012 - 01:52 PM

View PostWashu, on 02 February 2012 - 11:09 PM, said:

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!

#5 Washu   Senior Moderators   -  Reputation: 2447

Like
0Likes
Like

Posted 03 February 2012 - 01:59 PM

View PostLevyDee, on 03 February 2012 - 01:52 PM, said:

View PostWashu, on 02 February 2012 - 11:09 PM, said:

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!
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.
ScapeCode - Blog | SlimDX

#6 Matt-D   Members   -  Reputation: 114

Like
0Likes
Like

Posted 03 February 2012 - 08:21 PM

View PostSlig Commando, on 02 February 2012 - 08:43 PM, said:

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.microso...-unmanaged.aspx
http://blogs.microso...to-managed.aspx
http://blogs.microso...er-options.aspx






We are working on generating results for this topic
PARTNERS