Taught you PInvoke

When you write a long application layer code is not required hardware to play it that way?


This time you will come into contact with some wonderful dll, for example user32.dll,kernal32.dll

Of course, these unmanaged code, we can not be used directly in .net, so we will need to use PInvoke call

So you would use DllImportcharacteristics mark a method, introduced unmanaged function

For example, we want to pop up a message box, it will use the following function, add DllImportfeatures, shows that the introduction of methods from which dll

public class Win32
{
    [DllImport("user32.dll")]
    public static extern IntPtr MessageBox(int hWnd, String text, String caption, uint type);
}
class Program
{
    static void Main(string[] args)
    {
        Win32.MessageBox(0, "这是我的博客:https://xinyuehtx.github.io/", "Hello 黄腾霄", 0);

        Console.ReadLine();
    }
}

Here Insert Picture Description

Do not be fooled by these images, how you can say that simple.

We look at the original signature of MessageBox

Here Insert Picture Description
If you're like me, how did wrote c ++, the first feeling must be forced to look ignorant, besides int and uint had nothing else to read.

So we have a step by step look at how to convert c ++ MessageBox for our C # signature

Hands PInvoke

  1. First, open the Programming Reference for -Microsoft Windows API Docs , find the target function MessageBoxintroduced
  2. We can Requirementssee that the DLL bar User32.dll, this is what we in the DllImportname of the dll needed

Here Insert Picture Description

So as we can write (where? Represents the yet to fill in the content)

public class Win32
{
    [DllImport("user32.dll")]
    public static extern ? MessageBox(?);
}
  1. From then we Syntaxfind the function signature

Here Insert Picture Description

  1. Here it is troublesome takes four parameters into corresponding managed type, sometimes involves a number of structures and pointers.

Here we look atParameters

Here Insert Picture Description
The first is a HWNDtype that represents a window handle,

Can HWNDbe memorized = Handle to A Window

Then we can use in c # Intptrtype represents a pointer or handle

Here Insert Picture Description

The second and third arguments areLPCTSTR

LPCTSTR = Long Pointer to a Const TCHAR String so this is a string, we use herestring

Here Insert Picture Description

The last one is UINTthat we have a direct corresponding in c #uint

Such a look is not to be able to understand more of it.

Practical operation

Again a simple example, we expect to obtain HID device interface GUID

Methods to come to you, is HidD_GetHidGuid

Look at requirements.txt, DLL is foundHid.dll

Here Insert Picture Description

Followed by the signature and parameters, LPGUIDwe have not mentioned, see explanation here is a pointer pointing to a GUID, so we use Guidthis type

Here Insert Picture Description

So now we get the following results

[DllImport("hid.dll")]
public static extern void HidD_GetHidGuid(Guid hidGuid);

But note, we WindowsApi signature parameter type is a pointer, and now we pass Guidonly a structure

So we also need to be 引用passed by, by addingref

So is the final form

[DllImport("hid.dll")]
public static extern void HidD_GetHidGuid(ref Guid hidGuid);
var guid = Guid.Empty;

Win32.HidD_GetHidGuid(ref guid);

Console.WriteLine(guid);

Run can see the results as follows

Here Insert Picture Description

Tip

Some students say, so finished, I still worry about how to do wrong

It does not matter, here for everyone to recommend a site pinvoke.net:! At The Interop Wiki , which brought together various pinvoke wording, if you are unsure how to use, you can go where you can view

In addition vs pinvoke also with plug-ins, use can be found in Lu Yi students blog use PInvoke.net Visual Studio Extension assist with writing Win32 function signature - walterlv

Reference links:


This article will be updated frequently, please read personal blog Original: https://xinyuehtx.github.io/ , in order to avoid misleading the old error of knowledge, and a better reading experience.

Creative Commons License This work is Creative Commons Attribution - NonCommercial - ShareAlike 4.0 International License Agreement for licensing. Welcome to reprint, use, repost, but be sure to keep the article signed by Huang Tengxiao (containing links: https://xinyuehtx.github.io/ ), shall not be used for commercial purposes, be sure to publish the same work based on the paper license modification. If you have any questions, please contact me .

Published 68 original articles · won praise 0 · Views 2950

Guess you like

Origin blog.csdn.net/htxhtx123/article/details/104323450