Marshal.GetDelegateForFunctionPointer<TDelegate> Method (IntPtr)

[Supported in the .NET Framework 4.5.1 and later versions]

Converts an unmanaged function pointer to a delegate of a specified type.

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

public static TDelegate GetDelegateForFunctionPointer<TDelegate>(
	IntPtr ptr
)

Type Parameters

TDelegate

The type of the delegate to return.

Parameters

ptr
Type: System.IntPtr

The unmanaged function pointer to convert.

Return Value

Type: TDelegate
A instance of the specified delegate type.

ExceptionCondition
ArgumentException

The TDelegate generic parameter is not a delegate, or it is an open generic type.

ArgumentNullException

The ptr parameter is null.

You can use the GetDelegateForFunctionPointer<TDelegate>(IntPtr) and GetFunctionPointerForDelegate<TDelegate>(TDelegate) methods to marshal delegates in both directions.

The GetDelegateForFunctionPointer<TDelegate>(IntPtr) method has the following restrictions:

  • You cannot pass an invalid function pointer to this method.

  • You can use this method only for pure unmanaged function pointers.

  • You cannot use this method with function pointers obtained through C++ or from the GetFunctionPointer method.

  • You cannot use this method to create a delegate from a function pointer to another managed delegate.

.NET Framework

Supported in: 4.5.1

.NET for Windows Phone apps

Supported in: Windows Phone 8.1

  • SecurityCriticalAttribute 

    requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.

Windows Phone 8.1

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

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft