
Assembly: mscorlib (in mscorlib.dll)
'Declaration Public Shared Function GetDelegateForFunctionPointer ( _ ptr As IntPtr, _ t As Type _ ) As Delegate
'Usage Dim ptr As IntPtr Dim t As Type Dim returnValue As Delegate returnValue = Marshal.GetDelegateForFunctionPointer(ptr, t)
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 type | Condition |
|---|---|
The t parameter is not a delegate. | |
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.
Note: |
|---|
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. |
- SecurityPermission for permission to call unmanaged code. Associated enumeration: UnmanagedCode Security action: LinkDemand
Windows 98, Windows Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.ImportsSystem.Runtime.InteropServices
Friend Class EntryPoint
Class NativeMethods
Private Const KERNEL32_DLL As String = "Kernel32.dll"
<DllImport(KERNEL32_DLL)> _
Friend Shared Function LoadLibrary(ByVal szFileName As String) As IntPtr
End Function
<DllImport(KERNEL32_DLL)> _
Friend Shared Function GetProcAddress(ByVal hLibrary As IntPtr, ByVal szProcName As String) As IntPtr
End Function
<DllImport(KERNEL32_DLL)> _
Friend Shared Function FreeLibrary(ByVal hLibrary As IntPtr) As Integer
End Function
End Class
<UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet:=CharSet.Unicode)> _
Delegate Function MessageBoxProto(ByVal hWndParent As IntPtr, ByVal szText As String, ByVal szTitle As String, ByVal ulFlags As UInt32) As Integer
Friend Shared Sub Main()
Dim hUser32 As IntPtr = NativeMethods.LoadLibrary("User32.dll")
Dim pMethod As IntPtr = NativeMethods.GetProcAddress(hUser32, "MessageBoxW")
Dim MessageBox As MessageBoxProto = DirectCast(System.Runtime.InteropServices.Marshal.GetDelegateForFunctionPointer(pMethod, GetType(MessageBoxProto)), MessageBoxProto)
MessageBox.Invoke(IntPtr.Zero, "hello there", "cool!", Convert.ToUInt32(MsgBoxStyle.Information Or MsgBoxStyle.SystemModal))
NativeMethods.FreeLibrary(hUser32)
End Sub
End Class

- 9/5/2010
- Vozzie
