Jump to content
  • Advertisement
Sign in to follow this  
Headkaze

[.net] Using multiple objects of C++ dll via p/invoke

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

I have a C++ dll I have written which has a class inside the dll. I want to be able to use multiple instances of the object from my C# application. Right now I am using an "id" value to access different instances of the class. Eg. In the C++ I have for example:
CClass m_classObject[16];

CLASS_API void Function(int id);
{
   m_classObject[id].Function();
}
And I p/invoke Function() from a C# wrapper class and manage the "id" using a static variable inside it that will increment each time a new instance of the wrapper class is instanciated. Is there an easier way to do this so I don't need to manage the id of class objects in C#? How can I can dynamically allocate a new object as needed and keep all the management inside the dll keeping in mind that each instanciation of the C# wrapper class needs a new instanciation of the object inside the dll. I just can't seem to think of anything other than an id value.

Share this post


Link to post
Share on other sites
Advertisement
Thanks Mike sounds exactly like what I need. I've been doing a bit of reading about it and it seems quite easy to write a wrapper for it.

The only thing is the dll's I write are written in VC6, I know it sounds crazy but the reason is when I tried writing a C++ dll in VC 2005 people reported that my program stopped working. I found out later it's because they didn't have Visual C++ runtimes. FFS! I write code in C++ so I don't have to have dependancies. It's already painful enough that people have to have .NET 2.0 installed to run my application. Now they have to have VC++ runtimes as well?

So my question is now can you write a C++/CLI wrapper in VC6? I'm guessing you can't so I will have to upgrade my dll projects to use 2005, and also include the VC++ runtimes. If this is what I have to do, is it okay for me to include the VC++ runtime dll's in the same folder as my dll's? I would rather do that than require users to download the redistributable. Also what are the VC++ runtime dll's called and how big are they? I am running VS 2005 SP1.

Share this post


Link to post
Share on other sites
Quote:
I write code in C++ so I don't have to have dependancies[sic].


This is a bad idea to latch on to. Everything requires dependencies, including C and C++, which are in the form of runtimes. There is an option to statically link to them instead of dynamically so that you don't have to include the DLLs (which is probably the default option for VC6, but not VS2005), but this won't work if you are using C++/CLI (you are required to dynamically link).

You should not distribute the DLLs directly. Instead, include the runtime installer inside of your own installer, so that the user never even knows that it's separate from your program. There is an option to run it in silent mode.

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!