manu_dilip_shah
manu_dilip_shah

Reputation: 900

Calling c++ dll from c#

**unmanaged class**

this is the unmanaged class declaration

#ifdef EXPORT_CLASS
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT __declspec(dllimport)
#endif
public class DLL_EXPORT cppclass
{
private:
string x;
public:
cppclass();
~cppclass();
string native();
};


**UNMANAGED CLASS DEFINITION**

this is the unmanaged class definition

 cppclass::cppclass()
{
x="hello";
};
cppclass::~cppclass()
{
};
string cppclass::native()
{
return x;
};

**MANAGED CLASS**

this is the managed class declaration

public __gc class Mclass
{
//private:
public:
cppclass * obj;
public:
Mclass();
~Mclass();
string native();

};

**MANAGED CLASS DEFINITION**

//this is the managed class definition

#include"managed.h"
Mclass::Mclass()
{
    obj=new cppclass();
};
Mclass::~Mclass()
{
    delete obj;
};
string Mclass::native() 
{
return  obj->native();
};

All these four files are in a project which is made into a dll.Now how to use it in a c# project

Upvotes: 3

Views: 1122

Answers (2)

Ben Voigt
Ben Voigt

Reputation: 283634

This whole thing is a disaster. That isn't C++/CLI code, it's the old Managed Extensions for C++ which are broken. Also, exporting native classes from DLLs is a really bad idea. Statically link your native code with the managed class definition, creating a single DLL.

Upvotes: 1

Simon
Simon

Reputation: 1504

Add a reference to the DLL in your C# project and simply use it like any .NET type. Probably you don't find any exported types in the DLL, because these have to be public.

Edit: BTW. i hope you're talking about C++/CLI, not Managed C++ which was prior to C++/CLI.

Upvotes: 5

Related Questions