This documentation is archived and is not being maintained.

Marshal.GetDelegateForFunctionPointer Method

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

Converts an unmanaged function pointer to a delegate.

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

public static Delegate GetDelegateForFunctionPointer (
	IntPtr ptr,
	Type t
)
public static Delegate GetDelegateForFunctionPointer (
	IntPtr ptr, 
	Type t
)
public static function GetDelegateForFunctionPointer (
	ptr : IntPtr, 
	t : Type
) : Delegate

Parameters

ptr

An System.IntPtr type that is the unmanaged function pointer to be converted.

t

The type of the delegate to be returned.

Return Value

A delegate instance that can be cast to the appropriate delegate type.

Exception typeCondition

ArgumentException

The t parameter is not a delegate.

ArgumentNullException

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

-or-

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

In versions 1.0 and 1.1 of the .NET Framework, it was possible to pass a delegate representing a managed method to unmanaged code as a function pointer, allowing the unmanaged code to call the managed method through the function pointer. It was also possible for the unmanaged code to pass that function pointer back to the managed code, and the pointer was resolved properly to the underlying managed method.

Using this new method, GetDelegateForFunctionPointer, and a second new method, GetFunctionPointerForDelegate, you can now marshal delegates in both directions. With GetDelegateForFunctionPointer, ptr is imported as an IntPtr; an IntPtr can be obtained for a managed delegate by calling GetFunctionPointerForDelegate and passed as a parameter; then it can be called from inside the unmanaged method. Note that the parameter marshaler can also marshal function pointers to delegates in version 2.0.

NoteNote

You cannot pass an invalid function pointer to GetDelegateForFunctionPointer. In addition, you can only use this method for pure unmanaged function pointers. You cannot use this method with function pointers obtained through C++ or from GetFunctionPointer. You cannot use this method to create a delegate from a function pointer to another managed delegate.

Windows 98, Windows 2000 SP4, Windows Millennium Edition, 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
Show: