What is pinvoke first? Pinvoke is the abbreviation of platform invoke. Let's take a look at the explanations in the msdn document:
Platform Invoke is a service that enables managed code to call unmanaged functions implemented in Dynamic Link Libraries (DLLs), such as those in the Win32 API.
Obviously, pinvoke is designed to allow us to call the existing local machine.CodeAnd these codes exist in the form of binary DLL. Microsoft is aware of the importance of this interaction, and understands that developers cannot discard all existing things and directly turn to. net. Therefore, the pinvoke feature is added to. net.
So what is pinvoke. Net? Let's take a look at this sentence on the official website:
Think of this as the 21st century version of VB6's API text viewer.
Friends who have used VB6 should be familiar with that API text viewer! In the VB6 era, we need to use Win32 API functions. But is there nothing similar in the. NET era? Of course, now you know the answer to this question-pinvoke. net. You may also find that on its official website, we can also see a very eye-catching slogan:
Do InterOP the Wiki way!
All right, download a pinvoke. NET and install it. I won't bother with the details of this step. There are several reminders, pinvoke.. Net inserts Visual Studio as a plug-in. so, the premise is that you must have Visual Studio. #. net.
First, we will introduce this demonstration project, which is a consoleapplication project called pinvokelab. We will call MessageBox (...) this API is used to say "Hello, pinvoke. net! ". Now let's take a look.Do InterOP the Wiki way!
Right-click the place where you want to insert WIN32API signature (someone translates it into a signature, someone translates it into a structure, and I think I still use the original text, have you noticed that there are two more items on the top of the pop-up menu? Yes, these two items are the things that pinvoke. Net inserts into Visual Studio. NET: "insert pinvoke signatures..." and "contribute pinvoke signatures and types ...".
Select Insert pinvoke signatures... to obtain the following dialog box.
In "what function do you need ?" Write "MessageBox" and press the [go] button on the right. pinvoke. NET will automatically connect to the database www.pinvoke.net for data search.
After the search is completed, the result is returned.
We can see a lot of things with results. First, we have a brief introduction to MessageBox, including the introduction of functions and parameters (in English ). Second, the last update date of the signature of MessageBox. Third, it will be inserted into yourSource code. Fourth,Programming LanguageBecause C # is used here, it will provide the signature of the C # style. However, I create another VB.. Net project. net syntax style Insert the corresponding signature, do not know whether it is eccentric C #, hope it will be improved in future versions, so that more developers will benefit. Fifth, in the lower left corner, we provide suggestions for using managed APIs with similar or identical functions. If so, here is system. Windows. Forms. MessageBox. Show ().
Next, check whether the [insert] key is correct.
It will prompt you that the code has been inserted. Now you can press [close] to return to the original source code, and sort out the code indentation. The Code is 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 the code and view the running result:
Well, you should be clear about how to do it now.Do InterOP the Wiki way !?
At last, I would like to add pinvoke. net. Another menu item is that you upload the signature of WIN32API description to the database www.pinvoke.net. In addition, you can go to the official website to view the WIN32API instructions. Most of these instructions include signature, parameter information, demonstration examples, and optional managed API suggestions ), of course, you can also refer to other documents, such as the msdn library. Sometimes you may find that some pinvoke calls fail, such as exitwindowsex (...), you don't need to worry. When you view the function documentation, you will find that calling this function requires some Windows security measures, such as permission setting. Therefore, before you call WIN32API, first, you must determine the call conditions. OtherwiseProgramIt may have an inexplicable running effect. Another thing to note is that although pinvoke can call WIN32API, it sacrifices some runtime performance. Therefore, if there is a corresponding managed API, I suggest you choose managed API.