comparison 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
comparison
equal deleted inserted replaced
57:b1efeada517e 58:fd63c453ff65
6 6
7 namespace UCIS.HWLib.Windows.USB { 7 namespace UCIS.HWLib.Windows.USB {
8 public class UsbController { 8 public class UsbController {
9 public String DevicePath { get; private set; } 9 public String DevicePath { get; private set; }
10 public DeviceNode DeviceNode { get; private set; } 10 public DeviceNode DeviceNode { get; private set; }
11 public String DeviceDescription { get; private set; } 11 public String DeviceDescription {
12 public String DriverKey { get; private set; } 12 get { return DeviceNode.GetPropertyString(SPDRP.DeviceDesc); }
13 public UsbHub RootHub { get; private set; } 13 }
14 public String DriverKey {
15 get { return DeviceNode.GetPropertyString(SPDRP.Driver); }
16 }
17 public UsbHub RootHub {
18 get {
19 String rootHubName;
20 using (SafeFileHandle handle = UsbHub.OpenHandle(DevicePath)) rootHubName = UsbHub.GetRootHubName(handle);
21 return new UsbHub(null, new USB_NODE_CONNECTION_INFORMATION_EX(), @"\\?\" + rootHubName, 0, true);
22 }
23 }
14 internal UsbController(UsbBus parent, DeviceNode di, String devicePath) { 24 internal UsbController(UsbBus parent, DeviceNode di, String devicePath) {
15 this.DeviceNode = di; 25 this.DeviceNode = di;
16 this.DevicePath = devicePath; 26 this.DevicePath = devicePath;
17 this.DeviceDescription = di.GetPropertyString(SPDRP.DeviceDesc);
18 this.DriverKey = di.GetPropertyString(SPDRP.Driver);
19
20 USB_ROOT_HUB_NAME rootHubName;
21 using (SafeFileHandle handel1 = Kernel32.CreateFile(DevicePath, Kernel32.GENERIC_WRITE, Kernel32.FILE_SHARE_WRITE, IntPtr.Zero, Kernel32.OPEN_EXISTING, 0, IntPtr.Zero)) {
22 if (handel1.IsInvalid) throw new Exception("No port found!");
23 int nBytesReturned;
24 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))
25 throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error());
26 }
27 if (rootHubName.ActualLength <= 0) throw new Exception("rootHubName.ActualLength <= 0");
28 RootHub = new UsbHub(this, null, @"\\?\" + rootHubName.RootHubName);
29 } 27 }
30 } 28 }
31 } 29 }