Using the May CTP of VS 2005 I read an article "How to: Marshal Unicode
Strings using C++ Interop". That article talks about the need to pin the
delegate prior to marshaling it out to native code as a function pointer.
But then in the 2nd example it proceeds to NOT pin it and to instead simply
do a GCHandle.Alloc(delegate) on it.
So, the question is, do you have to pin them or is it enough to make sure
that they remain referenced?
Bern McCarty
Bentley Systems, Inc.
David Mortenson [MSFT] - 30 Jun 2004 22:42 GMT
As long as the CLR Interop layer is the one marshalling the delegate to a function pointer then you don't need to pin it, you just need to ensure it stays
referenced.
So when passing out the delegate to native code as a function pointer using the following P/Invoke:
[DllImport(...)]
void GiveFunction(MyDelegate d);
You don't need to pin the delegate
Thanks,
David Mortenson
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
>From: "Bern McCarty" <bern.mccarty@bentley.com>
>Subject: Must you really pin delegates when marshalling them as function pointers?
[quoted text clipped - 22 lines]
>Bern McCarty
>Bentley Systems, Inc.