Marshal Methods


.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)

Syntax

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.
Exceptions

Exception typeCondition

ArgumentNullException

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

Remarks

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.

.NET Framework Security

Platforms

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.

Version Information

.NET Framework

Supported in: 2.0

.NET Compact Framework

Supported in: 2.0
See Also

Tags :


Community Content

RFOG
Only valid for __stdcall calling convention

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.


Shri Borde
UnmanagedFunctionPointerAttribute
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 :

Page view tracker