diff USBLib/Windows/USB/UsbController.cs @ 58:fd63c453ff65

Improved Windows USB enumeration classes
author Ivo Smits <Ivo@UCIS.nl>
date Wed, 09 Oct 2013 20:54:15 +0200
parents dcfec2be27c9
children 3424fa5a12c9
line wrap: on
line diff
--- a/USBLib/Windows/USB/UsbController.cs	Fri Oct 04 13:22:21 2013 +0200
+++ b/USBLib/Windows/USB/UsbController.cs	Wed Oct 09 20:54:15 2013 +0200
@@ -8,24 +8,22 @@
 	public class UsbController {
 		public String DevicePath { get; private set; }
 		public DeviceNode DeviceNode { get; private set; }
-		public String DeviceDescription { get; private set; }
-		public String DriverKey { get; private set; }
-		public UsbHub RootHub { get; private set; }
+		public String DeviceDescription {
+			get { return DeviceNode.GetPropertyString(SPDRP.DeviceDesc); }
+		}
+		public String DriverKey {
+			get { return DeviceNode.GetPropertyString(SPDRP.Driver); }
+		}
+		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);
+			}
+		}
 		internal UsbController(UsbBus parent, DeviceNode di, String devicePath) {
 			this.DeviceNode = di;
 			this.DevicePath = devicePath;
-			this.DeviceDescription = di.GetPropertyString(SPDRP.DeviceDesc);
-			this.DriverKey = di.GetPropertyString(SPDRP.Driver);
-
-			USB_ROOT_HUB_NAME rootHubName;
-			using (SafeFileHandle handel1 = Kernel32.CreateFile(DevicePath, Kernel32.GENERIC_WRITE, Kernel32.FILE_SHARE_WRITE, IntPtr.Zero, Kernel32.OPEN_EXISTING, 0, IntPtr.Zero)) {
-				if (handel1.IsInvalid) throw new Exception("No port found!");
-				int nBytesReturned;
-				if (!Kernel32.DeviceIoControl(handel1, UsbApi.IOCTL_USB_GET_ROOT_HUB_NAME, IntPtr.Zero, 0, out rootHubName, Marshal.SizeOf(typeof(USB_ROOT_HUB_NAME)), out nBytesReturned, IntPtr.Zero))
-					throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error());
-			}
-			if (rootHubName.ActualLength <= 0) throw new Exception("rootHubName.ActualLength <= 0");
-			RootHub = new UsbHub(this, null, @"\\?\" + rootHubName.RootHubName);
 		}
 	}
-}
\ No newline at end of file
+}