Raise events from unmanaged to managed
-
Hi, I tried to raise an event from unmanaged code class to a class in managed code. I passed a delegate pointer (using Marshal) to the unmanaged class but I got an error when the unmanaged class tried to execute the function. I tried to use pin_ptr to the delegate pointer with no results. How can I raise event from unmanaged code class to a managed code class using delegate (managed) and function pointer(unmanaged)? Thanks
-
Hi, I tried to raise an event from unmanaged code class to a class in managed code. I passed a delegate pointer (using Marshal) to the unmanaged class but I got an error when the unmanaged class tried to execute the function. I tried to use pin_ptr to the delegate pointer with no results. How can I raise event from unmanaged code class to a managed code class using delegate (managed) and function pointer(unmanaged)? Thanks
I don't think that it is possible to raise event directly from unmanaged code, but it is possible to call managed callback function which does this. Use Marshal.GetFunctionPointerForDelegate method to pass managed callbak function address to unmanaged code. Keep delegate instance alive all time when callback function can be called.
-
I don't think that it is possible to raise event directly from unmanaged code, but it is possible to call managed callback function which does this. Use Marshal.GetFunctionPointerForDelegate method to pass managed callbak function address to unmanaged code. Keep delegate instance alive all time when callback function can be called.
Thanks for your reply, I've a delegate instance that lives all the time and I sent it to the unmanaged code using Marshal.GetFunctionPointerForDelegate. But I think there is a problem with the GC since it can be reallocated the delegate on the heap. How can it be handle? Thanks
-
Thanks for your reply, I've a delegate instance that lives all the time and I sent it to the unmanaged code using Marshal.GetFunctionPointerForDelegate. But I think there is a problem with the GC since it can be reallocated the delegate on the heap. How can it be handle? Thanks
I think that that delegate is pinned internally by this function. .NET doesn't allow to take unmanaged pointers to managed unpinned objects. You can do this manually using GCHandle, as shown in MSDN topic "GCHandle.Alloc Method (Object)", but I think this is exactly what GetFunctionPointerForDelegate does.