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

Started by
2 comments, last by Mike.Popoloski 15 years, 8 months ago
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.
Advertisement
It sounds like you need a C++/CLI wrapper around your C++ DLL to help your two layers interop properly.
Mike Popoloski | Journal | SlimDX
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.
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.
Mike Popoloski | Journal | SlimDX

This topic is closed to new replies.

Advertisement