diff USBLib/Windows/USB/UsbController.cs @ 63:309c705d7460

Updates and cleanup in Windows USB enumeration code
author Ivo Smits <Ivo@UCIS.nl>
date Sun, 13 Oct 2013 18:43:45 +0200
parents edc41c861d96
children
line wrap: on
line diff
--- a/USBLib/Windows/USB/UsbController.cs	Sun Oct 13 02:47:08 2013 +0200
+++ b/USBLib/Windows/USB/UsbController.cs	Sun Oct 13 18:43:45 2013 +0200
@@ -1,21 +1,26 @@
 using System;
 using System.Collections.Generic;
+using System.ComponentModel;
+using System.Runtime.InteropServices;
 using Microsoft.Win32.SafeHandles;
 using UCIS.HWLib.Windows.Devices;
 using UCIS.USBLib.Internal.Windows;
 
 namespace UCIS.HWLib.Windows.USB {
 	public class UsbController {
-		static readonly Guid IID_DEVINTERFACE_USB_HOST_CONTROLLER = new Guid(UsbApi.GUID_DEVINTERFACE_USB_HOST_CONTROLLER);
 		public String DevicePath { get; private set; }
 		public DeviceNode DeviceNode { get; private set; }
 		public String DeviceDescription { get { return DeviceNode.DeviceDescription; } }
 		public String DriverKey { get { return DeviceNode.DriverKey; } }
 		public UsbHub RootHub {
 			get {
-				String rootHubName;
-				using (SafeFileHandle handle = UsbHub.OpenHandle(DevicePath)) rootHubName = UsbHub.GetRootHubName(handle);
-				return new UsbHub(null, new USB_NODE_CONNECTION_INFORMATION_EX(), @"\\?\" + rootHubName, 0, true);
+				USB_ROOT_HUB_NAME rootHubName = new USB_ROOT_HUB_NAME();
+				int nBytesReturned;
+				using (SafeFileHandle handle = UsbHub.OpenHandle(DevicePath))
+					if (!Kernel32.DeviceIoControl(handle, UsbApi.IOCTL_USB_GET_ROOT_HUB_NAME, IntPtr.Zero, 0, out rootHubName, Marshal.SizeOf(rootHubName), out nBytesReturned, IntPtr.Zero))
+						throw new Win32Exception(Marshal.GetLastWin32Error());
+				if (rootHubName.ActualLength <= 0) return null;
+				return new UsbHub(null, @"\\?\" + rootHubName.RootHubName, 0);
 			}
 		}
 		private UsbController(DeviceNode di, String devicePath) {
@@ -23,7 +28,9 @@
 			this.DevicePath = devicePath;
 		}
 
+		static readonly Guid IID_DEVINTERFACE_USB_HOST_CONTROLLER = new Guid(UsbApi.GUID_DEVINTERFACE_USB_HOST_CONTROLLER);
 		public static UsbController GetControllerForDeviceNode(DeviceNode node) {
+			if (node == null) return null;
 			String[] interfaces = node.GetInterfaces(IID_DEVINTERFACE_USB_HOST_CONTROLLER);
 			if (interfaces == null || interfaces.Length == 0) return null;
 			return new UsbController(node, interfaces[0]);