I have a native C++ application and I have a C# DLL. My native C++ application needs to access the functionality of the C# DLL. In order to do this, I have created a mixed-mode C++ DLL. My design is basically the same as this. In other words:
I have a C# DLL used by a Mixed C++ DLL, which is used by a native C++ application. Here's a simplified version of what I'm doing:
C# DLL
using System;
namespace CSDLL
{
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public Person() : this("Bob", 30)
{
}
public Person(String name, Int32 age)
{
Name = name;
Age = age;
}
public override string ToString()
{
return String.Format("<Person Name=\"{0}\" Age=\"{1}\"/>", Name, Age);
}
}
}
C++ DLL
Person.h: A header file suitable for native applications.
#include <string>
namespace CPPDLL
{
class __declspec(dllexport) Person
{
public:
Person () ;
Person (const std::string &name, int age);
void PrintPerson () const ;
};
}
Person.cpp: Mixed code implementation. Notice the static variable person
.
#include "Person.h"
namespace CPPDLL
{
ref class Wrap
{
public:
static CSDLL::Person ^person = gcnew CSDLL::Person () ;
};
Person::Person ()
{
}
Person::Person (const std::string &name, int age)
{
System::String ^csName = gcnew System::String (name.data ()) ;
Wrap::person = gcnew CSDLL::Person (csName, age) ;
}
void Person::PrintPerson () const
{
System::Console::WriteLine (Wrap::person) ;
}
}
Sample Native C++ Application
#include "Person.h"
int main ()
{
CPPDLL::Person person ("Billy", 5) ;
person.PrintPerson () ;
return 0 ;
}
Problem
- I have to expose a native API header from the C++ DLL to the C++ application.
- A native class cannot have a managed object as a member variable.
- Currently, if the native application created multiple instances of
CPPDLL::Person
, then they will all be working on the same instance ofCSDLL::Person^
. Because a native class cannot have a managed data member, I'm not sure of any obvious way of getting around this.
Question
Is there an easy/obvious way to make it so that every instance of CPPDLL::Person
works with its own copy of CSDLL::Person^
?