In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you the example analysis of the parameter types of C# calling Windows API, I believe that most people do not know much about it, so share this article for your reference. I hope you will gain a lot after reading this article. Let's learn about it together.
Parameter type 1 of C # calling Windows API.
The numerical type can be used directly with the corresponding one.
(DWORD-> int, WORD-> Int16)
Parameter type 2 of C # calling Windows API.
String pointer type in API-> string in .net
Parameter type 3 of C # calling Windows API.
Handle in API (dWord)-> IntPtr in .net
Parameter type 4 of C # calling Windows API.
Structure in API->. Net structure or class. Note that in this case, you should first qualify the declaration structure or class with the StructLayout attribute
The common language runtime uses StructLayoutAttribute to control the physical layout of the data fields of a class or structure in managed memory, that is, the class or structure needs to be arranged in some way. If you want to pass a class to unmanaged code that needs to specify a layout, it is important to explicitly control the class layout. Its constructor initializes a new instance of the StructLayoutAttribute class with the LayoutKindle value. LayoutKind.Sequential is used to force members to be laid out in the order in which they appear.
LayoutKind.Explicit is used to control the exact location of each data member. With Explicit, each member must use FieldOffsetAttribute to indicate the location of this field in the type. Such as:
[StructLayout (LayoutKind.Explicit, Size=16, CharSet=CharSet.Ansi)] public class MySystemTime {[FieldOffset (0)] public ushort wYear; [FieldOffset (2)] public ushort wMonth; [FieldOffset (4)] public ushort wDayOfWeek; [FieldOffset (6)] public ushort wDay; [FieldOffset (8)] public ushort wHour; [FieldOffset (10)] public ushort wMinute; [FieldOffset (12)] public ushort wSecond; [FieldOffset (14)] public ushort wMilliseconds;}
An example of the parameter type of calling Windows API by C #:
The following is an example of defining a corresponding class or structure in .net for the OSVERSIONINFO structure in API:
/ * * API defines the original structure declaration * OSVERSIONINFOA STRUCT * dwOSVersionInfoSize DWORD? * dwMajorVersion DWORD? * dwMinorVersion DWORD? * dwBuildNumber DWORD? * dwPlatformId DWORD ? * szCSDVersion BYTE 128 dup (?) * OSVERSIONINFOA ENDS * * OSVERSIONINFO equ * / / .net declared as class [StructLayout (LayoutKind.Sequential)] public class OSVersionInfo {public int OSVersionInfoSize Public int majorVersion; public int minorVersion; public int buildNumber; public int platformId; [MarshalAs (UnmanagedType.ByValTStr, SizeConst=128)] public String versionString;} / or / / .net declared as a structure [StructLayout (LayoutKind.Sequential)] public struct OSVersionInfo2 {public int OSVersionInfoSize; public int majorVersion; public int minorVersion; public int buildNumber; public int platformId; [MarshalAs (UnmanagedType.ByValTStr, SizeConst=128)] public String versionString;}
The MashalAs feature is used in this example to describe the marshaling format of a field, method, or parameter. Use it as a parameter prefix and specify the data type required by the target. For example, the following code marshals two parameters as a data type long pointer to the string (LPStr) of the Windows API function:
[MarshalAs (UnmanagedType.LPStr)] String existingfile; [MarshalAs (UnmanagedType.LPStr)] String newfile
Note on the parameter type of calling Windows API by C #:
Note that when the structure is used as a parameter, it is generally preceded by the ref modifier, otherwise there will be an error: the reference to the object does not specify an instance of the object.
[DllImport ("kernel32", EntryPoint= "GetVersionEx")] public static extern bool GetVersionEx2 (ref OSVersionInfo2 osvi); the above is all the contents of the article "sample analysis of parameter types of C# calling Windows API". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.