I'm using a small C webserver. I'm doing some of the actual request processing with C# libraries.
I'm using a glue layer which appears to be written in something close to c++ to join the two worlds. It provides a 'public ref class' which I can derive from in the C# world.
If I want to derive several different classes from this in C#, how do I create instances of these derived classes from the C/C++?
What information (the class name? A factory function?) shoud I pass (and and in what form) from the C# to the C/C++ code?
I would like a solution compatible with .NET 2.0, and I'm using Visual Studio 2008 to create my code.
Passing custom objects between C# and unmanaged C++
Can't get SAFEARRAY to work with Interop
What are the differences between C, C# and C++ in terms of real-world application
- You cannot simply call managed code from a native C++ application.
What's the “Hello World!” of genetic algorithms good for?
In order to call it, you will first have to expose your .Net code to COM, and then instantiate a COM object in C++.
OOP Terminology: “Container” & “Collection”
This way your native app thinks it is creating a COM object like any other, and .Net is doing all the interop in runtime.
Checking if Registry Value exists Visual C++ 2005
Problem in calling a C++ dll function from C#
- Second way is to use managed C++/CLI (which is not native C++, but supports both worlds).
C++ blowfish works with fewer letters,but C# blowfish doesn't
This way both C++ and C# apps are managed and can communicate seamlessly.
So as long as the "glue layer" is written using managed C++/CLI, you can both work with native and managed data..
If you don't mind using managed C++/CLI, then you have a simpler solution - you can instantiate managed classes easily (with some changes in syntax you should quickly get used to)..
If you only want the C++ code to be aware of the base class, you'll need to call some kind of factory method..
#using "ThingLib.dll" using namespace ThingLib;
If you want to actually instantiate the derived classes in C++, use the
ThingBase^ thing = myThingFactory.MakeThing(aParameter);
There's a good summary here of the new C++ language features for talking to managed code: http://msdn.microsoft.com/en-us/library/xey702bw.aspx.
ThingBase^ thing = gcnew DerivedThing(aParameter);