Creating a C++ object involves calling operator new to get storage for it
and then invoking a constructor. Conversely, destroying a C++ object
involves calling the destructor and then invoking operator delete to release
its storage. Even if you could somehow hack these steps into working
directly from managed code, you would still have to maually maintain and
pass the object's 'this' pointer to any methods.
Basically, you have 2 options I can see.
(1) Make your class COM. Then you can use existing interop tools to access
your class.
(2) Add global (or even better, static) methods to your class to mirror its
existing methods. The first parameter would be the this pointer. You also
need factory methods. Then p/invoke to these.
class MyClass {
public:
MyClass();
~MyClass();
int Method1(int i);
//...
// Add these
static MyClass *MyClass_Ctor() { return new MyClass(); }
static MyClass *MyClass_Dtor(MyClass *_this) { delete (MyClass
*)_this; }
static int MyClass_Method1(MyClass *_this,int i) { return ((MyClass
*)_this)->Method1(i); }
};
Then you delcare these for p/invoke as
IntPtr MyClass_Ctor();
void MyClass_Dtor(IntPtr _this);
int MyClass_Method1(IntPtr _this,int i);
Dunno, maybe making them COM is easier. Or could you make then plain old
non-OO C or C++ to begin with?
"Chris Kiechel" <ch***********@applicationcraftsmen.com> wrote in message
news:05****************************@phx.gbl...
I am writing a .NET Windows application and it needs to
perform DDE calls to a legacy system. I created a C++
unmanaged class that performs the actual DDE connection
and communication. However, I need .NET to instantiate
this object and somehow allow the this object to callback
to .NET. So, my question is:
1. How can a managed class instantiate and unmanaged
class?
2. How can an unmanaged class call a function or delegate
in the managed class?
I would like both classes to reside in the same project,
but it doesn't have to.
Thanks!