Marshal.GetDelegateForFunctionPointer Method

Definition

Overloads

GetDelegateForFunctionPointer(IntPtr, Type)
Obsolete.

Converts an unmanaged function pointer to a delegate.

GetDelegateForFunctionPointer<TDelegate>(IntPtr)

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

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

GetDelegateForFunctionPointer(IntPtr, Type)

Caution

GetDelegateForFunctionPointer(IntPtr, Type) may be unavailable in future releases. Instead, use GetDelegateForFunctionPointer(IntPtr). For more info, go to http://go.microsoft.com/fwlink/?LinkID=296521

Converts an unmanaged function pointer to a delegate.

public:
 static Delegate ^ GetDelegateForFunctionPointer(IntPtr ptr, Type ^ t);
public static Delegate GetDelegateForFunctionPointer (IntPtr ptr, Type t);
[System.Obsolete("GetDelegateForFunctionPointer(IntPtr, Type) may be unavailable in future releases. Instead, use GetDelegateForFunctionPointer<T>(IntPtr). For more info, go to http://go.microsoft.com/fwlink/?LinkID=296521")]
[System.Security.SecurityCritical]
public static Delegate GetDelegateForFunctionPointer (IntPtr ptr, Type t);
[System.Security.SecurityCritical]
public static Delegate GetDelegateForFunctionPointer (IntPtr ptr, Type t);
static member GetDelegateForFunctionPointer : nativeint * Type -> Delegate
[<System.Obsolete("GetDelegateForFunctionPointer(IntPtr, Type) may be unavailable in future releases. Instead, use GetDelegateForFunctionPointer<T>(IntPtr). For more info, go to http://go.microsoft.com/fwlink/?LinkID=296521")>]
[<System.Security.SecurityCritical>]
static member GetDelegateForFunctionPointer : nativeint * Type -> Delegate
[<System.Security.SecurityCritical>]
static member GetDelegateForFunctionPointer : nativeint * Type -> Delegate
Public Shared Function GetDelegateForFunctionPointer (ptr As IntPtr, t As Type) As Delegate

Parameters

ptr
IntPtr

The unmanaged function pointer to be converted.

t
Type

The type of the delegate to be returned.

Returns

Delegate

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

Attributes

Exceptions

The t parameter is not a delegate or is generic.

The ptr parameter is null.

-or-

The t parameter is null.

Remarks

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.

In the .NET Framework 2.0 and later versions, you can use the GetDelegateForFunctionPointer and GetFunctionPointerForDelegate methods to marshal delegates in both directions. With GetDelegateForFunctionPointer, ptr is imported as a System.IntPtr. A System.IntPtr can be obtained for a managed delegate by calling GetFunctionPointerForDelegate and passed as a parameter; it can then be called from inside the unmanaged method. Note that the parameter marshaler can also marshal function pointers to delegates in the .NET Framework 2.0 and later versions.

ptr is converted to a delegate that invokes the unmanaged method using the __stdcall calling convention on Windows, or the __cdecl calling convention on Linux and macOS. You can set the calling convention by applying the UnmanagedFunctionPointerAttribute to the delegate.

The GetDelegateForFunctionPointer method has the following restrictions:

  • Generics are not supported in interop scenarios.

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

  • You cannot use this method with function pointers obtained through C++.

Applies to

Product Introduced

GetDelegateForFunctionPointer<TDelegate>(IntPtr)

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

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

public:
generic <typename TDelegate>
 static TDelegate GetDelegateForFunctionPointer(IntPtr ptr);
public static TDelegate GetDelegateForFunctionPointer<TDelegate> (IntPtr ptr);
[System.Security.SecurityCritical]
public static TDelegate GetDelegateForFunctionPointer<TDelegate> (IntPtr ptr);
static member GetDelegateForFunctionPointer : nativeint -> 'Delegate
[<System.Security.SecurityCritical>]
static member GetDelegateForFunctionPointer : nativeint -> 'Delegate
Public Shared Function GetDelegateForFunctionPointer(Of TDelegate) (ptr As IntPtr) As TDelegate

Type Parameters

TDelegate

The type of the delegate to return.

Parameters

ptr
IntPtr

The unmanaged function pointer to convert.

Returns

TDelegate

A instance of the specified delegate type.

Attributes

Exceptions

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

The ptr parameter is null.

Remarks

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

ptr is converted to a delegate that invokes the unmanaged method using the __stdcall calling convention on Windows, or the __cdecl calling convention on Linux and macOS. You can set the calling convention by applying the UnmanagedFunctionPointerAttribute to the delegate.

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

  • Generics are not supported in interop scenarios.

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

  • You cannot use this method with function pointers obtained through C++.

Applies to

Product Introduced