Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
Previous Versions
.NET Framework 2.0
Marshal Class
Marshal Methods
 GetFunctionPointerForDelegate Metho...

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
.NET Framework Class Library
Marshal.GetFunctionPointerForDelegate Method

Note: This method is new in the .NET Framework version 2.0.

Converts a delegate into a function pointer callable from unmanaged code.

Namespace: System.Runtime.InteropServices
Assembly: mscorlib (in mscorlib.dll)

Visual Basic (Declaration)
Public Shared Function GetFunctionPointerForDelegate ( _
    d As Delegate _
) As IntPtr
Visual Basic (Usage)
Dim d As Delegate
Dim returnValue As IntPtr

returnValue = Marshal.GetFunctionPointerForDelegate(d)
C#
public static IntPtr GetFunctionPointerForDelegate (
    Delegate d
)
C++
public:
static IntPtr GetFunctionPointerForDelegate (
    Delegate^ d
)
J#
public static IntPtr GetFunctionPointerForDelegate (
    Delegate d
)
JScript
public static function GetFunctionPointerForDelegate (
    d : Delegate
) : IntPtr

Parameters

d

The delegate to be passed to unmanaged code.

Return Value

An System.IntPtr value that can be passed to unmanaged code, which in turn can use it to call the underlying managed delegate.
Exception typeCondition

ArgumentNullException

The d parameter is a null reference (Nothing in Visual Basic).

The delegate d is converted to a function pointer that can be passed to unmanaged code.

Note that you must manually keep the delegate from being collected by the garbage collector (GC) from managed code. The GC does not track reference to unmanaged code.

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

.NET Compact Framework

Supported in: 2.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Only valid for __stdcall calling convention      RFOG   |   Edit   |   Show History

GetFunctionPointerForDelegate returns a pointer that is only ready for __stdcall calling convention. Then, if it is used in a callback scenario, the callback must use __stdcall calling convention, as all of the Windows callbacks does. If you use a 3rd party library that uses non __stdcall callbacks you will get strange malfunctions.

For example, if you need a callback that has the signature "bool Notification(char a,int b);", that function MUST use __stdcall convention and when using in a C++/CLI scenario, the casting must be explicitly done:

IntPtr p=Marshal::GetFunctionPointerForDelegate(theDelegate);

bool (__stdcall *pfnc)(char,int)=(bool (__stdcall*)(char,int))p;

InstallCallback(pfnc); //InstallCallback receives a native callback whit "bool fnc(char,char)" signature.

UnmanagedFunctionPointerAttribute      Shri Borde   |   Edit   |   Show History
You can use UnmanagedFunctionPointerAttribute (http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.unmanagedfunctionpointerattribute.aspx) on a delegate to indicate how it should be marshaled to unmamanaged code
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker