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. |