Pinvoke.net: Do Interop The Wiki Way!
Updated on Friday, October 29, 2004
Writen by Allen Lee
What is PINVOKE? Pinvoke is an abbreviation of Platform Invoke. Let's first take a look at the explanation in the MSDN documentation:
Platform Invoke IS a Service That Enables Managed Code To Call Unmanaged Functions Implement in Dynamic Link Libraries (DLLS), Such As Those In The Win32 API.
Obviously, Pinvoke is to allow us to invoke existing native code, and these code exists in the form of binary DLL. Microsoft realizes the importance of this interaction, and also knows that developers cannot discard all existing things directly to .NET, and add Pinvoke in .NET. This feature is added. So what is pinvoke.net? Let's take a look at the sentence above the official website:
Think of this as the 21st Century Version of VB6'S API TEXT Viewer.
Friends who have used VB6 should be unfamiliar with that API Text Viewer! In the era of VB6, you need to call those Win32 API functions, you need to use this stuff. However, is there a similar thing in the .NET era? Of course, now you know the answer to this question --pinvoke.net. Perhaps, you have also found it on its official website, we can also see a very eye-catching slogan:
Do Interop The Wiki Way!
Ok, download a pinvoke.net and install it, I will not be embarrassed. There are a few points to be reminded, and Pinvoke.net is inserted into Visual Studio .NET in the form of a plugin, so, the premise is that you must have a version of Visual Studio .Net, here is used in Visual C # .NET to make demonstrations.
First introduce this demonstration project, it is a consoleapplication project called pinvokelab, and we will call the MessageBox (...) API to say "Hello, Pinvoke.net!" To the user. Let us feel how to do Do Interop The Wiki Way!
In you need to insert Win32API Signature (someone translates it into a signature, someone translates it into the form, I still use back to the original) place to click the right mouse button, do you pay attention to this pop-up menu? That's right, these two are Pinvoke.Net inserted to Visual Studio .NET: "INSERT PINVOKE SIGNATURES ..." and "Contribute Pinvoke Signatures and Types ...". Then select "INSERT PINVOKE SIGNATURES ..." to get the following dialog. Write "Me Function Do You NEED?", "MessageBox", press the [GO] button on the right, and Pinvoke.net will automatically connect to the database lookup data for www.pinvoke.net. The result will be returned if you look for it. On this screenshot with results, we can see a lot of things. First, there is a brief introduction to MessageBox, including the introduction of features and parameters (English). Second, the last update date of the Signature of MessageBox. Third, the code snippet will be inserted into your source code. Fourth, the selection of programming languages, because of the use of C # here, it will give the Signature of the C # style. However, the author deliberately creates a VB.NET's Project, found that it cannot insert the corresponding signature in vb.net syntax style, don't know if it eccentrically C #, I hope it has improved in the future version, let more developers Benefit. Fifth, there is a managed API that provides similar or identical functions as a suggestion, if any, here is System.Windows.Forms.MessageBox.show (). Then, you check it out, no problem, press the [INSERT] button. It will prompt your code to have been inserted. Now you can press [Close] to return to the original source code, and organize the indentation of the code. Finishing code as follows: using System; using System.Runtime.InteropServices; namespace PInvokeLab {class Tester {[STAThread] static void Main (string [] args) {MessageBox (0, "Hello, PInvoke.NET", "PInvokeLab" , 0);} [DLLIMPORT ("User32.dll", Charset = Charset.Auto)] Public Static Extern Int MessageBox (int hWnd, string text, string caption, uint type);}}
Compile this code now and check the results:
Ok, I said that you should do it now, you should be very clear, don't hurry to do Interop The Wiki Way! ?