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!
Willy Denoyette [MVP] - 28 Mar 2006 10:24 GMT
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,
|
[quoted text clipped - 14 lines]
| ps: I'll publish optimal solution in codeproject, there is so many people
| having this question!
Jochen Kalmbach [MVP] - 28 Mar 2006 10:45 GMT
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/577d07f35
22b5a25?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
Remi THOMAS [MVP] - 28 Mar 2006 13:33 GMT
> Hi,
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