Jump to content
  • Advertisement
Sign in to follow this  
LevyDee

Using C++ unmanaged objects from DLL in C#

This topic is 2502 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Hidden

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!

Share this post


Link to post

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!

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!