/// <summary>Return Root Hub for this Controller</summary> internal UsbHub GetRootHub() { IntPtr h, h2; UsbHub root = new UsbHub { HubIsRootHub = true, HubDeviceDesc = "Root Hub" }; // Open a handle to the Host Controller h = CreateFile(ControllerDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); if (h == INVALID_HANDLE_VALUE) { return(root); } UsbRootHubName hubName = new UsbRootHubName(); int nBytes = Marshal.SizeOf(hubName); IntPtr ptrHubName = Marshal.AllocHGlobal(nBytes); // get the Hub Name if (DeviceIoControl(h, IOCTL_USB_GET_ROOT_HUB_NAME, ptrHubName, nBytes, ptrHubName, nBytes, out _, IntPtr.Zero)) { hubName = (UsbRootHubName)Marshal.PtrToStructure(ptrHubName, typeof(UsbRootHubName)); root.HubDevicePath = @"\\.\" + hubName.RootHubName; } // TODO: Get DriverKeyName for Root Hub // Now let's open the Hub (based upon the HubName we got above) h2 = CreateFile(root.HubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); if (h2 != INVALID_HANDLE_VALUE) { UsbNodeInformation nodeInfo = new UsbNodeInformation { NodeType = (int)UsbHubNode.UsbHub }; nBytes = Marshal.SizeOf(nodeInfo); IntPtr ptrNodeInfo = Marshal.AllocHGlobal(nBytes); Marshal.StructureToPtr(nodeInfo, ptrNodeInfo, true); // get the Hub Information if (DeviceIoControl(h2, IOCTL_USB_GET_NODE_INFORMATION, ptrNodeInfo, nBytes, ptrNodeInfo, nBytes, out _, IntPtr.Zero)) { nodeInfo = (UsbNodeInformation)Marshal.PtrToStructure(ptrNodeInfo, typeof(UsbNodeInformation)); root.HubIsBusPowered = Convert.ToBoolean(nodeInfo.HubInformation.HubIsBusPowered); root.HubPortCount = nodeInfo.HubInformation.HubDescriptor.bNumberOfPorts; } Marshal.FreeHGlobal(ptrNodeInfo); CloseHandle(h2); } Marshal.FreeHGlobal(ptrHubName); CloseHandle(h); return(root); }
/// <summary> /// 增加USB节点信息 /// </summary> /// <param name="Items">要增加的列表</param> /// <param name="Info">要增加的信息</param> private static void Add(ref List <ListViewUsbItem> Items, UsbNodeInformation Info) { if (Info.NodeType == USB_HUB_NODE.UsbHub) { Items.Add(new ListViewUsbItem("Name", Info.Name)); Items.Add(new ListViewUsbItem("PNPDeviceID", Info.PNPDeviceID)); Items.Add(new ListViewUsbItem("DevicePath", Info.DevicePath)); Items.Add(new ListViewUsbItem("NodeType", Info.NodeType.ToString())); Items.Add(new ListViewUsbItem("HubIsBusPowered", Info.HubIsBusPowered.ToString())); Items.Add(new ListViewUsbItem("NumberOfPorts", Info.NumberOfPorts.ToString())); Items.Add(new ListViewUsbItem("HubCharacteristics", "0x" + Info.HubCharacteristics.ToString("X4"))); Items.Add(new ListViewUsbItem("PowerOnToPowerGood", (Info.PowerOnToPowerGood * 2).ToString() + "ms")); Items.Add(new ListViewUsbItem("HubControlCurrent", Info.HubControlCurrent.ToString())); } else { Items.Add(new ListViewUsbItem("Name", Info.Name)); Items.Add(new ListViewUsbItem("PNPDeviceID", Info.PNPDeviceID)); Items.Add(new ListViewUsbItem("DevicePath", Info.DevicePath)); Items.Add(new ListViewUsbItem("NodeType", Info.NodeType.ToString())); Items.Add(new ListViewUsbItem("NumberOfInterfaces", Info.NumberOfInterfaces.ToString())); } }
/// <summary>return a down stream external hub</summary> /// <returns>Downstream external hub</returns> internal UsbHub GetHub() { if (!PortIsHub) { return(null); } var hub = new UsbHub(); IntPtr h, h2; hub.HubIsRootHub = false; hub.HubDeviceDesc = "External Hub"; // Open a handle to the Host Controller h = CreateFile(PortHubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); if (h == INVALID_HANDLE_VALUE) { return(hub); } // Get the DevicePath for downstream hub var nodeName = new UsbNodeConnectionName { ConnectionIndex = PortPortNumber }; int nBytes = Marshal.SizeOf(nodeName); IntPtr ptrNodeName = Marshal.AllocHGlobal(nBytes); Marshal.StructureToPtr(nodeName, ptrNodeName, true); // Use an IOCTL call to request the Node Name if (DeviceIoControl(h, IOCTL_USB_GET_NODE_CONNECTION_NAME, ptrNodeName, nBytes, ptrNodeName, nBytes, out _, IntPtr.Zero)) { nodeName = (UsbNodeConnectionName)Marshal.PtrToStructure(ptrNodeName, typeof(UsbNodeConnectionName)); hub.HubDevicePath = @"\\.\" + nodeName.NodeName; } // Now let's open the Hub (based upon the HubName we got above) h2 = CreateFile(hub.HubDevicePath, GENERIC_WRITE, FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero); if (h2 != INVALID_HANDLE_VALUE) { var nodeInfo = new UsbNodeInformation { NodeType = (int)UsbHubNode.UsbHub }; nBytes = Marshal.SizeOf(nodeInfo); IntPtr ptrNodeInfo = Marshal.AllocHGlobal(nBytes); Marshal.StructureToPtr(nodeInfo, ptrNodeInfo, true); // get the Hub Information if (DeviceIoControl(h2, IOCTL_USB_GET_NODE_INFORMATION, ptrNodeInfo, nBytes, ptrNodeInfo, nBytes, out _, IntPtr.Zero)) { nodeInfo = (UsbNodeInformation)Marshal.PtrToStructure(ptrNodeInfo, typeof(UsbNodeInformation)); hub.HubIsBusPowered = Convert.ToBoolean(nodeInfo.HubInformation.HubIsBusPowered); hub.HubPortCount = nodeInfo.HubInformation.HubDescriptor.bNumberOfPorts; } Marshal.FreeHGlobal(ptrNodeInfo); CloseHandle(h2); } // Fill in the missing Manufacture, Product, and SerialNumber values // values by just creating a Device instance and copying the values UsbDevice device = GetDevice(); hub.HubInstanceId = device.DeviceInstanceId; hub.HubManufacturer = device.Manufacturer; hub.HubProduct = device.Product; hub.HubSerialNumber = device.SerialNumber; hub.HubDriverKey = device.DriverKey; Marshal.FreeHGlobal(ptrNodeName); CloseHandle(h); return(hub); }
/// <summary> /// 生成USB属性列表 /// </summary> /// <param name="Data">用于生成列表的USB数据</param> /// <returns>属性列表</returns> public static List <ListViewUsbItem> UsbDetail(Object Data) { if (Data is String) { // 机器名 String Info = Data as String; if (!String.IsNullOrEmpty(Info)) { String[] Content = Info.Split(new Char[] { ':' }); if (Content.Length == 2) { return(new List <ListViewUsbItem>(1) { new ListViewUsbItem(Content[0], Content[1]) }); } } } else if (Data is HostControllerInfo) { // 主控制器信息 HostControllerInfo Info = (HostControllerInfo)Data; return(new List <ListViewUsbItem>(3) { new ListViewUsbItem("Name", Info.Name), new ListViewUsbItem("PNPDeviceID", Info.PNPDeviceID), new ListViewUsbItem("HcdDriverKeyName", Info.HcdDriverKeyName) }); } else if (Data is UsbNodeInformation) { // USB节点信息 UsbNodeInformation Info = (UsbNodeInformation)Data; List <ListViewUsbItem> Items = new List <ListViewUsbItem>(); Add(ref Items, Info); return(Items); } else if (Data is UsbNodeConnectionInformation) { // USB节点连接信息 UsbNodeConnectionInformation Info = (UsbNodeConnectionInformation)Data; if (Info.ConnectionStatus != USB_CONNECTION_STATUS.DeviceConnected) { return(null); } List <ListViewUsbItem> Items = new List <ListViewUsbItem>(); Add(ref Items, Info); return(Items); } else if (Data is ExternalHubInfo) { // 外部Hub信息 ExternalHubInfo Info = (ExternalHubInfo)Data; List <ListViewUsbItem> Items = new List <ListViewUsbItem>(); // 加入USB节点信息 Items.Add(new ListViewUsbItem("Node Information:", null)); Add(ref Items, Info.NodeInfo); // 加入USB节点连接信息 Items.Add(new ListViewUsbItem(null, null)); Items.Add(new ListViewUsbItem("Node Connection Information:", null)); Add(ref Items, Info.NodeConnectionInfo); return(Items); } return(null); }