# HG changeset patch # User Ivo Smits # Date 1401921975 -7200 # Node ID 9b898d8b2541558891838b2af2e3493854bd0480 # Parent 5d1b72ba44dcfc453e530c0698a4fbc65a1beef5 Small improvements in Windows USB device handling diff -r 5d1b72ba44dc -r 9b898d8b2541 USBLib/Windows/USB/UsbDevice.cs --- 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'); } diff -r 5d1b72ba44dc -r 9b898d8b2541 USBLib/Windows/USB/UsbHub.cs --- 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; }