Jump to content
  • Advertisement
Sign in to follow this  
Roof Top Pew Wee

Using C# dlls in C++ code. How do objects work?

This topic is 5425 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

Say I have the following in C#:
public class SpriteManager
{
     List spriteList = new List();// just a sprite list

     public Sprite AddSprite()
     {
        Sprite tempSprite = new Sprite();
        spriteList.Add(tempSprite);
        return tempSprite;
     }
}

Now, I have a friend who wants to use my .dlls in his C++ program. However, I'm curious how this would work. In C#, the preceding code would return a reference to the sprite created and stored in the list. So, the call would be: Sprite mySprite = spriteManager.AddSprite(); How would this work in C++? Would you use a Sprite pointer? I thought that pointers were discouraged in .NET. And simply creating a Sprite and assigning the return from AddSprite() would do a value copy in C++, right? I'm a little lost on this. Anyone know how this is done, or have any references on the subject? Thanks, --Vic--

Share this post


Link to post
Share on other sites
Advertisement
Is your friend using managed or unmanaged C++? If he's using managed then I'd imagine it's quite simple to do and there'll be plenty of example around (haven't used managed C++ myself). However, if he's using unmanaged C++ then I think you'll have to use COM to use managed code from unmanaged code (ignoring mixed mode assemblies since your using C#). Try looking up info on "COM interoperability".

Share this post


Link to post
Share on other sites
The easiest way to accomplish this is for your friend to be using Managed C++, but he's probably not currently. He would compile his C++ code to the same IL that your C# code compiles to, and then he would be able to call your libraries without any problems.

Managed C++ really feels like a bit of a hack in VS 2003 and it's complicated enough that I'll leave the Google search as an exercise for the reader. C++/CLI is a much more thoroughly thought out language extension and will be available in VS 2005.

But, assuming you want this to be simple and don't really want to learn a new language :), this article should help (also if you wanted to call his C++ code from C#)
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/manunmancode.asp

Good luck,
-Joe

Share this post


Link to post
Share on other sites
Well, I've not touched C++ since the days of Visual Studio 6. He just got C++ .NET yesterday, so he's new to it as well. We're trying to figure it out together.

Since it's .NET, I'm assuming it is managed C++. So with that in mind, any other direction?

--Vic--

Share this post


Link to post
Share on other sites
Because Managed C++ compiles to IL, you use it very differently from VC++6; for example, instead of new() you use gcnew() which garbage collects for you the same way C# does. There are semantic differences between Handles (.Net references) and C-style pointers, the language supports them both. Google search or the MSDN documentation that came with your friend's copy are probably your best starting points for this.

If you're feeling lucky,
http://www.ondotnet.com/pub/a/dotnet/2003/01/13/intromcpp.html

From the situation you describe though, you may be best off both working in the same language...

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!