Recently, I have seen some basic InterOP problems on the Forum, giving me the motivation to write the previous. Net InterOP getting started series, and giving my friends who have just been involved in. Net InterOP a general concept.
Every time I talk about. Net InterOP, the following figure is always displayed in my mind:
The figure shows four typical scenarios of. Net InterOP. Previously, my colleagues and I discussed the applications of. NET and COM interoperability:
- Getting started with calling COM: COM InterOP in. net
- Call. Net in COM: Use the. NET Component in the com application, and use the idispatch: invoke function to call the Managed class library method implemented by C # In C ++.
Today, I will focus on P/invoke and reverse P/invoke. Compared with COM InterOP, P/invoke does not need to register components, so it is lighter and greener.
1. P/invoke
P/invoke (platform invoke) is a lightweight Method for. Net to call native code. You only need to write the local code into a dynamic link library, and then declare an external static function in the C # code, and use the dllimport attribute to specify the portal for the dynamic Connection Library. Example:
using System;using System.Runtime.InteropServices;class PInvoke{ [DllImportAttribute("user32.dll", EntryPoint = "MessageBoxW")] public static extern int MessageBoxW( [In]System.IntPtr hWnd, [In][MarshalAs(UnmanagedType.LPWStr)] string lpText, [In][MarshalAs(UnmanagedType.LPWStr)] string lpCaption, uint uType); public static void Main() { MessageBoxW(IntPtr.Zero, "Hello", "Interop", 0); }}
Explain the Code a little. In the pinvoke class, there is a function declaration of messageboxw, which is implemented in user32.dll (which is included in the system). The entry is messageboxw, and the parameter composition is determined by the declaration of Windows API, we have a tool on codeplex to help you claim that a function written in local code (C ++) has been declared in code (C, previously, our team members wrote articles about the use of this tool.
With this statement, calling MessageBox in main is as easy as calling other hosted code.
2. Reverse P/invoke
Next, let's see how to call the. Net Method in the local code. The local code needs to get a. Net delegate (delegate), and then use this delegate as a function pointer, for example:
using System;using System.Windows.Forms;using System.Runtime.InteropServices;public class Program{ internal delegate void DelegateMessageBox([MarshalAs(UnmanagedType.LPWStr)]string msg); [DllImport("Native.dll", CallingConvention = CallingConvention.Cdecl)] static extern void NativeMethod(DelegateMessageBox d); public static void ShowMessageBox(string msg) { MessageBox.Show(msg); } public static void Main() { NativeMethod(new DelegateMessageBox(ShowMessageBox)); }}
In this example, we hope that the local code can call the hosted function showmessagebox to display a dialog box. In order for the local code to call this function, we decided a delegate based on its declaration and passed the delegate to the local code through P/invoke. The local code can call the managed Code as follows:
#include <stdio.h>#include <wtypes.h>extern "C" { __declspec(dllexport) void NativeMethod(void (__stdcall *pShowMsgBox)(WCHAR *wChar)) { (*pShowMsgBox)(L"hello reverse interop"); }}
Note that the delegate in the managed code is a function pointer to the local code. The local code can call the hosted code like a common function pointer.
You may notice that the DLL Declaration uses the extern "C", which indicates that the call specification is cdecl, and the call conventions are also specified in the dllimport Code previously written, for more information about call conventions, see my other blog.
Today's introduction is here. You can use these sample codes as a template and make specific changes as needed.
Http://blogs.msdn.com/ B /silverlightshanghai/archive/2009/03/29/net-interop-p-invoke-reverse-p-invoke.aspx