Mercurial > hg > ucis.core
comparison USBLib/Windows/USB/UsbController.cs @ 21:dcfec2be27c9
Added USBLib
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Mon, 15 Apr 2013 01:04:59 +0200 |
parents | |
children | fd63c453ff65 |
comparison
equal
deleted
inserted
replaced
20:c873e3dd73fe | 21:dcfec2be27c9 |
---|---|
1 using System; | |
2 using System.Runtime.InteropServices; | |
3 using Microsoft.Win32.SafeHandles; | |
4 using UCIS.HWLib.Windows.Devices; | |
5 using UCIS.USBLib.Internal.Windows; | |
6 | |
7 namespace UCIS.HWLib.Windows.USB { | |
8 public class UsbController { | |
9 public String DevicePath { get; private set; } | |
10 public DeviceNode DeviceNode { get; private set; } | |
11 public String DeviceDescription { get; private set; } | |
12 public String DriverKey { get; private set; } | |
13 public UsbHub RootHub { get; private set; } | |
14 internal UsbController(UsbBus parent, DeviceNode di, String devicePath) { | |
15 this.DeviceNode = di; | |
16 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 } | |
30 } | |
31 } |