首页 > 学院 > 开发设计 > 正文

C#USB设备枚举Kernel32的PInvoke

2019-11-17 02:23:35
字体:
来源:转载
供稿:网友

C#USB设备枚举Kernel32的PInvoke

Posted on 2015-05-13 15:09 haishu 阅读(...) 评论(...) 编辑 收藏
  1. usingSystem;
  2. usingSystem.Runtime.InteropServices;
  3. usingSystem.Security;
  4. namespaceSplash
  5. {
  6. #regionKernel32
  7. [SupPRessUnmanagedCodeSecurity]
  8. internalstaticclassKernel32
  9. {
  10. ///<summary>
  11. ///无效的文件句柄
  12. ///</summary>
  13. publicstaticreadonlyIntPtrINVALID_HANDLE_VALUE=newIntPtr(-1);
  14. #regionCreateFile
  15. [DllImport("kernel32.dll",CharSet=CharSet.Auto,SetLastError=true)]
  16. publicstaticexternIntPtrCreateFile(
  17. StringfileName,
  18. [MarshalAs(UnmanagedType.U4)]NativeFileaccessfileAccess,
  19. [MarshalAs(UnmanagedType.U4)]NativeFileSharefileShare,
  20. IntPtrsecurityAttributes,
  21. [MarshalAs(UnmanagedType.U4)]NativeFileModecreationDisposition,
  22. NativeFileFlagflags,
  23. IntPtrtemplate
  24. );
  25. [DllImport("kernel32.dll",CharSet=CharSet.Auto,SetLastError=true)]
  26. publicstaticexternIntPtrCreateFile(
  27. StringfileName,
  28. [MarshalAs(UnmanagedType.U4)]NativeFileAccessfileAccess,
  29. [MarshalAs(UnmanagedType.U4)]NativeFileSharefileShare,
  30. IntPtrsecurityAttributes,
  31. [MarshalAs(UnmanagedType.U4)]NativeFileModecreationDisposition,
  32. IntPtrflags,
  33. IntPtrtemplate
  34. );
  35. #endregion
  36. [DllImport("kernel32.dll",SetLastError=true)]
  37. [return:MarshalAs(UnmanagedType.Bool)]
  38. publicstaticexternBooleanCloseHandle(IntPtrhFile);
  39. #regionReadFile
  40. [DllImport("kernel32.dll",SetLastError=true)]
  41. publicstaticexternBooleanReadFile(
  42. IntPtrhFile,
  43. [Out]Byte[]lpBuffer,
  44. Int32nNumberOfBytesToRead,
  45. outInt32lpNumberOfBytesRead,
  46. [In]refSystem.Threading.NativeOverlappedlpOverlapped
  47. );
  48. [DllImport("kernel32.dll",SetLastError=true)]
  49. publicstaticexternBooleanReadFile(
  50. IntPtrhFile,
  51. [Out]Byte[]lpBuffer,
  52. Int32nNumberOfBytesToRead,
  53. IntPtrlpNumberOfBytesRead,
  54. [In]refSystem.Threading.NativeOverlappedlpOverlapped
  55. );
  56. [DllImport("kernel32.dll",SetLastError=true)]
  57. publicstaticexternBooleanReadFile(
  58. IntPtrhFile,
  59. [Out]Byte[]lpBuffer,
  60. Int32nNumberOfBytesToRead,
  61. outInt32lpNumberOfBytesRead,
  62. IntPtrlpOverlapped
  63. );
  64. #endregion
  65. #regionWriteFile
  66. [DllImport("kernel32.dll")]
  67. publicstaticexternBooleanWriteFile(
  68. IntPtrhFile,
  69. Byte[]lpBuffer,
  70. Int32nNumberOfBytesToWrite,
  71. outInt32lpNumberOfBytesWritten,
  72. [In]refSystem.Threading.NativeOverlappedlpOverlapped
  73. );
  74. [DllImport("kernel32.dll")]
  75. publicstaticexternBooleanWriteFile(
  76. IntPtrhFile,
  77. Byte[]lpBuffer,
  78. Int32nNumberOfBytesToWrite,
  79. IntPtrlpNumberOfBytesWritten,
  80. [In]refSystem.Threading.NativeOverlappedlpOverlapped
  81. );
  82. [DllImport("kernel32.dll")]
  83. publicstaticexternBooleanWriteFile(
  84. IntPtrhFile,
  85. Byte[]lpBuffer,
  86. Int32nNumberOfBytesToWrite,
  87. IntPtrlpNumberOfBytesWritten,
  88. IntPtrlpOverlapped
  89. );
  90. #endregion
  91. [DllImport("kernel32.dll",CharSet=CharSet.Auto,SetLastError=true)]
  92. publicstaticexternBooleanGetOverlappedResult(
  93. IntPtrhFile,
  94. [In]refSystem.Threading.NativeOverlappedlpOverlapped,
  95. outInt32lpNumberOfBytesTransferred,
  96. BooleanbWait
  97. );
  98. }
  99. #endregion
  100. #regionENUM
  101. [Flags]
  102. internalenumNativeFileAccess: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. internalenumNativeFileMode: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. internalenumNativeFileShare:uint
  141. {
  142. NONE=0,
  143. FILE_SHARE_READ=0x00000001,
  144. FILE_SHARE_WRITE=0x00000002,
  145. FILE_SHARE_DEELETE=0x00000004,
  146. }
  147. [Flags]
  148. internalenumNativeFileFlag: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. }

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表