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] >From: "Bern McCarty" <bern.mccarty@bentley.com>
>Subject: Must you really pin delegates when marshalling them as function pointers?
>Date: Tue, 22 Jun 2004 12:43:12 -0400
>Lines: 14
>X-Priority: 3
>X-MSMail-Priority: Normal
>X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
>X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
>Message-ID: <uUtZDgHWEHA.2176@TK2MSFTNGP11.phx.gbl>
>Newsgroups: microsoft.public.dotnet.framework.clr
>NNTP-Posting-Host: bsi-uu.bentley.com 64.90.224.40
>Path: cpmsftngxa10.phx.gbl!TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP11.phx.gbl
>Xref: cpmsftngxa10.phx.gbl microsoft.public.dotnet.framework.clr:10993
>X-Tomcat-NG: microsoft.public.dotnet.framework.clr
>
>
>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.
>
>
>