Groups | Blog | Home
all groups > dotnet clr > june 2004 >

dotnet clr : Must you really pin delegates when marshalling them as function pointers?


Bern McCarty
6/22/2004 12:43:12 PM

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.

dmortens NO[at]SPAM online.microsoft.com
6/30/2004 9:42:05 PM
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.

--------------------
[quoted text, click to view]

AddThis Social Bookmark Button