Managed Class Instantiating Unmanaged Class

  • Thread starter Thread starter Chris Kiechel
  • Start date Start date
C

Chris Kiechel

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!
 
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?
 
Hmmm...you're challenging my assumptions. Is "unmanaged
code" and regular C++ the same thing, or are they two
different beasts?

They must be different, which would explain why the
unmanaged code examples that I've reviewed looked so
foreign.

So, I need to "get smart" on the differences between
unmanaged code and C++!

Thanks, Ted! That was helpful.
 
Unmanaged == "regular C++"

Chris Kiechel said:
Hmmm...you're challenging my assumptions. Is "unmanaged
code" and regular C++ the same thing, or are they two
different beasts?

They must be different, which would explain why the
unmanaged code examples that I've reviewed looked so
foreign.

So, I need to "get smart" on the differences between
unmanaged code and C++!

Thanks, Ted! That was helpful.
 
1. ---------------------------------

struct Unmanaged
{
void func(){}
};
__gc class Managed /*: public IDispose*/
{
Unmanaged* m_pUnMan;
public:
Managed() { m_pUnMan = new Unmanaged(); }
~Managed() { delete m_pUnMan, m_pUnMan=0; }
void func()
{
Managed __pin* pThis = this;
m_pUnMan->func(pThis);
}
};

2. ---------------------------------

//.h
struct Unmanaged
{
void func(Managed* pMan);
};

__gc class Managed /*: public IDispose*/
{
Unmanaged* m_p;
public:
Managed() { m_p = new Unmanaged(); }
~Managed() { delete m_p, m_p=0; }
void func()
{
Managed __pin* pThis = this;
m_p->func(pThis);
}
};
//.cpp
void Unmanaged::func(Managed* p)
{
Console::Writeline(p->ToString());
}
 
Bingo! Thanks!
-----Original Message-----
1. ---------------------------------

struct Unmanaged
{
void func(){}
};
__gc class Managed /*: public IDispose*/
{
Unmanaged* m_pUnMan;
public:
Managed() { m_pUnMan = new Unmanaged(); }
~Managed() { delete m_pUnMan, m_pUnMan=0; }
void func()
{
Managed __pin* pThis = this;
m_pUnMan->func(pThis);
}
};

2. ---------------------------------

//.h
struct Unmanaged
{
void func(Managed* pMan);
};

__gc class Managed /*: public IDispose*/
{
Unmanaged* m_p;
public:
Managed() { m_p = new Unmanaged(); }
~Managed() { delete m_p, m_p=0; }
void func()
{
Managed __pin* pThis = this;
m_p->func(pThis);
}
};
//.cpp
void Unmanaged::func(Managed* p)
{
Console::Writeline(p->ToString());
}




.
 
Back
Top