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 DllImport
characteristics mark a method, introduced unmanaged function
For example, we want to pop up a message box, it will use the following function, add DllImport
features, 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();
}
}
Do not be fooled by these images, how you can say that simple.
We look at the original signature of MessageBox
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
- First, open the Programming Reference for -Microsoft Windows API Docs , find the target function
MessageBox
introduced - We can
Requirements
see that the DLL barUser32.dll
, this is what we in theDllImport
name of the dll needed
So as we can write (where? Represents the yet to fill in the content)
public class Win32
{
[DllImport("user32.dll")]
public static extern ? MessageBox(?);
}
- From then we
Syntax
find the function signature
- Here it is troublesome takes four parameters into corresponding managed type, sometimes involves a number of structures and pointers.
Here we look atParameters
The first is a HWND
type that represents a window handle,
Can HWND
be memorized = Handle to A Window
Then we can use in c # Intptr
type represents a pointer or handle
The second and third arguments areLPCTSTR
LPCTSTR
= Long Pointer to a Const TCHAR String so this is a string, we use herestring
The last one is UINT
that 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
Followed by the signature and parameters, LPGUID
we have not mentioned, see explanation here is a pointer pointing to a GUID, so we use Guid
this type
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 Guid
only 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
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:
- pinvoke.net: the interop wiki!
- Platform Invoke Examples - Microsoft Docs
- Passing Structures -Microsoft Docs
- Use PInvoke.net Visual Studio Extension assist with writing Win32 function signature - walterlv
- MessageBox function - Microsoft Docs
- Programming reference for Windows API - Microsoft Docs
- HidD_GetHidGuid function (hidsdi.h) - Windows drivers 0 - Microsoft Docs
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.
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 .