C#USB设备枚举Kernel32的PInvoke

时间:2024-09-21 20:35:51
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Security;
  4. namespace Splash
  5. {
  6. #region Kernel32
  7. [SuppressUnmanagedCodeSecurity]
  8. internal static class Kernel32
  9. {
  10. /// <summary>
  11. /// 无效的文件句柄
  12. /// </summary>
  13. public static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
  14. #region CreateFile
  15. [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  16. public static extern IntPtr CreateFile(
  17. String fileName,
  18. [MarshalAs(UnmanagedType.U4)] NativeFileAccess fileAccess,
  19. [MarshalAs(UnmanagedType.U4)] NativeFileShare fileShare,
  20. IntPtr securityAttributes,
  21. [MarshalAs(UnmanagedType.U4)] NativeFileMode creationDisposition,
  22. NativeFileFlag flags,
  23. IntPtr template
  24. );
  25. [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  26. public static extern IntPtr CreateFile(
  27. String fileName,
  28. [MarshalAs(UnmanagedType.U4)] NativeFileAccess fileAccess,
  29. [MarshalAs(UnmanagedType.U4)] NativeFileShare fileShare,
  30. IntPtr securityAttributes,
  31. [MarshalAs(UnmanagedType.U4)] NativeFileMode creationDisposition,
  32. IntPtr flags,
  33. IntPtr template
  34. );
  35. #endregion
  36. [DllImport("kernel32.dll", SetLastError = true)]
  37. [return: MarshalAs(UnmanagedType.Bool)]
  38. public static extern Boolean CloseHandle(IntPtr hFile);
  39. #region ReadFile
  40. [DllImport("kernel32.dll", SetLastError = true)]
  41. public static extern Boolean ReadFile(
  42. IntPtr hFile,
  43. [Out] Byte[] lpBuffer,
  44. Int32 nNumberOfBytesToRead,
  45. out Int32 lpNumberOfBytesRead,
  46. [In] ref System.Threading.NativeOverlapped lpOverlapped
  47. );
  48. [DllImport("kernel32.dll", SetLastError = true)]
  49. public static extern Boolean ReadFile(
  50. IntPtr hFile,
  51. [Out] Byte[] lpBuffer,
  52. Int32 nNumberOfBytesToRead,
  53. IntPtr lpNumberOfBytesRead,
  54. [In] ref System.Threading.NativeOverlapped lpOverlapped
  55. );
  56. [DllImport("kernel32.dll", SetLastError = true)]
  57. public static extern Boolean ReadFile(
  58. IntPtr hFile,
  59. [Out] Byte[] lpBuffer,
  60. Int32 nNumberOfBytesToRead,
  61. out Int32 lpNumberOfBytesRead,
  62. IntPtr lpOverlapped
  63. );
  64. #endregion
  65. #region WriteFile
  66. [DllImport("kernel32.dll")]
  67. public static extern Boolean WriteFile(
  68. IntPtr hFile,
  69. Byte[] lpBuffer,
  70. Int32 nNumberOfBytesToWrite,
  71. out Int32 lpNumberOfBytesWritten,
  72. [In] ref System.Threading.NativeOverlapped lpOverlapped
  73. );
  74. [DllImport("kernel32.dll")]
  75. public static extern Boolean WriteFile(
  76. IntPtr hFile,
  77. Byte[] lpBuffer,
  78. Int32 nNumberOfBytesToWrite,
  79. IntPtr lpNumberOfBytesWritten,
  80. [In] ref System.Threading.NativeOverlapped lpOverlapped
  81. );
  82. [DllImport("kernel32.dll")]
  83. public static extern Boolean WriteFile(
  84. IntPtr hFile,
  85. Byte[] lpBuffer,
  86. Int32 nNumberOfBytesToWrite,
  87. IntPtr lpNumberOfBytesWritten,
  88. IntPtr lpOverlapped
  89. );
  90. #endregion
  91. [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  92. public static extern Boolean GetOverlappedResult(
  93. IntPtr hFile,
  94. [In] ref System.Threading.NativeOverlapped lpOverlapped,
  95. out Int32 lpNumberOfBytesTransferred,
  96. Boolean bWait
  97. );
  98. }
  99. #endregion
  100. #region ENUM
  101. [Flags]
  102. internal enum NativeFileAccess : uint
  103. {
  104. GENERIC_READ = (0x80000000),
  105. GENERIC_WRITE = (0x40000000),
  106. GENERIC_EXECUTE = (0x20000000),
  107. GENERIC_ALL = (0x10000000),
  108. FILE_SPECIAL = 0,
  109. FILE_APPEND_DATA = (0x0004),    // 文件
  110. FILE_READ_DATA = (0x0001),      // 文件和管道
  111. FILE_WRITE_DATA = (0x0002),     // 文件和管道
  112. FILE_READ_EA = (0x0008),        // 文件和目录
  113. FILE_WRITE_EA = (0x0010),       // 文件和目录
  114. FILE_READ_ATTRIBUTES = (0x0080),    // 所有
  115. FILE_WRITE_ATTRIBUTES = (0x0100),   // 所有
  116. DELETE = 0x00010000,
  117. READ_CONTROL = (0x00020000),
  118. WRITE_DAC = (0x00040000),
  119. WRITE_OWNER = (0x00080000),
  120. SYNCHRONIZE = (0x00100000),
  121. STANDARD_RIGHTS_REQUIRED = (0x000F0000),
  122. STANDARD_RIGHTS_READ = (READ_CONTROL),
  123. STANDARD_RIGHTS_WRITE = (READ_CONTROL),
  124. STANDARD_RIGHTS_EXECUTE = (READ_CONTROL),
  125. STANDARD_RIGHTS_ALL = (0x001F0000),
  126. SPECIFIC_RIGHTS_ALL = (0x0000FFFF),
  127. FILE_GENERIC_READ = (STANDARD_RIGHTS_READ | FILE_READ_DATA | FILE_READ_ATTRIBUTES | FILE_READ_EA | SYNCHRONIZE),
  128. FILE_GENERIC_WRITE = (STANDARD_RIGHTS_WRITE | FILE_WRITE_DATA | FILE_WRITE_ATTRIBUTES | FILE_WRITE_EA | FILE_APPEND_DATA | SYNCHRONIZE),
  129. SPECIAL = 0
  130. }
  131. internal enum NativeFileMode : uint
  132. {
  133. CREATE_NEW = 1,
  134. CREATE_ALWAYS = 2,
  135. OPEN_EXISTING = 3,
  136. OPEN_ALWAYS = 4,
  137. TRUNCATE_EXISTING = 5,
  138. }
  139. [Flags]
  140. internal enum NativeFileShare : uint
  141. {
  142. NONE = 0,
  143. FILE_SHARE_READ = 0x00000001,
  144. FILE_SHARE_WRITE = 0x00000002,
  145. FILE_SHARE_DEELETE = 0x00000004,
  146. }
  147. [Flags]
  148. internal enum NativeFileFlag : uint
  149. {
  150. FILE_ATTRIBUTE_READONLY = 0x00000001,
  151. FILE_ATTRIBUTE_HIDDEN = 0x00000002,
  152. FILE_ATTRIBUTE_SYSTEM = 0x00000004,
  153. FILE_ATTRIBUTE_DIRECTORY = 0x00000010,
  154. FILE_ATTRIBUTE_ARCHIVE = 0x00000020,
  155. FILE_ATTRIBUTE_DEVICE = 0x00000040,
  156. FILE_ATTRIBUTE_NORMAL = 0x00000080,
  157. FILE_ATTRIBUTE_TEMPORARY = 0x00000100,
  158. FILE_ATTRIBUTE_SPARSE_FILE = 0x00000200,
  159. FILE_ATTRIBUTE_REPARSE_POINT = 0x00000400,
  160. FILE_ATTRIBUTE_COMPRESSED = 0x00000800,
  161. FILE_ATTRIBUTE_OFFLINE = 0x00001000,
  162. FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 0x00002000,
  163. FILE_ATTRIBUTE_ENCRYPTED = 0x00004000,
  164. FILE_FLAG_WRITE_THROUGH = 0x80000000,
  165. FILE_FLAG_OVERLAPPED = 0x40000000,
  166. FILE_FLAG_NO_BUFFERING = 0x20000000,
  167. FILE_FLAG_RANDOM_ACCESS = 0x10000000,
  168. FILE_FLAG_SEQUENTIAL_SCAN = 0x08000000,
  169. FILE_FLAG_DELETE_ON_CLOSE = 0x04000000,
  170. FILE_FLAG_BACKUP_SEMANTICS = 0x02000000,
  171. FILE_FLAG_POSIX_SEMANTICS = 0x01000000,
  172. FILE_FLAG_OPEN_REPARSE_POINT = 0x00200000,
  173. FILE_FLAG_OPEN_NO_RECALL = 0x00100000,
  174. FILE_FLAG_FIRST_PIPE_INSTANCE = 0x00080000,
  175. }
  176. #endregion
  177. }