Ejemplo n.º 1
0
Archivo: USB.cs Proyecto: rambom/Source
        /// <summary>
        /// 获取USB HUB节点信息
        /// </summary>
        /// <param name="DevicePath">USB HUB设备路径</param>
        /// <returns>节点信息</returns>
        public static UsbNodeInformation[] GetUsbNodeInformation(String DevicePath)
        {
            if (String.IsNullOrEmpty(DevicePath))
            {
                return(null);
            }

            // 打开设备文件
            IntPtr hHubDevice = Kernel32.CreateFile(
                "\\\\.\\" + DevicePath,
                NativeFileAccess.GENERIC_WRITE,
                NativeFileShare.FILE_SHARE_WRITE,
                IntPtr.Zero,
                NativeFileMode.OPEN_EXISTING,
                IntPtr.Zero,
                IntPtr.Zero);

            if (hHubDevice == Kernel32.INVALID_HANDLE_VALUE)
            {
                return(null);
            }

            // 查询节点信息
            Int32 nBytesReturned;
            USB_NODE_INFORMATION Buffer = new USB_NODE_INFORMATION();
            Boolean Status = DeviceIoControl(hHubDevice,
                                             IOCTL_USB_GET_NODE_INFORMATION,
                                             ref Buffer,
                                             Marshal.SizeOf(Buffer),
                                             ref Buffer,
                                             Marshal.SizeOf(Buffer),
                                             out nBytesReturned,
                                             IntPtr.Zero);

            // 关闭设备文件
            Kernel32.CloseHandle(hHubDevice);
            if (!Status)
            {
                return(null);
            }

            UsbNodeInformation Node = new UsbNodeInformation();

            Node.NodeType    = Buffer.NodeType;                                                         // 节点类型
            Node.PNPDeviceID = DevicePath.Substring(0, DevicePath.LastIndexOf('#')).Replace('#', '\\'); // 设备ID
            Node.DevicePath  = DevicePath;                                                              // 设备路径
            Node.Name        = GetUsbHubName(DevicePath);                                               // 设备名称
            if (Buffer.NodeType == USB_HUB_NODE.UsbHub)
            {
                Node.NumberOfPorts      = Buffer.u.HubInformation.HubDescriptor.bNumberOfPorts;       // 端口数
                Node.HubIsBusPowered    = Convert.ToBoolean(Buffer.u.HubInformation.HubIsBusPowered); // 供电方式
                Node.HubCharacteristics = Buffer.u.HubInformation.HubDescriptor.wHubCharacteristics;
                Node.PowerOnToPowerGood = Buffer.u.HubInformation.HubDescriptor.bPowerOnToPowerGood;
                Node.HubControlCurrent  = Buffer.u.HubInformation.HubDescriptor.bHubControlCurrent;
            }
            else
            {
                Node.NumberOfInterfaces = Buffer.u.MiParentInformation.NumberOfInterfaces;  // 接口数
            }

            return(new UsbNodeInformation[1] {
                Node
            });
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 获取USB HUB节点信息
        /// </summary>
        /// <param name="DevicePath">USB HUB设备路径</param>
        /// <returns>节点信息</returns>
        public static UsbNodeInformation[] GetUsbNodeInformation(String DevicePath)
        {
            if (String.IsNullOrEmpty(DevicePath)) return null;

            // 打开设备文件
            IntPtr hHubDevice = Kernel32.CreateFile(
                "\\\\.\\" + DevicePath,
                NativeFileAccess.GENERIC_WRITE,
                NativeFileShare.FILE_SHARE_WRITE,
                IntPtr.Zero,
                NativeFileMode.OPEN_EXISTING,
                IntPtr.Zero,
                IntPtr.Zero);
            if (hHubDevice == Kernel32.INVALID_HANDLE_VALUE) return null;

            // 查询节点信息
            Int32 nBytesReturned;
            USB_NODE_INFORMATION Buffer = new USB_NODE_INFORMATION();
            Boolean Status = DeviceIoControl(hHubDevice,
                IOCTL_USB_GET_NODE_INFORMATION,
                ref Buffer,
                Marshal.SizeOf(Buffer),
                ref Buffer,
                Marshal.SizeOf(Buffer),
                out nBytesReturned,
                IntPtr.Zero);

            // 关闭设备文件
            Kernel32.CloseHandle(hHubDevice);
            if (!Status) return null;

            UsbNodeInformation Node = new UsbNodeInformation();
            Node.NodeType = Buffer.NodeType;    // 节点类型
            Node.PNPDeviceID = DevicePath.Substring(0, DevicePath.LastIndexOf('#')).Replace('#', '\\'); // 设备ID
            Node.DevicePath = DevicePath;       // 设备路径
            Node.Name = GetUsbHubName(DevicePath);  // 设备名称
            if (Buffer.NodeType == USB_HUB_NODE.UsbHub)
            {
                Node.NumberOfPorts = Buffer.u.HubInformation.HubDescriptor.bNumberOfPorts;         // 端口数
                Node.HubIsBusPowered = Convert.ToBoolean(Buffer.u.HubInformation.HubIsBusPowered);  // 供电方式
                Node.HubCharacteristics = Buffer.u.HubInformation.HubDescriptor.wHubCharacteristics;
                Node.PowerOnToPowerGood = Buffer.u.HubInformation.HubDescriptor.bPowerOnToPowerGood;
                Node.HubControlCurrent = Buffer.u.HubInformation.HubDescriptor.bHubControlCurrent;
            }
            else
            {
                Node.NumberOfInterfaces = Buffer.u.MiParentInformation.NumberOfInterfaces;  // 接口数
            }

            return new UsbNodeInformation[1] { Node };
        }