3
**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

5
  • 2
    The snippet is very incomplete. The Mclass must be declared public ref class so it is visible outside of the assembly. And you must implement a finalizer so you don't leak memory when the client code doesn't call Dispose(). And your native() method should check if obj wasn't deleted. Commented Feb 9, 2012 at 13:11
  • @HansPassant:your help is highly appreciated....will try to post complete snippets in future for clarity.... Commented Feb 9, 2012 at 14:25
  • 2
    The point is to do it now, not next time. Commented Feb 9, 2012 at 14:38
  • @HansPassant:sorry for taking too long...edited the code Commented Feb 10, 2012 at 4:06
  • have you check swig before? swig.org Commented Feb 10, 2012 at 4:29

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Classes don't have to be in a namespace though AFAIK
@Simon yup i am talking about C++/CLI.Can you please elaborate a little with an example
@ManuDilipShah - You should be able to simply add the reference to the compiled C++/CLI file to your project. You wanting example makes no sense. If you have a new question, start a new question, post a better sample application when you do.
1

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.

2 Comments

sorry for the confusion....the native code along with the managed class definition are in a project which together creates a dll
@Manu: Ok, that's good. You should nuke all the EXPORT_CLASS and __declspec(dllexport) stuff then, it's just begging for someone to turn it on and cause trouble.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.