[C++/CLI] Callback from unmanaged to managed

R

Remi THOMAS [MVP]

Hi,

I did spend quite a long time finding how to call managed code from
unmanaged C++ class.
Here is the my final code:
http://xtware.com/mcallback/mcallback.htm

Is it the best way to do it?
Does
mgt^ callback = (mgt^)GCHandle::FromIntPtr(IntPtr(m_callback)).Target;
take long time to execute?

Can we do it a better way?

Remi

ps: I'll publish optimal solution in codeproject, there is so many people
having this question!
 
W

Willy Denoyette [MVP]

I don't get the point, nothing in this sample is unmanaged code, everything
gets compiled to IL.
What you are doing can just be done like this:

class umgt
{
public:

void MyTreatment(mgt ^obj)
{
MyParameter ^param = gcnew MyParameter;
param->m_value = 123;
obj->MyCallback(param);
}
};

So I don't think this illustrates how to callback from unmanaged to managed.

Willy.


| Hi,
|
| I did spend quite a long time finding how to call managed code from
| unmanaged C++ class.
| Here is the my final code:
| http://xtware.com/mcallback/mcallback.htm
|
| Is it the best way to do it?
| Does
| mgt^ callback = (mgt^)GCHandle::FromIntPtr(IntPtr(m_callback)).Target;
| take long time to execute?
|
| Can we do it a better way?
|
| Remi
|
| ps: I'll publish optimal solution in codeproject, there is so many people
| having this question!
|
 
J

Jochen Kalmbach [MVP]

Hi Remi!
I did spend quite a long time finding how to call managed code from
unmanaged C++ class.
Here is the my final code:
http://xtware.com/mcallback/mcallback.htm

Is it the best way to do it?

The easiest Solution is to use the "gcroot" template...
This is exactly designed for your purpose...
See:
http://groups.google.de/group/microsoft.public.dotnet.languages.vc/msg/577d07f3522b5a25?hl=de&

By the way:
Unmanaged in this context means: The class in not handled by the GC.
Unmanaged does not mean "native"...

Greetings
Jochen
 
R

Remi THOMAS [MVP]


Thanks for your answer.

I was sure gcroot disapear with C++/CLI. It's badly classify in MSDN I
think.

#pragma unmanaged
MSND says "An unmanaged function will be compiled for the native
platform...", confusing isn't it?

Remi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top