Export (0) Print
Expand All

Marshal.GetDelegateForFunctionPointer Method (IntPtr, Type)

Note: This API is now obsolete.

Converts an unmanaged function pointer to a delegate.

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

'Declaration
<ObsoleteAttribute("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")> _
Public Shared Function GetDelegateForFunctionPointer ( _
	ptr As IntPtr, _
	t As Type _
) As Delegate

Parameters

ptr
Type: System.IntPtr

The unmanaged function pointer to be converted.

t
Type: System.Type

The type of the delegate to be returned.

Return Value

Type: System.Delegate
A delegate instance that can be cast to the appropriate delegate type.

ExceptionCondition
ArgumentException

The t parameter is not a delegate or is generic.

ArgumentNullException

The ptr parameter is Nothing.

-or-

The t parameter is Nothing.

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.

The GetDelegateForFunctionPointer method has the following restrictions:

  • Generics are not supported in interop scenarios.

  • 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.6, 4.5, 4, 3.5, 3.0, 2.0
Obsolete (compiler warning) in 4.6

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

  • SecurityCriticalAttribute 

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

Show:
© 2015 Microsoft