C#:USB设备枚举(一)DeviceIoControl的PInvoke

215 篇文章 2 订阅
120 篇文章 3 订阅

作者:Splash

转自:http://blog.csdn.net/jhqin/article/details/6918152


[csharp] view plain copy
  1. /* ---------------------------------------------------------- 
  2. 文件名称:DeviceIoControl.cs 
  3.  
  4. 作者:秦建辉 
  5.  
  6. MSN:splashcn@msn.com 
  7. QQ:36748897 
  8.  
  9. 博客:http://blog.csdn.net/jhqin 
  10.  
  11. 开发环境: 
  12.     Visual Studio V2010 
  13.     .NET Framework 4 Client Profile 
  14.  
  15. 版本历史: 
  16.     V1.0    2011年10月10日 
  17.             实现对DeviceIoControl接口的PInvoke 
  18.  
  19. 参考资料: 
  20.     http://www.pinvoke.net/ 
  21. ------------------------------------------------------------ */  
  22. using System;  
  23. using System.Runtime.InteropServices;  
  24.   
  25. namespace Splash.IO.PORTS  
  26. {  
  27.     #region ENUM  
  28.     public enum USB_HUB_NODE : uint  
  29.     {  
  30.         UsbHub,  
  31.         UsbMIParent  
  32.     }  
  33.   
  34.     public enum USB_CONNECTION_STATUS  
  35.     {  
  36.         NoDeviceConnected,  
  37.         DeviceConnected,  
  38.         DeviceFailedEnumeration,  
  39.         DeviceGeneralFailure,  
  40.         DeviceCausedOvercurrent,  
  41.         DeviceNotEnoughPower,  
  42.         DeviceNotEnoughBandwidth,  
  43.         DeviceHubNestedTooDeeply,  
  44.         DeviceInLegacyHub  
  45.     }  
  46.   
  47.     public enum USB_DEVICE_SPEED : byte  
  48.     {  
  49.         UsbLowSpeed,    // 低速USB 1.1  
  50.         UsbFullSpeed,   // 全速USB 1.1  
  51.         UsbHighSpeed,   // 高速USB 2.0  
  52.         UsbSuperSpeed   // 极速USB 3.0  
  53.     }  
  54.     #endregion  
  55.   
  56.     public partial class USB  
  57.     {  
  58.         internal const Int32 IOCTL_GET_HCD_DRIVERKEY_NAME = 0x220424;  
  59.         internal const Int32 IOCTL_USB_GET_ROOT_HUB_NAME = 0x220408;  
  60.         internal const Int32 IOCTL_USB_GET_NODE_CONNECTION_NAME = 0x220414;  
  61.         internal const Int32 IOCTL_USB_GET_NODE_INFORMATION = 0x220408;  
  62.         internal const Int32 IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX = 0x220448;  
  63.         internal const Int32 IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION = 0x220410;  
  64.   
  65.         internal const Int32 MAXIMUM_USB_STRING_LENGTH = 255;  
  66.         internal const Int32 USB_STRING_DESCRIPTOR_TYPE = 3;  
  67.   
  68.         [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]  
  69.         internal struct USB_HCD_DRIVERKEY_NAME  
  70.         {  
  71.             public Int32 ActualLength;  
  72.             [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]  
  73.             public String Name;  
  74.         }  
  75.  
  76.         #region USB_NODE_INFORMATION  
  77.         [StructLayout(LayoutKind.Sequential, Pack = 1)]  
  78.         internal struct USB_HUB_DESCRIPTOR  
  79.         {  
  80.             public Byte bDescriptorLength;  
  81.             public Byte bDescriptorType;    // 描述符类型:0x29  
  82.             public Byte bNumberOfPorts;     // 支持的下游端口数目  
  83.             public Int16 wHubCharacteristics;   // 特征描述  
  84.             public Byte bPowerOnToPowerGood;    // 从端口加电到端口正常工作的时间间隔(以2ms为单位)  
  85.             public Byte bHubControlCurrent;     // 设备所需最大电流  
  86.             [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]  
  87.             public Byte[] bRemoveAndPowerMask;  // 指示连接在集线器端口的设备是否可移走  
  88.         }  
  89.   
  90.         [StructLayout(LayoutKind.Sequential)]  
  91.         internal struct USB_HUB_INFORMATION  
  92.         {  
  93.             public USB_HUB_DESCRIPTOR HubDescriptor;  
  94.             public Byte HubIsBusPowered;  
  95.         }  
  96.   
  97.         [StructLayout(LayoutKind.Sequential)]  
  98.         internal struct USB_MI_PARENT_INFORMATION  
  99.         {  
  100.             public Int32 NumberOfInterfaces;  
  101.         };  
  102.           
  103.         [StructLayout(LayoutKind.Explicit)]  
  104.         internal struct UsbNodeUnion  
  105.         {  
  106.             [FieldOffset(0)]  
  107.             public USB_HUB_INFORMATION HubInformation;  
  108.             [FieldOffset(0)]  
  109.             public USB_MI_PARENT_INFORMATION MiParentInformation;  
  110.         }  
  111.   
  112.         [StructLayout(LayoutKind.Sequential)]  
  113.         internal struct USB_NODE_INFORMATION  
  114.         {  
  115.             public USB_HUB_NODE NodeType;  
  116.             public UsbNodeUnion u;  
  117.         }  
  118.         #endregion  
  119.  
  120.         #region USB_NODE_CONNECTION_INFORMATION  
  121.         [StructLayout(LayoutKind.Sequential, Pack = 1)]  
  122.         internal struct USB_DEVICE_DESCRIPTOR  
  123.         {  
  124.             public Byte bLength;  
  125.             public Byte bDescriptorType;  
  126.             public UInt16 bcdUSB;  
  127.             public Byte bDeviceClass;  
  128.             public Byte bDeviceSubClass;  
  129.             public Byte bDeviceProtocol;  
  130.             public Byte bMaxPacketSize0;  
  131.             public UInt16 idVendor;  
  132.             public UInt16 idProduct;  
  133.             public UInt16 bcdDevice;  
  134.             public Byte iManufacturer;  
  135.             public Byte iProduct;  
  136.             public Byte iSerialNumber;  
  137.             public Byte bNumConfigurations;  
  138.         }         
  139.   
  140.         [StructLayout(LayoutKind.Sequential, Pack = 1)]  
  141.         internal struct USB_ENDPOINT_DESCRIPTOR  
  142.         {  
  143.             public Byte bLength;  
  144.             public Byte bDescriptorType;  
  145.             public Byte bEndpointAddress;  
  146.             public Byte bmAttributes;  
  147.             public UInt16 wMaxPacketSize;  
  148.             public Byte bInterval;  
  149.         }  
  150.   
  151.         [StructLayout(LayoutKind.Sequential, Pack = 1)]  
  152.         internal struct USB_PIPE_INFO  
  153.         {  
  154.             public USB_ENDPOINT_DESCRIPTOR EndpointDescriptor;  
  155.             public UInt32 ScheduleOffset;  
  156.         }  
  157.   
  158.         [StructLayout(LayoutKind.Sequential, Pack = 1)]  
  159.         internal struct USB_NODE_CONNECTION_INFORMATION_EX  
  160.         {  
  161.             public Int32 ConnectionIndex;  
  162.             public USB_DEVICE_DESCRIPTOR DeviceDescriptor;  
  163.             public Byte CurrentConfigurationValue;  
  164.             public Byte Speed;  
  165.             public Byte DeviceIsHub;  
  166.             public Int16 DeviceAddress;  
  167.             public Int32 NumberOfOpenPipes;  
  168.             public USB_CONNECTION_STATUS ConnectionStatus;  
  169.             [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]  
  170.             public USB_PIPE_INFO[] PipeList;  
  171.         }  
  172.         #endregion  
  173.  
  174.         #region USB_DESCRIPTOR_REQUEST  
  175.         [StructLayout(LayoutKind.Sequential)]  
  176.         internal struct USB_SETUP_PACKET  
  177.         {  
  178.             public Byte bmRequest;  
  179.             public Byte bRequest;  
  180.             public UInt16 wValue;  
  181.             public UInt16 wIndex;  
  182.             public UInt16 wLength;  
  183.         }  
  184.   
  185.         [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]  
  186.         internal struct USB_STRING_DESCRIPTOR  
  187.         {  
  188.             public Byte bLength;  
  189.             public Byte bDescriptorType;  
  190.             [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAXIMUM_USB_STRING_LENGTH)]  
  191.             public String bString;  
  192.         }  
  193.   
  194.         [StructLayout(LayoutKind.Sequential)]  
  195.         internal struct USB_DESCRIPTOR_REQUEST  
  196.         {  
  197.             public Int32 ConnectionIndex;  
  198.             public USB_SETUP_PACKET SetupPacket;         
  199.             public USB_STRING_DESCRIPTOR Data;  
  200.         }  
  201.         #endregion  
  202.  
  203.         #region USB_NODE_CONNECTION_DRIVERKEY_NAME  
  204.         [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]  
  205.         internal struct USB_NODE_CONNECTION_DRIVERKEY_NAME  
  206.         {  
  207.             public Int32 ConnectionIndex;  
  208.             public Int32 ActualLength;  
  209.             [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAXIMUM_USB_STRING_LENGTH)]  
  210.             public String DriverKeyName;  
  211.         }  
  212.         #endregion  
  213.  
  214.         #region DeviceIoControl  
  215.         [DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true)]  
  216.         internal static extern Boolean DeviceIoControl(  
  217.             IntPtr hFile,  
  218.             Int32 dwIoControlCode,  
  219.             IntPtr lpInBuffer,  
  220.             Int32 nInBufferSize,  
  221.             ref USB_HCD_DRIVERKEY_NAME lpOutBuffer,  
  222.             Int32 nOutBufferSize,  
  223.             out Int32 nBytesReturned,  
  224.             IntPtr lpOverlapped  
  225.             );  
  226.   
  227.         [DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true)]  
  228.         internal static extern Boolean DeviceIoControl(  
  229.             IntPtr hFile,  
  230.             Int32 dwIoControlCode,  
  231.             ref USB_NODE_INFORMATION lpInBuffer,  
  232.             Int32 nInBufferSize,  
  233.             ref USB_NODE_INFORMATION lpOutBuffer,  
  234.             Int32 nOutBufferSize,  
  235.             out Int32 lpBytesReturned,  
  236.             IntPtr lpOverlapped  
  237.             );  
  238.   
  239.         [DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true)]  
  240.         internal static extern Boolean DeviceIoControl(  
  241.             IntPtr hFile,  
  242.             Int32 dwIoControlCode,  
  243.             ref USB_NODE_CONNECTION_INFORMATION_EX lpInBuffer,  
  244.             Int32 nInBufferSize,  
  245.             ref USB_NODE_CONNECTION_INFORMATION_EX lpOutBuffer,  
  246.             Int32 nOutBufferSize,  
  247.             out Int32 lpBytesReturned,  
  248.             IntPtr lpOverlapped  
  249.             );  
  250.   
  251.         [DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true)]  
  252.         internal static extern Boolean DeviceIoControl(  
  253.             IntPtr hFile,  
  254.             Int32 dwIoControlCode,  
  255.             ref USB_DESCRIPTOR_REQUEST lpInBuffer,  
  256.             Int32 nInBufferSize,  
  257.             ref USB_DESCRIPTOR_REQUEST lpOutBuffer,  
  258.             Int32 nOutBufferSize,  
  259.             out Int32 lpBytesReturned,  
  260.             IntPtr lpOverlapped  
  261.             );  
  262.   
  263.         [DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true)]  
  264.         internal static extern Boolean DeviceIoControl(  
  265.             IntPtr hFile,  
  266.             Int32 dwIoControlCode,  
  267.             ref USB_NODE_CONNECTION_DRIVERKEY_NAME lpInBuffer,  
  268.             Int32 nInBufferSize,  
  269.             ref USB_NODE_CONNECTION_DRIVERKEY_NAME lpOutBuffer,  
  270.             Int32 nOutBufferSize,  
  271.             out Int32 lpBytesReturned,  
  272.             IntPtr lpOverlapped  
  273.             );  
  274.         #endregion  
  275.     }  
  276. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值