changeset 87:9b898d8b2541

Small improvements in Windows USB device handling
author Ivo Smits <Ivo@UCIS.nl>
date Thu, 05 Jun 2014 00:46:15 +0200
parents 5d1b72ba44dc
children ebdff34b9e4f
files USBLib/Windows/USB/UsbDevice.cs USBLib/Windows/USB/UsbHub.cs
diffstat 2 files changed, 14 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/USBLib/Windows/USB/UsbDevice.cs	Thu Jun 05 00:24:24 2014 +0200
+++ b/USBLib/Windows/USB/UsbDevice.cs	Thu Jun 05 00:46:15 2014 +0200
@@ -49,9 +49,20 @@
 		public int VendorID { get { return DeviceDescriptor.VendorID; } }
 		public int ProductID { get { return DeviceDescriptor.ProductID; } }
 
+		private short[] languages = null;
+
 		private String GetStringSafe(Byte id) {
 			if (id == 0) return null;
-			String s = UsbStringDescriptor.GetStringFromDevice(this, id, 0);
+			if (languages == null) {
+				Byte[] buff = new Byte[256];
+				int len = GetDescriptor((Byte)UsbDescriptorType.String, 0, 0, buff, 0, buff.Length);
+				if (len > 1) {
+					languages = new short[len / 2 - 1];
+					for (int i = 0; i < languages.Length; i++) languages[i] = BitConverter.ToInt16(buff, i * 2 + 2);
+				}
+			}
+			short language = (languages == null || languages.Length == 0) ? (short)0 : languages[0];
+			String s = UsbStringDescriptor.GetStringFromDevice(this, id, language);
 			if (s == null) return s;
 			return s.Trim(' ', '\0');
 		}
--- a/USBLib/Windows/USB/UsbHub.cs	Thu Jun 05 00:24:24 2014 +0200
+++ b/USBLib/Windows/USB/UsbHub.cs	Thu Jun 05 00:46:15 2014 +0200
@@ -22,9 +22,8 @@
 			if (HasNodeInformation) return;
 			NodeInformation = new USB_NODE_INFORMATION();
 			int nBytes = Marshal.SizeOf(typeof(USB_NODE_INFORMATION));
-			using (SafeFileHandle handle = OpenHandle()) 
-				if (!Kernel32.DeviceIoControl(handle, UsbApi.IOCTL_USB_GET_NODE_INFORMATION, ref NodeInformation, nBytes, out NodeInformation, nBytes, out nBytes, IntPtr.Zero))
-					throw new Win32Exception(Marshal.GetLastWin32Error());
+			using (SafeFileHandle handle = OpenHandle())
+				HasNodeInformation = Kernel32.DeviceIoControl(handle, UsbApi.IOCTL_USB_GET_NODE_INFORMATION, ref NodeInformation, nBytes, out NodeInformation, nBytes, out nBytes, IntPtr.Zero);
 		}
 
 		public bool IsRootHub { get; private set; }