المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Unmanaged function pointers / Marshal.GetFunctionPointerForDelegate



C# Programming
06-16-2009, 06:00 PM
I'm using a couple of API calls that require function pointers for callbacks.

So far I've been passing in a delegate instance and it's worked fine. I'm concerned that the delegate reference however may be changed in managed memory. On researching this I came accross the Marshal.GetFunctionPointerForDelegate method.

Some people say this is not needed as passing the delegate directly invokes the internal version of this anyway. Other's suggest that pinning the delegate or the class instance that contains it is necessary.

All MSDN has to offer is "You must manually keep the delegate from being collected by the garbage collector from managed code."

Has anyone had total success (or occaisional failures) just passing the delegate instance directly, or recommend a robust approach?

Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn (http://www.codeproject.com/Members/Luc-Pattyn))
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia (http://uncyclopedia.org/wiki/.NET))
Why are you using VB6? Do you hate yourself? (Christian Graus (http://www.codeproject.com/script/Membership/View.aspx?mid=6556))