Exporting whole C++ class to C#

Mar 2, 2012 at 9:07am
I found this article on exporting a whole C++ http://www.codeproject.com/Articles/28969/HowTo-Export-C-classes-from-a-DLL

which boils down to having a GetInstance() function which returns a pointer to the class you want to export, however I can't think how I can achieve the same thing but make it possible for a C# project to use the class, I can't return a whole C++ class to C#, just functions.

I do need my C# app to create several instances of the class I'm exporting however, so I can't think how to approach this.

Thanks.
Mar 2, 2012 at 11:05am
Create a manged c++ wrapper class for your native c++ dll.

This will have to be done in a mix mode c++/cli dll.

You can store a pointer to your native class within this managed wrapper.

Then create all the corresponding methods you need from your native class in your managed class and simply relay those calls from your managed class method to the corresponding native classes method via the pointer to the native class:

 
void SomeClassNatv::SomeMethod(int x, const CString & strVal);


then correspnding manged method:

1
2
3
4
void SomeClassMngd::SomeMethod(int x, String ^ strVal)
{
   pNatvInst->SomeMethod(x, CString(strVal));
}


Then a C# app can use the mix mode dll by just adding it as reference to project and creating an instance of SomeClassMngd ...
Topic archived. No new replies allowed.