C#USB设备枚举Kernel32的PInvoke
- using System;
- using System.Runtime.InteropServices;
- using System.Security;
- namespace Splash
- {
- #region Kernel32
- [SuppressUnmanagedCodeSecurity]
- internal static class Kernel32
- {
- /// <summary>
- /// 无效的文件句柄
- /// </summary>
- public static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
- #region CreateFile
- [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
- public static extern IntPtr CreateFile(
- String fileName,
- [MarshalAs(UnmanagedType.U4)] NativeFileAccess fileAccess,
- [MarshalAs(UnmanagedType.U4)] NativeFileShare fileShare,
- IntPtr securityAttributes,
- [MarshalAs(UnmanagedType.U4)] NativeFileMode creationDisposition,
- NativeFileFlag flags,
- IntPtr template
- );
- [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
- public static extern IntPtr CreateFile(
- String fileName,
- [MarshalAs(UnmanagedType.U4)] NativeFileAccess fileAccess,
- [MarshalAs(UnmanagedType.U4)] NativeFileShare fileShare,
- IntPtr securityAttributes,
- [MarshalAs(UnmanagedType.U4)] NativeFileMode creationDisposition,
- IntPtr flags,
- IntPtr template
- );
- #endregion
- [DllImport("kernel32.dll", SetLastError = true)]
- [return: MarshalAs(UnmanagedType.Bool)]
- public static extern Boolean CloseHandle(IntPtr hFile);
- #region ReadFile
- [DllImport("kernel32.dll", SetLastError = true)]
- public static extern Boolean ReadFile(
- IntPtr hFile,
- [Out] Byte[] lpBuffer,
- Int32 nNumberOfBytesToRead,
- out Int32 lpNumberOfBytesRead,
- [In] ref System.Threading.NativeOverlapped lpOverlapped
- );
- [DllImport("kernel32.dll", SetLastError = true)]
- public static extern Boolean ReadFile(
- IntPtr hFile,
- [Out] Byte[] lpBuffer,
- Int32 nNumberOfBytesToRead,
- IntPtr lpNumberOfBytesRead,
- [In] ref System.Threading.NativeOverlapped lpOverlapped
- );
- [DllImport("kernel32.dll", SetLastError = true)]
- public static extern Boolean ReadFile(
- IntPtr hFile,
- [Out] Byte[] lpBuffer,
- Int32 nNumberOfBytesToRead,
- out Int32 lpNumberOfBytesRead,
- IntPtr lpOverlapped
- );
- #endregion
- #region WriteFile
- [DllImport("kernel32.dll")]
- public static extern Boolean WriteFile(
- IntPtr hFile,
- Byte[] lpBuffer,
- Int32 nNumberOfBytesToWrite,
- out Int32 lpNumberOfBytesWritten,
- [In] ref System.Threading.NativeOverlapped lpOverlapped
- );
- [DllImport("kernel32.dll")]
- public static extern Boolean WriteFile(
- IntPtr hFile,
- Byte[] lpBuffer,
- Int32 nNumberOfBytesToWrite,
- IntPtr lpNumberOfBytesWritten,
- [In] ref System.Threading.NativeOverlapped lpOverlapped
- );
- [DllImport("kernel32.dll")]
- public static extern Boolean WriteFile(
- IntPtr hFile,
- Byte[] lpBuffer,
- Int32 nNumberOfBytesToWrite,
- IntPtr lpNumberOfBytesWritten,
- IntPtr lpOverlapped
- );
- #endregion
- [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
- public static extern Boolean GetOverlappedResult(
- IntPtr hFile,
- [In] ref System.Threading.NativeOverlapped lpOverlapped,
- out Int32 lpNumberOfBytesTransferred,
- Boolean bWait
- );
- }
- #endregion
- #region ENUM
- [Flags]
- internal enum NativeFileAccess : uint
- {
- GENERIC_READ = (0x80000000),
- GENERIC_WRITE = (0x40000000),
- GENERIC_EXECUTE = (0x20000000),
- GENERIC_ALL = (0x10000000),
- FILE_SPECIAL = 0,
- FILE_APPEND_DATA = (0x0004), // 文件
- FILE_READ_DATA = (0x0001), // 文件和管道
- FILE_WRITE_DATA = (0x0002), // 文件和管道
- FILE_READ_EA = (0x0008), // 文件和目录
- FILE_WRITE_EA = (0x0010), // 文件和目录
- FILE_READ_ATTRIBUTES = (0x0080), // 所有
- FILE_WRITE_ATTRIBUTES = (0x0100), // 所有
- DELETE = 0x00010000,
- READ_CONTROL = (0x00020000),
- WRITE_DAC = (0x00040000),
- WRITE_OWNER = (0x00080000),
- SYNCHRONIZE = (0x00100000),
- STANDARD_RIGHTS_REQUIRED = (0x000F0000),
- STANDARD_RIGHTS_READ = (READ_CONTROL),
- STANDARD_RIGHTS_WRITE = (READ_CONTROL),
- STANDARD_RIGHTS_EXECUTE = (READ_CONTROL),
- STANDARD_RIGHTS_ALL = (0x001F0000),
- SPECIFIC_RIGHTS_ALL = (0x0000FFFF),
- FILE_GENERIC_READ = (STANDARD_RIGHTS_READ | FILE_READ_DATA | FILE_READ_ATTRIBUTES | FILE_READ_EA | SYNCHRONIZE),
- FILE_GENERIC_WRITE = (STANDARD_RIGHTS_WRITE | FILE_WRITE_DATA | FILE_WRITE_ATTRIBUTES | FILE_WRITE_EA | FILE_APPEND_DATA | SYNCHRONIZE),
- SPECIAL = 0
- }
- internal enum NativeFileMode : uint
- {
- CREATE_NEW = 1,
- CREATE_ALWAYS = 2,
- OPEN_EXISTING = 3,
- OPEN_ALWAYS = 4,
- TRUNCATE_EXISTING = 5,
- }
- [Flags]
- internal enum NativeFileShare : uint
- {
- NONE = 0,
- FILE_SHARE_READ = 0x00000001,
- FILE_SHARE_WRITE = 0x00000002,
- FILE_SHARE_DEELETE = 0x00000004,
- }
- [Flags]
- internal enum NativeFileFlag : uint
- {
- FILE_ATTRIBUTE_READONLY = 0x00000001,
- FILE_ATTRIBUTE_HIDDEN = 0x00000002,
- FILE_ATTRIBUTE_SYSTEM = 0x00000004,
- FILE_ATTRIBUTE_DIRECTORY = 0x00000010,
- FILE_ATTRIBUTE_ARCHIVE = 0x00000020,
- FILE_ATTRIBUTE_DEVICE = 0x00000040,
- FILE_ATTRIBUTE_NORMAL = 0x00000080,
- FILE_ATTRIBUTE_TEMPORARY = 0x00000100,
- FILE_ATTRIBUTE_SPARSE_FILE = 0x00000200,
- FILE_ATTRIBUTE_REPARSE_POINT = 0x00000400,
- FILE_ATTRIBUTE_COMPRESSED = 0x00000800,
- FILE_ATTRIBUTE_OFFLINE = 0x00001000,
- FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 0x00002000,
- FILE_ATTRIBUTE_ENCRYPTED = 0x00004000,
- FILE_FLAG_WRITE_THROUGH = 0x80000000,
- FILE_FLAG_OVERLAPPED = 0x40000000,
- FILE_FLAG_NO_BUFFERING = 0x20000000,
- FILE_FLAG_RANDOM_ACCESS = 0x10000000,
- FILE_FLAG_SEQUENTIAL_SCAN = 0x08000000,
- FILE_FLAG_DELETE_ON_CLOSE = 0x04000000,
- FILE_FLAG_BACKUP_SEMANTICS = 0x02000000,
- FILE_FLAG_POSIX_SEMANTICS = 0x01000000,
- FILE_FLAG_OPEN_REPARSE_POINT = 0x00200000,
- FILE_FLAG_OPEN_NO_RECALL = 0x00100000,
- FILE_FLAG_FIRST_PIPE_INSTANCE = 0x00080000,
- }
- #endregion
- }
C#USB设备枚举Kernel32的PInvoke的更多相关文章
- C#:USB设备枚举 --转自CSDN作者:Splash
(一)DeviceIoControl的PInvoke /* ---------------------------------------------------------- 文件名称:Device ...
- usb设备枚举过程
USB主机在检测到USB设备插入后,就要对设备进行枚举了.为什么要枚举呢?枚举就是从设备读取一些信息,知道设备是什么样的设备,如何进行通信,这样主机就可以根据这些信息来加载合适的驱动程序.调试USB设 ...
- USB设备驱动之设备初始化(设备枚举)
USB设备从接入HUB到正常工作之前.都属于设备枚举阶段.所谓设备枚举.就是让host控制器认识USB设备,并为其准备资源.建立好主机与设备间的数据传递机制. 该阶段的工作,是USB通信协议规定的,所 ...
- Linux下usb设备驱动详解
USB驱动分为两块,一块是USB的bus驱动,这个东西,Linux内核已经做好了,我们可以不管,我们只需要了解它的功能.形象的说,USB的bus驱动相当于铺出一条路来,让所有的信息都可以通过这条USB ...
- USB 设备驱动(写给自己看的)
集线器与控制器(USB地址7bit) 设备,配置,端点,接口 USB1.0(低速1.2),1.1(全速450m),2.0(高速,电流传输)区别 引脚4根(V,D-,D+,gnd),miniUSB增加 ...
- Windows下USB磁盘开发系列二:枚举系统中所有USB设备
上篇 <Windows下USB磁盘开发系列一:枚举系统中U盘的盘符>介绍了很简单的获取系统U盘盘符的办法,现在介绍下如何枚举系统中所有USB设备(不光是U盘). 主要调用的API如下: 1 ...
- USB协议-USB设备的枚举过程
USB主机在检测到USB设备插入后,就要对设备进行枚举了.为什么要枚举?枚举就是从设备读取各种描述符信息,这样主机就可以根据这些信息来加载合适的驱动程序,从而知道设备是什么样的设备,如何进行通信等. ...
- MFC枚举USB设备碰到的一个疑难,还没解决
代码如下: 打开USB Hub设备之后,返回句柄hHubDevice,然后使用EnumerateHubPorts来枚举Hub的端 口.疑问在代码的中文注释中. bool CUsbEnumHub::En ...
- 浅析USB之设备枚举
当一个USB设备插入主机后,会有以下活动: 配上状态图
随机推荐
- 解决Delphi 2010启动时卡死并报“displayNotification: 堆栈溢出”错误
1. 清理IE的历史记录,删除浏览器缓存(不需要清cookie) 2. 禁用startpage 2.1 从 Delphi 2010 启动菜单上点右键 -> 查看属性->快捷方式->目 ...
- 关于Tomcat配置虚拟路径保存、访问图片
在项目中往往需要上传一些图片文件之类,一般不建议直接保存在数据库内,往往是讲图片等资源保存在服务器的某个文件夹下,传统做法是上传到部署目录下,通过相对路径进行访问. 这样当我们系统需要进行升级,进行全 ...
- VMWare VSphere6.0的实验笔记
在现有的一个vsphere6.0虚拟平台上环境下搭建一套VSphere环境平台. 任务1: 1.建立1个win2008主机,192.168.12.10.16Gram,40G硬盘1独立存储+150G硬盘 ...
- 使用jquery.mCustomScrollbar自定义滚动条(3)callback onCreate
碰到了一个问题,想简洁,所以在页面上使用 <div class="div_box mCustomScrollbar" data-mcs-theme="dark-3& ...
- mysql监测工具
可视性是系统设计的最佳境界,MySQL 也不例外.一旦完成了 MySQL 环境的搭建.运行并调优,您千万不要认为已经万事大吉了. 数据库环境既会受到来自系统更改或流量负荷的影响,也会遇到例如流量高峰. ...
- Python——字符串2.0(实验)(python programming)
直接打s,是程序员看到的:打print(),是用户看到的 列表 ] #列表索引,与数组唯一不同:等号左端可修改 转载自:https://www.cnblogs.com/wwwwwei/p/104816 ...
- docker下centos安装ping命令
https://blog.csdn.net/king_gun/article/details/78423115 [问题] 从docker hub上拉取到则镜像centos:6.7在执行ping命令是报 ...
- [UE4]条件融合动画: Blend Posed by int
Aim Group=0:使用动画“Blend Pose 0” Aim Group=1:使用动画“Blend Pose 1”
- sklearn获得某个参数的不同取值在训练集和测试集上的表现的曲线刻画
from sklearn.svm import SVC from sklearn.datasets import make_classification import numpy as np X,y ...
- Eclipse中Activiti插件的安装
要想使用Activiti流程引擎,需要在Eclipse安装Activiti插件,才能画流程设计图. 打开Eclipse,点击help -> Install new Software 然后点击 A ...