473,326 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Managed Class Instantiating Unmanaged Class

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!
Nov 16 '05 #1
5 3600
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!

Nov 16 '05 #2
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.

-----Original Message-----
Creating a C++ object involves calling operator new to get storage for itand then invoking a constructor. Conversely, destroying a C++ objectinvolves calling the destructor and then invoking operator delete to releaseits storage. Even if you could somehow hack these steps into workingdirectly from managed code, you would still have to maually maintain andpass 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 accessyour class.

(2) Add global (or even better, static) methods to your class to mirror itsexisting methods. The first parameter would be the this pointer. You alsoneed 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 oldnon-OO C or C++ to begin with?

"Chris Kiechel" <ch***********@applicationcraftsmen.com> wrote in messagenews: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!

.

Nov 16 '05 #3
Unmanaged == "regular C++"

"Chris Kiechel" <ch***********@applicationcraftsmen.com> wrote in message
news:08****************************@phx.gbl...
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.

-----Original Message-----
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!

.

Nov 16 '05 #4
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());
}

"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!

Nov 16 '05 #5
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());
}

"Chris Kiechel" <ch***********@applicationcraftsmen.com> wrote in messagenews: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!

.

Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a...
3
by: zhphust | last post by:
I want to convert a object of a managed class to a unmanaged structure that has the same member with that managed class. Can anybody tell me how i can do it? Thanks in advance. -- zhphust...
2
by: Martin Zenkel | last post by:
Dear VS Team, using the Beta 2 of VS 2005 I've encontered the following problem. Let's assume threre are three Dll's, one unmanaged and two managed. In the unmanaged we put a simple unmanged...
3
by: WithPit | last post by:
I am trying to create an managed wrapper but have some problems with it by using abstract classes. In my unmanaged library code i had the following three classes with the following hierarchy ...
3
by: Thorsten | last post by:
HI I'm a C# developer and unfortunately I have to write now some code in managed and unmanaged C++. In this area I'm Newbie and therefore please forgive me if this is a really simple...
13
by: bonk | last post by:
Hello, I am trying to create a dll that internally uses managed types but exposes a plain unmanaged interface. All the managed stuff shall be "wrapped out of sight". So that I would be able to...
6
by: Vijay | last post by:
Hi, I am faced with the following common managed/unmanaged C++ interop problem: - I have a .NET form that acts as a front end GUI to a processing engine in the background. The processing...
9
by: Amit Dedhia | last post by:
Hi All I have a VC++ 2005 MFC application with all classes defined as unmanaged classes. I want to write my application data in xml format. Since ADO.NET has buit in functions available for...
12
by: DaTurk | last post by:
Hi, I have a rather interesting problem. I have a unmanged c++ class which needs to communicate information to managed c++ via callbacks, with a layer of c# on top of the managed c++ ultimatley...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.