Example #1
0
 public static extern Boolean DeviceIoControl(IntPtr hDevice,
                                              Int32 dwIoControlCode,
                                              ref UsbNodeInformation lpInBuffer,
                                              Int32 nInBufferSize,
                                              ref UsbNodeInformation lpOutBuffer,
                                              Int32 nOutBufferSize,
                                              out Int32 lpBytesReturned,
                                              IntPtr lpOverlapped);
Example #2
0
        /// <summary>
        /// 获取USB集线器节点信息。
        /// </summary>
        /// <param name="usbHubPath">USB集线器路径。</param>
        /// <returns>USB集线器节点信息。</returns>
        public static Datas.UsbNodeInformation GetUsbHubNodeInformation(String usbHubPath)
        {
            if (String.IsNullOrWhiteSpace(usbHubPath))
            {
                return(null);
            }
            IntPtr hcdPtr = Win32Api.CreateFile($@"\\.\{usbHubPath}",
                                                NativeFileAccess.GENERIC_WRITE,
                                                NativeFileShare.FILE_SHARE_WRITE,
                                                IntPtr.Zero,
                                                NativeFileMode.OPEN_EXISTING,
                                                IntPtr.Zero,
                                                IntPtr.Zero);

            if (hcdPtr == Win32Api.INVALID_HANDLE_VALUE)
            {
                return(null);
            }

            Int32 bytesReturned;

            Primitives.UsbNodeInformation buffer = new Primitives.UsbNodeInformation();
            Boolean status = Win32Api.DeviceIoControl(hcdPtr,
                                                      Win32Api.IOCTL_USB_GET_NODE_INFORMATION,
                                                      ref buffer,
                                                      Marshal.SizeOf(buffer),
                                                      ref buffer,
                                                      Marshal.SizeOf(buffer),
                                                      out bytesReturned,
                                                      IntPtr.Zero);

            Win32Api.CloseHandle(hcdPtr);
            if (!status)
            {
                return(null);
            }
            Datas.UsbNodeInformation nodeInfo = new Datas.UsbNodeInformation
            {
                NodeType    = buffer.NodeType,
                PNPDeviceId = GetDeviceIdFromPath(usbHubPath),
                DevicePath  = usbHubPath,
                Name        = GetUsbHubName(usbHubPath),
            };
            if (nodeInfo.NodeType == UsbHubNode.UsbHub)
            {
                nodeInfo.NumberOfPorts      = buffer.U.HubInformation.HubDescriptor.NumberOfPorts;
                nodeInfo.HubCharateristics  = buffer.U.HubInformation.HubDescriptor.HubCharacteristics;
                nodeInfo.PowerOnToPowerGood = buffer.U.HubInformation.HubDescriptor.PowerOnToPowerGood;
                nodeInfo.HubControlCurrent  = buffer.U.HubInformation.HubDescriptor.HubControlCurrent;
                nodeInfo.HubIsBusPowered    = buffer.U.HubInformation.HubIsBusPowered;
            }
            else
            {
                nodeInfo.NumberOfInterfaces = buffer.U.MiParentInformation.NumberOfInterfaces;
            }
            return(nodeInfo);
        }