C#:USB设备枚举(二)设备枚举API

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

作者:Splash

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


[csharp] view plain copy
  1. /* ---------------------------------------------------------- 
  2. 文件名称:WDKUsbEnum.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.             基于WDK枚举USB设备 
  18. ------------------------------------------------------------ */  
  19. using System;  
  20. using System.Collections.Generic;  
  21. using System.Management;  
  22. using System.Runtime.InteropServices;  
  23. using System.Text;  
  24.   
  25. namespace Splash.IO.PORTS  
  26. {  
  27.     /// <summary>  
  28.     /// USB主控制器信息  
  29.     /// </summary>  
  30.     public struct HostControllerInfo  
  31.     {  
  32.         public String PNPDeviceID;      // 设备ID  
  33.         public String Name;             // 设备名称  
  34.     }  
  35.   
  36.     /// <summary>  
  37.     /// USB Hub信息  
  38.     /// </summary>  
  39.     public struct UsbHubInfo  
  40.     {  
  41.         public String PNPDeviceID;      // 设备ID  
  42.         public String Name;             // 设备名称  
  43.         public String Status;           // 设备状态  
  44.     }  
  45.   
  46.     /// <summary>  
  47.     /// USB HUB节点信息  
  48.     /// </summary>  
  49.     public struct UsbNodeInformation  
  50.     {  
  51.         public String DevicePath;           // 设备路径  
  52.         public String PNPDeviceID;          // 设备ID          
  53.         public String Name;                 // 设备名称  
  54.   
  55.         public USB_HUB_NODE NodeType;       // 节点类型  
  56.   
  57.         // USB_HUB_INFORMATION  
  58.         public Boolean HubIsBusPowered;     // 供电方式:true-总线供电 false-独立供电  
  59.         public Int32 NumberOfPorts;         // 端口数  
  60.         public Int16 HubCharacteristics;    // 特征描述  
  61.         public Byte PowerOnToPowerGood;     // 从端口加电到端口正常工作的时间间隔(以2ms为单位)  
  62.         public Byte HubControlCurrent;      // 设备所需最大电流  
  63.   
  64.         // USB_MI_PARENT_INFORMATION  
  65.         public Int32 NumberOfInterfaces;    // 接口数  
  66.     }  
  67.   
  68.     /// <summary>  
  69.     /// USB设备描述符  
  70.     /// </summary>  
  71.     public struct UsbDeviceDescriptor  
  72.     {  
  73.         public Byte bDescriptorType;    // 描述符类型 USB_DEVICE_DESCRIPTOR_TYPE  
  74.         public String UsbVersion;       // USB规格版本号  
  75.         public Byte bDeviceClass;       // 设备类型  
  76.         public Byte bDeviceSubClass;    // 设备子类型  
  77.         public Byte bDeviceProtocol;    // 设备协议  
  78.         public Byte bMaxPacketSize0;    // 最大封包大小  
  79.         public UInt16 idVendor;         // VID  
  80.         public UInt16 idProduct;        // PID  
  81.         public String DeviceVersion;    // 设备版本号  
  82.         public String Manufacturer;     // 制造商  
  83.         public String Product;          // 产品描述  
  84.         public String SerialNumber;     // 序列号  
  85.         public Byte bNumConfigurations; // 配置总数  
  86.     }  
  87.   
  88.     /// <summary>  
  89.     /// USB管道信息  
  90.     /// </summary>  
  91.     public struct UsbPipeInfo  
  92.     {  
  93.         public UInt32 ScheduleOffset;  
  94.         public Byte bDescriptorType;  
  95.         public Byte bEndpointAddress;  
  96.         public Byte bmAttributes;  
  97.         public UInt16 wMaxPacketSize;  
  98.         public Byte bInterval;  
  99.     }  
  100.   
  101.     /// <summary>  
  102.     /// USB节点连接信息  
  103.     /// </summary>  
  104.     public struct UsbNodeConnectionInformation  
  105.     {  
  106.         public String DevicePath;           // 设备路径  
  107.         public Int32 ConnectionIndex;       // 端口号  
  108.   
  109.         public UsbDeviceDescriptor DeviceDescriptor;  
  110.   
  111.         public Byte CurrentConfigurationValue;  // 当前设备配置  
  112.         public Byte Speed;                  // 设备速度  
  113.         public Boolean DeviceIsHub;         // 是否是集线器          
  114.         public Int32 DeviceAddress;         // 设备地址  
  115.         public Int32 NumberOfOpenPipes;     // 管道数  
  116.         public USB_CONNECTION_STATUS ConnectionStatus;  // 连接状态  
  117.   
  118.         public List<UsbPipeInfo> PipeList;  // 管道信息  
  119.     }  
  120.       
  121.     /// <summary>  
  122.     /// USB设备枚举  
  123.     /// </summary>  
  124.     public partial class USB  
  125.     {  
  126.         #region HostController  
  127.         /// <summary>  
  128.         /// USB主控制器  
  129.         /// </summary>  
  130.         public static HostControllerInfo[] AllHostControllers  
  131.         {  
  132.             get  
  133.             {  
  134.                 List<HostControllerInfo> HostControllers = new List<HostControllerInfo>();  
  135.   
  136.                 // 获取USB控制器及其相关联的设备实体  
  137.                 ManagementObjectCollection MOC = new ManagementObjectSearcher("SELECT * FROM Win32_USBController").Get();  
  138.                 if (MOC != null)  
  139.                 {  
  140.                     foreach (ManagementObject MO in MOC)  
  141.                     {  
  142.                         HostControllerInfo Element;  
  143.                         Element.PNPDeviceID = MO["PNPDeviceID"as String;  // 设备ID  
  144.                         Element.Name = MO["Name"as String;    // 设备描述                          
  145.                         HostControllers.Add(Element);  
  146.                     }  
  147.                 }  
  148.   
  149.                 if (HostControllers.Count == 0) return nullelse return HostControllers.ToArray();  
  150.             }  
  151.         }  
  152.           
  153.         /// <summary>  
  154.         /// 获取驱动键名  
  155.         /// </summary>  
  156.         /// <param name="PNPDeviceID">USB主控制器设备ID</param>  
  157.         /// <returns>获取设备驱动在注册表HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class下的路径</returns>  
  158.         public static String GetHcdDriverKeyName(String PNPDeviceID)  
  159.         {  
  160.             if (String.IsNullOrEmpty(PNPDeviceID)) return null;  
  161.               
  162.             // 打开设备  
  163.             IntPtr hHCDev = Kernel32.CreateFile(  
  164.                 "\\\\.\\" + PNPDeviceID.Replace('\\', '#') + "#{3ABF6F2D-71C4-462A-8A92-1E6861E6AF27}",  
  165.                 NativeFileAccess.GENERIC_WRITE,  
  166.                 NativeFileShare.FILE_SHARE_WRITE,  
  167.                 IntPtr.Zero,  
  168.                 NativeFileMode.OPEN_EXISTING,  
  169.                 IntPtr.Zero,  
  170.                 IntPtr.Zero);  
  171.             if (hHCDev == Kernel32.INVALID_HANDLE_VALUE) return null;  
  172.   
  173.             // 获取驱动键名  
  174.             Int32 nBytesReturned;  
  175.             USB_HCD_DRIVERKEY_NAME Buffer = new USB_HCD_DRIVERKEY_NAME();  
  176.             Boolean Status = DeviceIoControl(hHCDev,  
  177.                 IOCTL_GET_HCD_DRIVERKEY_NAME,  
  178.                 IntPtr.Zero,  
  179.                 0,  
  180.                 ref Buffer,  
  181.                 Marshal.SizeOf(Buffer),  
  182.                 out nBytesReturned,  
  183.                 IntPtr.Zero);              
  184.   
  185.             // 关闭设备  
  186.             Kernel32.CloseHandle(hHCDev);  
  187.             return Status ? Buffer.Name : null;  
  188.         }        
  189.         #endregion  
  190.  
  191.         #region USBHUB  
  192.         /// <summary>  
  193.         /// USB Hub信息集合  
  194.         /// </summary>  
  195.         public static UsbHubInfo[] AllUsbHubs  
  196.         {  
  197.             get  
  198.             {  
  199.                 List<UsbHubInfo> UsbHubs = new List<UsbHubInfo>();  
  200.   
  201.                 // 获取USB控制器及其相关联的设备实体  
  202.                 ManagementObjectCollection MOC = new ManagementObjectSearcher("SELECT * FROM Win32_USBHub").Get();  
  203.                 if (MOC != null)  
  204.                 {  
  205.                     foreach (ManagementObject MO in MOC)  
  206.                     {  
  207.                         UsbHubInfo Element;  
  208.                         Element.PNPDeviceID = MO["PNPDeviceID"as String;  // 设备ID  
  209.                         Element.Name = MO["Name"as String;        // 设备描述      
  210.                         Element.Status = MO["Status"as String;    // 设备状态  
  211.                         UsbHubs.Add(Element);  
  212.                     }  
  213.                 }  
  214.   
  215.                 if (UsbHubs.Count == 0) return nullelse return UsbHubs.ToArray();  
  216.             }  
  217.         }  
  218.           
  219.         /// <summary>  
  220.         /// USB ROOT HUB设备路径  
  221.         /// </summary>  
  222.         /// <param name="PNPDeviceID">USB主控制器设备ID</param>  
  223.         /// <returns>USB ROOT HUB设备路径</returns>  
  224.         public static String GetUsbRootHubPath(String PNPDeviceID)  
  225.         {  
  226.             if (String.IsNullOrEmpty(PNPDeviceID)) return null;              
  227.   
  228.             // 打开设备  
  229.             IntPtr hHCDev = Kernel32.CreateFile(  
  230.                 "\\\\.\\" + PNPDeviceID.Replace('\\', '#') + "#{3ABF6F2D-71C4-462A-8A92-1E6861E6AF27}",  
  231.                 NativeFileAccess.GENERIC_WRITE,  
  232.                 NativeFileShare.FILE_SHARE_WRITE,  
  233.                 IntPtr.Zero,  
  234.                 NativeFileMode.OPEN_EXISTING,  
  235.                 IntPtr.Zero,  
  236.                 IntPtr.Zero);  
  237.             if (hHCDev == Kernel32.INVALID_HANDLE_VALUE) return null;  
  238.               
  239.             // 获取USB ROOT HUB名称  
  240.             Int32 nBytesReturned;  
  241.             USB_HCD_DRIVERKEY_NAME Buffer = new USB_HCD_DRIVERKEY_NAME();  
  242.             Boolean Status = DeviceIoControl(hHCDev,  
  243.                 IOCTL_USB_GET_ROOT_HUB_NAME,  
  244.                 IntPtr.Zero,  
  245.                 0,  
  246.                 ref Buffer,  
  247.                 Marshal.SizeOf(Buffer),  
  248.                 out nBytesReturned,  
  249.                 IntPtr.Zero);  
  250.   
  251.             // 关闭设备  
  252.             Kernel32.CloseHandle(hHCDev);  
  253.             return Status ? Buffer.Name : null;  
  254.         }  
  255.   
  256.         /// <summary>  
  257.         /// USB HUB设备名称  
  258.         /// </summary>  
  259.         /// <param name="DevicePath">设备路径</param>  
  260.         /// <returns>设备名称</returns>  
  261.         public static String GetUsbHubName(String DevicePath)  
  262.         {  
  263.             if (String.IsNullOrEmpty(DevicePath)) return null;  
  264.   
  265.             // 从设备路径中提取设备ID  
  266.             String DeviceID = DevicePath.Substring(0, DevicePath.LastIndexOf('#')).Replace('#''_');  
  267.   
  268.             // 从Win32_USBHub获取设备描述  
  269.             ManagementObjectCollection MOC = new ManagementObjectSearcher("SELECT * FROM Win32_USBHub WHERE DeviceID LIKE '" + DeviceID + "'").Get();  
  270.             if (MOC != null)  
  271.             {  
  272.                 foreach (ManagementObject MO in MOC)  
  273.                 {  
  274.                     return MO["Name"as String;  
  275.                 }  
  276.             }  
  277.   
  278.             return null;  
  279.         }  
  280.   
  281.         /// <summary>  
  282.         /// 获取USB HUB节点信息  
  283.         /// </summary>  
  284.         /// <param name="DevicePath">USB HUB设备路径</param>  
  285.         /// <returns>节点信息</returns>  
  286.         public static UsbNodeInformation[] GetUsbNodeInformation(String DevicePath)  
  287.         {  
  288.             if (String.IsNullOrEmpty(DevicePath)) return null;  
  289.   
  290.             // 打开设备文件  
  291.             IntPtr hHubDevice = Kernel32.CreateFile(  
  292.                 "\\\\.\\" + DevicePath,  
  293.                 NativeFileAccess.GENERIC_WRITE,  
  294.                 NativeFileShare.FILE_SHARE_WRITE,  
  295.                 IntPtr.Zero,  
  296.                 NativeFileMode.OPEN_EXISTING,  
  297.                 IntPtr.Zero,  
  298.                 IntPtr.Zero);  
  299.             if (hHubDevice == Kernel32.INVALID_HANDLE_VALUE) return null;  
  300.   
  301.             // 查询节点信息  
  302.             Int32 nBytesReturned;  
  303.             USB_NODE_INFORMATION Buffer = new USB_NODE_INFORMATION();  
  304.             Boolean Status = DeviceIoControl(hHubDevice,  
  305.                 IOCTL_USB_GET_NODE_INFORMATION,  
  306.                 ref Buffer,  
  307.                 Marshal.SizeOf(Buffer),  
  308.                 ref Buffer,  
  309.                 Marshal.SizeOf(Buffer),  
  310.                 out nBytesReturned,  
  311.                 IntPtr.Zero);  
  312.   
  313.             // 关闭设备文件  
  314.             Kernel32.CloseHandle(hHubDevice);  
  315.             if (!Status) return null;  
  316.   
  317.             UsbNodeInformation Node = new UsbNodeInformation();  
  318.             Node.NodeType = Buffer.NodeType;    // 节点类型  
  319.             Node.PNPDeviceID = DevicePath.Substring(0, DevicePath.LastIndexOf('#')).Replace('#''\\'); // 设备ID  
  320.             Node.DevicePath = DevicePath;       // 设备路径  
  321.             Node.Name = GetUsbHubName(DevicePath);  // 设备名称  
  322.             if (Buffer.NodeType == USB_HUB_NODE.UsbHub)  
  323.             {  
  324.                 Node.NumberOfPorts = Buffer.u.HubInformation.HubDescriptor.bNumberOfPorts;         // 端口数  
  325.                 Node.HubIsBusPowered = Convert.ToBoolean(Buffer.u.HubInformation.HubIsBusPowered);  // 供电方式  
  326.                 Node.HubCharacteristics = Buffer.u.HubInformation.HubDescriptor.wHubCharacteristics;  
  327.                 Node.PowerOnToPowerGood = Buffer.u.HubInformation.HubDescriptor.bPowerOnToPowerGood;  
  328.                 Node.HubControlCurrent = Buffer.u.HubInformation.HubDescriptor.bHubControlCurrent;  
  329.             }  
  330.             else  
  331.             {  
  332.                 Node.NumberOfInterfaces = Buffer.u.MiParentInformation.NumberOfInterfaces;  // 接口数  
  333.             }  
  334.   
  335.             return new UsbNodeInformation[1] { Node };  
  336.         }  
  337.         #endregion   
  338.         
  339.         #region NODECONNECTION  
  340.         /// <summary>  
  341.         /// 获取USB节点连接信息  
  342.         /// </summary>  
  343.         /// <param name="DevicePath">设备路径</param>  
  344.         /// <param name="NumberOfPorts">端口总数</param>  
  345.         /// <returns>USB节点信息连接信息集合</returns>  
  346.         public static UsbNodeConnectionInformation[] GetUsbNodeConnectionInformation(String DevicePath, Int32 NumberOfPorts)  
  347.         {  
  348.             if (String.IsNullOrEmpty(DevicePath)) return null;  
  349.   
  350.             // 打开设备文件  
  351.             IntPtr hHubDevice = Kernel32.CreateFile(  
  352.                 "\\\\.\\" + DevicePath,  
  353.                 NativeFileAccess.GENERIC_WRITE,  
  354.                 NativeFileShare.FILE_SHARE_WRITE,  
  355.                 IntPtr.Zero,  
  356.                 NativeFileMode.OPEN_EXISTING,  
  357.                 IntPtr.Zero,  
  358.                 IntPtr.Zero);  
  359.             if (hHubDevice == Kernel32.INVALID_HANDLE_VALUE) return null;  
  360.   
  361.             List<UsbNodeConnectionInformation> NodeCollection = new List<UsbNodeConnectionInformation>();  
  362.               
  363.             // 枚举端口  
  364.             USB_NODE_CONNECTION_INFORMATION_EX Buffer = new USB_NODE_CONNECTION_INFORMATION_EX();  
  365.             for (Int32 ConnectionIndex = 1; ConnectionIndex <= NumberOfPorts; ConnectionIndex++)  
  366.             {  
  367.                 // 查询节点信息  
  368.                 Int32 nBytesReturned;  
  369.                 Buffer.ConnectionIndex = ConnectionIndex;  
  370.                 Boolean Status = DeviceIoControl(hHubDevice,  
  371.                     IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX,  
  372.                     ref Buffer,  
  373.                     Marshal.SizeOf(Buffer),  
  374.                     ref Buffer,  
  375.                     Marshal.SizeOf(Buffer),  
  376.                     out nBytesReturned,  
  377.                     IntPtr.Zero);  
  378.                 if (Status)  
  379.                 {  
  380.                     // 确定语言ID  
  381.                     UInt16 LanguageID = SelectLanguageID(hHubDevice, ConnectionIndex);  
  382.                       
  383.                     // 提取信息  
  384.                     UsbNodeConnectionInformation Node = new UsbNodeConnectionInformation();  
  385.   
  386.                     Node.DevicePath = DevicePath;  
  387.                     Node.ConnectionIndex = Buffer.ConnectionIndex;  
  388.                     Node.ConnectionStatus = Buffer.ConnectionStatus;  
  389.                     if (Buffer.ConnectionStatus == USB_CONNECTION_STATUS.DeviceConnected)  
  390.                     {  
  391.                         Node.CurrentConfigurationValue = Buffer.CurrentConfigurationValue;  
  392.                         Node.Speed = Buffer.Speed;  
  393.                         Node.DeviceIsHub = Convert.ToBoolean(Buffer.DeviceIsHub);  
  394.                         Node.DeviceAddress = Buffer.DeviceAddress;  
  395.                         Node.NumberOfOpenPipes = Buffer.NumberOfOpenPipes;                          
  396.   
  397.                         // 设备描述符  
  398.                         Node.DeviceDescriptor.bDescriptorType = Buffer.DeviceDescriptor.bDescriptorType;  
  399.                         Node.DeviceDescriptor.bDeviceClass = Buffer.DeviceDescriptor.bDeviceClass;  
  400.                         Node.DeviceDescriptor.bDeviceSubClass = Buffer.DeviceDescriptor.bDeviceSubClass;  
  401.                         Node.DeviceDescriptor.bDeviceProtocol = Buffer.DeviceDescriptor.bDeviceProtocol;  
  402.   
  403.                         Node.DeviceDescriptor.UsbVersion = BcdVersionToString(Buffer.DeviceDescriptor.bcdUSB); // USB版本号  
  404.                         Node.DeviceDescriptor.DeviceVersion = BcdVersionToString(Buffer.DeviceDescriptor.bcdDevice);    // 设备版本号  
  405.   
  406.                         Node.DeviceDescriptor.idVendor = Buffer.DeviceDescriptor.idVendor;      // 厂商标识  
  407.                         Node.DeviceDescriptor.idProduct = Buffer.DeviceDescriptor.idProduct;    // 产品标识  
  408.                           
  409.                         if (LanguageID != 0)  
  410.                         {  
  411.                             if (Buffer.DeviceDescriptor.iSerialNumber != 0)  
  412.                             {   // 序列号  
  413.                                 Node.DeviceDescriptor.SerialNumber = GetStringDescriptor(hHubDevice,  
  414.                                     Buffer.ConnectionIndex,  
  415.                                     Buffer.DeviceDescriptor.iSerialNumber,  
  416.                                     LanguageID);  
  417.                             }  
  418.   
  419.                             if (Buffer.DeviceDescriptor.iManufacturer != 0)  
  420.                             {   // 制造商名称  
  421.                                 Node.DeviceDescriptor.Manufacturer = GetStringDescriptor(hHubDevice,  
  422.                                     Buffer.ConnectionIndex,  
  423.                                     Buffer.DeviceDescriptor.iManufacturer,  
  424.                                     LanguageID);  
  425.                             }  
  426.   
  427.                             if (Buffer.DeviceDescriptor.iProduct != 0)  
  428.                             {   // 产品名称  
  429.                                 Node.DeviceDescriptor.Product = GetStringDescriptor(hHubDevice,  
  430.                                     Buffer.ConnectionIndex,  
  431.                                     Buffer.DeviceDescriptor.iProduct,  
  432.                                     LanguageID);  
  433.                             }  
  434.                         }  
  435.                          
  436.                         Node.DeviceDescriptor.bMaxPacketSize0 = Buffer.DeviceDescriptor.bMaxPacketSize0;  
  437.                         Node.DeviceDescriptor.bNumConfigurations = Buffer.DeviceDescriptor.bNumConfigurations;  
  438.   
  439.                         // 管道信息  
  440.                         Node.PipeList = new List<UsbPipeInfo>();  
  441.                         for (Int32 PipeIndex = 0; PipeIndex < Buffer.NumberOfOpenPipes; PipeIndex++)  
  442.                         {  
  443.                             UsbPipeInfo PipeInfo;  
  444.   
  445.                             PipeInfo.ScheduleOffset = Buffer.PipeList[PipeIndex].ScheduleOffset;  
  446.                             PipeInfo.bDescriptorType = Buffer.PipeList[PipeIndex].EndpointDescriptor.bDescriptorType;  
  447.                             PipeInfo.bEndpointAddress = Buffer.PipeList[PipeIndex].EndpointDescriptor.bEndpointAddress;  
  448.                             PipeInfo.bmAttributes = Buffer.PipeList[PipeIndex].EndpointDescriptor.bmAttributes;  
  449.                             PipeInfo.wMaxPacketSize = Buffer.PipeList[PipeIndex].EndpointDescriptor.wMaxPacketSize;  
  450.                             PipeInfo.bInterval = Buffer.PipeList[PipeIndex].EndpointDescriptor.bInterval;  
  451.   
  452.                             Node.PipeList.Add(PipeInfo);  
  453.                         }  
  454.                     }  
  455.   
  456.                     NodeCollection.Add(Node);  
  457.                 }  
  458.             }  
  459.   
  460.             // 关闭设备文件  
  461.             Kernel32.CloseHandle(hHubDevice);  
  462.   
  463.             // 返回结果  
  464.             if (NodeCollection.Count == 0)  
  465.                 return null;  
  466.             else  
  467.                 return NodeCollection.ToArray();  
  468.         }  
  469.   
  470.         /// <summary>  
  471.         /// 获取字符串描述符  
  472.         /// </summary>  
  473.         /// <param name="hHubDevice">USB Hub设备句柄</param>  
  474.         /// <param name="ConnectionIndex">连接索引号</param>  
  475.         /// <param name="DescriptorIndex">描述符索引号</param>  
  476.         /// <param name="LanguageID">语言ID</param>  
  477.         /// <returns>字符串描述符</returns>  
  478.         public static String GetStringDescriptor(IntPtr hHubDevice, Int32 ConnectionIndex, Byte DescriptorIndex, UInt16 LanguageID)  
  479.         {          
  480.             USB_DESCRIPTOR_REQUEST Buffer = new USB_DESCRIPTOR_REQUEST();  
  481.             Buffer.ConnectionIndex = ConnectionIndex;  
  482.             Buffer.SetupPacket.wValue = (UInt16)((USB_STRING_DESCRIPTOR_TYPE << 8) | DescriptorIndex);  
  483.             Buffer.SetupPacket.wIndex = LanguageID;  
  484.             Buffer.SetupPacket.wLength = MAXIMUM_USB_STRING_LENGTH;  
  485.             Int32 nBytesReturned;  
  486.             Boolean Status = DeviceIoControl(hHubDevice,  
  487.                     IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION,  
  488.                     ref Buffer,  
  489.                     Marshal.SizeOf(Buffer),  
  490.                     ref Buffer,  
  491.                     Marshal.SizeOf(Buffer),  
  492.                     out nBytesReturned,  
  493.                     IntPtr.Zero);  
  494.             if (Status)  
  495.                 return Buffer.Data.bString;  
  496.             else  
  497.                 return null;  
  498.         }  
  499.   
  500.         /// <summary>  
  501.         /// 选择语言ID  
  502.         /// </summary>  
  503.         /// <param name="hHubDevice">USB Hub设备句柄</param>  
  504.         /// <param name="ConnectionIndex">连接索引号</param>  
  505.         /// <returns></returns>  
  506.         public static UInt16 SelectLanguageID(IntPtr hHubDevice, Int32 ConnectionIndex)  
  507.         {     
  508.             // 获取支持的语言列表  
  509.             String SupportedLanguagesString = GetStringDescriptor(hHubDevice, ConnectionIndex, 0, 0);  
  510.             if(String.IsNullOrEmpty(SupportedLanguagesString))return 0;  
  511.   
  512.             UInt16 UserDefaultUILanguage = Splash.Environment.UserDefaultUILanguage;  
  513.             if(SupportedLanguagesString.IndexOf(Convert.ToChar(UserDefaultUILanguage)) != -1)  
  514.             {   // 用户缺省界面语言  
  515.                 return UserDefaultUILanguage;  
  516.             }  
  517.             else if(SupportedLanguagesString.IndexOf(Convert.ToChar(0x0409)) != -1)  
  518.             {   // 美国英语 0x0409  
  519.                 return 0x0409;  
  520.             }  
  521.             else  
  522.             {   // 第一个可选择的LANGID  
  523.                 return Convert.ToUInt16(SupportedLanguagesString[0]);  
  524.             }        
  525.         }  
  526.         #endregion  
  527.  
  528.         #region EXTERNALHUB  
  529.         /// <summary>  
  530.         /// 获取外接Hub设备路径  
  531.         /// </summary>  
  532.         /// <param name="ParentDevicePath">上层Hub设备路径</param>  
  533.         /// <param name="ConnectionIndex">连接索引号</param>  
  534.         /// <returns>外接Hub设备路径</returns>  
  535.         public static String GetExternalHubPath(String ParentDevicePath, Int32 ConnectionIndex)  
  536.         {  
  537.             if (String.IsNullOrEmpty(ParentDevicePath)) return null;  
  538.   
  539.             // 打开设备文件  
  540.             IntPtr hParentHubDevice = Kernel32.CreateFile(  
  541.                 "\\\\.\\" + ParentDevicePath,  
  542.                 NativeFileAccess.GENERIC_WRITE,  
  543.                 NativeFileShare.FILE_SHARE_WRITE,  
  544.                 IntPtr.Zero,  
  545.                 NativeFileMode.OPEN_EXISTING,  
  546.                 IntPtr.Zero,  
  547.                 IntPtr.Zero);  
  548.             if (hParentHubDevice == Kernel32.INVALID_HANDLE_VALUE) return null;  
  549.   
  550.             USB_NODE_CONNECTION_DRIVERKEY_NAME Buffer = new USB_NODE_CONNECTION_DRIVERKEY_NAME();  
  551.             Buffer.ConnectionIndex = ConnectionIndex;  
  552.             Int32 nBytesReturned;  
  553.             Boolean Status = DeviceIoControl(hParentHubDevice,  
  554.                     IOCTL_USB_GET_NODE_CONNECTION_NAME,  
  555.                     ref Buffer,  
  556.                     Marshal.SizeOf(Buffer),  
  557.                     ref Buffer,  
  558.                     Marshal.SizeOf(Buffer),  
  559.                     out nBytesReturned,  
  560.                     IntPtr.Zero);  
  561.   
  562.             // 关闭设备文件  
  563.             Kernel32.CloseHandle(hParentHubDevice);  
  564.   
  565.             if (Status)  
  566.                 return Buffer.DriverKeyName;  
  567.             else  
  568.                 return null;  
  569.         }  
  570.   
  571.         /// <summary>  
  572.         /// 获取外接Hub设备路径  
  573.         /// </summary>  
  574.         /// <param name="hParentHubDevice">上层Hub设备句柄</param>  
  575.         /// <param name="ConnectionIndex">连接索引号</param>  
  576.         /// <returns>外接Hub设备路径</returns>  
  577.         public static String GetExternalHubPath(IntPtr hParentHubDevice, Int32 ConnectionIndex)  
  578.         {  
  579.             if (hParentHubDevice == IntPtr.Zero || ConnectionIndex <= 0) return null;  
  580.   
  581.             USB_NODE_CONNECTION_DRIVERKEY_NAME Buffer = new USB_NODE_CONNECTION_DRIVERKEY_NAME();  
  582.             Buffer.ConnectionIndex = ConnectionIndex;              
  583.             Int32 nBytesReturned;  
  584.             Boolean Status = DeviceIoControl(hParentHubDevice,  
  585.                     IOCTL_USB_GET_NODE_CONNECTION_NAME,  
  586.                     ref Buffer,  
  587.                     Marshal.SizeOf(Buffer),  
  588.                     ref Buffer,  
  589.                     Marshal.SizeOf(Buffer),  
  590.                     out nBytesReturned,  
  591.                     IntPtr.Zero);              
  592.   
  593.             if (Status)  
  594.                 return Buffer.DriverKeyName;  
  595.             else  
  596.                 return null;  
  597.         }  
  598.         #endregion  
  599.  
  600.         #region BCDVERSION  
  601.         /// <summary>  
  602.         /// 版本BCD编码转字符串  
  603.         /// </summary>  
  604.         /// <param name="bcd">版本BCD编码</param>  
  605.         /// <returns>版本字符串</returns>  
  606.         private static String BcdVersionToString(UInt16 bcd)  
  607.         {  
  608.             StringBuilder sb = new StringBuilder(5);  
  609.   
  610.             // 主版本号  
  611.             Int32 BIT4 = (bcd >> 12) & 0x0F;  
  612.             if (BIT4 != 0) sb.Append(BIT4.ToString());  
  613.   
  614.             BIT4 = (bcd >> 8) & 0x0F;  
  615.             sb.Append(BIT4.ToString());  
  616.   
  617.             sb.Append(".");  
  618.   
  619.             // 子版本号  
  620.             BIT4 = (bcd >> 4) & 0x0F;  
  621.             sb.Append(BIT4.ToString());  
  622.   
  623.             BIT4 = bcd & 0x0F;  
  624.             if (BIT4 != 0) sb.Append(BIT4.ToString());  
  625.   
  626.             return sb.ToString();  
  627.         }  
  628.         #endregion  
  629.     }  
  630. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值