Exemple #1
0
        /// <summary>
        /// Builds the device.
        /// </summary>
        /// <param name="parent">The parent.</param>
        /// <param name="portCount">The port count.</param>
        /// <param name="devicePath">The device path.</param>
        /// <returns>The device.</returns>
        public static Device BuildDevice(Device parent, uint portCount, string devicePath)
        {
            Device device = null;

            // Open a handle to the Hub device
            IntPtr deviceHandle = KernelApi.CreateFile(devicePath, UsbApi.GenericWrite, UsbApi.FileShareWrite,
                                                       IntPtr.Zero,
                                                       UsbApi.OpenExisting, 0, IntPtr.Zero);

            try
            {
                if (deviceHandle.ToInt64() != UsbApi.InvalidHandleValue)
                {
                    if (GetNodeConnectionInformationEx(portCount, deviceHandle, out var nodeConnection))
                    {
                        if (nodeConnection.ConnectionStatus == UsbIoControl.UsbConnectionStatus.DeviceConnected)
                        {
                            GetUsbPortConnectorProperties(portCount, deviceHandle,
                                                          out var portConnectorProperties);

                            GetNodeConnectionInformationExV2(portCount, deviceHandle, out var nodeConnectionV2);

                            if (nodeConnection.DeviceDescriptor.bDeviceClass == UsbDesc.DeviceClassType.UsbHubDevice)
                            {
                                if (GetUsbNodeConnectionName(portCount, deviceHandle, out var connectionName))
                                {
                                    string name = @"\\?\" + connectionName.NodeName;

                                    device = new UsbHub(nodeConnection.DeviceDescriptor, name)
                                    {
                                        NodeConnectionInfo         = nodeConnection,
                                        NodeConnectionInfoV2       = nodeConnectionV2,
                                        UsbPortConnectorProperties = portConnectorProperties
                                    };
                                }
                            }
                            else
                            {
                                device = new UsbDevice(nodeConnection.DeviceDescriptor, portCount, devicePath)
                                {
                                    NodeConnectionInfo         = nodeConnection,
                                    NodeConnectionInfoV2       = nodeConnectionV2,
                                    UsbPortConnectorProperties = portConnectorProperties
                                };
                            }
                        }
                        else
                        {
                            device = new UsbDevice(null, portCount)
                            {
                                NodeConnectionInfo = nodeConnection
                            };
                        }
                    }
                }
            }
            finally
            {
                if (deviceHandle.ToInt64() != UsbApi.InvalidHandleValue)
                {
                    KernelApi.CloseHandle(deviceHandle);
                }
            }

            return(device);
        }