private static byte[] GetUsbDevicePropertyInWindows( Windows.SafeDevInfoSetHandle deviceInfoSetHandle, ref Windows.SetupDeviceInfoData deviceInfoData, Windows.SetupDeviceRegistryProperty property, ref Windows.RegType regType) { var requiredSize = 0U; var success = Windows.SetupDiGetDeviceRegistryPropertyW( deviceInfoSetHandle, ref deviceInfoData, property, IntPtr.Zero, IntPtr.Zero, 0, ref requiredSize ); if (!success) { var win32Error = Marshal.GetLastWin32Error(); if (win32Error == (int)Windows.Error.InvalidData) { Logger.GetInstance(typeof(UsbManager)).Debug("The requested property " + property + " does not exist"); regType = 0; return(null); } if (win32Error != (int)Windows.Error.InsufficientBuffer) { Logger.GetInstance(typeof(UsbManager)).Error("Can not query USB device registry property, error code: " + win32Error); regType = 0; return(null); } } var propertyBuffer = new byte[requiredSize]; success = Windows.SetupDiGetDeviceRegistryPropertyW( deviceInfoSetHandle, ref deviceInfoData, property, ref regType, propertyBuffer, (uint)propertyBuffer.Length, ref requiredSize ); if (!success) { var win32Error = Marshal.GetLastWin32Error(); Logger.GetInstance(typeof(UsbManager)).Error("Can not get USB device registry property, error code: " + win32Error); return(null); } return(propertyBuffer); }
private static string GetUsbDeviceStringPropertyInWindows( Windows.SafeDevInfoSetHandle deviceInfoSetHandle, ref Windows.SetupDeviceInfoData deviceInfoData, Windows.SetupDeviceRegistryProperty property) { var regType = Windows.RegType.None; var bytes = GetUsbDevicePropertyInWindows( deviceInfoSetHandle, ref deviceInfoData, property, ref regType ); if (bytes == null || bytes.Length == 0 || regType != Windows.RegType.Sz) { return(string.Empty); } return(Encoding.Unicode.GetString(bytes, 0, bytes.Length - sizeof(char)).Trim()); }
private static string[] GetUsbDeviceMultiStringPropertyInWindows( Windows.SafeDevInfoSetHandle deviceInfoSetHandle, ref Windows.SetupDeviceInfoData deviceInfoData, Windows.SetupDeviceRegistryProperty property) { var regType = Windows.RegType.None; var bytes = GetUsbDevicePropertyInWindows( deviceInfoSetHandle, ref deviceInfoData, property, ref regType ); if (bytes == null || bytes.Length == 0 || regType != Windows.RegType.MultiSz) { return(new string[] {}); } return(Encoding.Unicode.GetString(bytes).Split( new[] { '\0' }, StringSplitOptions.RemoveEmptyEntries )); }
private static List <DeviceInfo> GetUsbDevicesInWindows() { var deviceInfos = new List <DeviceInfo>(); var classGuid = GetUsbGuidInWindows(); using (var deviceInfoSetHandle = Windows.SetupDiGetClassDevsW( ref classGuid, null, IntPtr.Zero, Windows.DeviceInfoGetClassFlag.Present | Windows.DeviceInfoGetClassFlag.DeviceInterface )) { if (deviceInfoSetHandle == null || deviceInfoSetHandle.IsInvalid) { Logger.GetInstance(typeof(UsbManager)).Error("Can not find USB devices, error code: " + Marshal.GetLastWin32Error()); return(deviceInfos); } var deviceIndex = 0U; while (true) { var deviceInterfaceData = new Windows.SetupDeviceInterfaceData(); deviceInterfaceData.cbSize = (uint)Marshal.SizeOf(deviceInterfaceData); var success = Windows.SetupDiEnumDeviceInterfaces( deviceInfoSetHandle, IntPtr.Zero, ref classGuid, deviceIndex, ref deviceInterfaceData ); if (!success) { var win32Error = Marshal.GetLastWin32Error(); if (win32Error != (int)Windows.Error.NoMoreItems) { Logger.GetInstance(typeof(UsbManager)).Error("Can not enumerate USB device on index: " + deviceIndex + ", error code: " + win32Error); } break; } var bufferSize = 0; success = Windows.SetupDiGetDeviceInterfaceDetailW( deviceInfoSetHandle, ref deviceInterfaceData, IntPtr.Zero, 0, ref bufferSize, IntPtr.Zero ); if (!success) { var win32Error = Marshal.GetLastWin32Error(); if (win32Error != (int)Windows.Error.InsufficientBuffer) { Logger.GetInstance(typeof(UsbManager)).Error("Can not query USB device interface detail on index: " + deviceIndex + ", error code: " + win32Error); break; } } var deviceInterfaceDetailData = Marshal.AllocHGlobal(bufferSize); Marshal.WriteInt32(deviceInterfaceDetailData, IntPtr.Size == 8 ? 8 : 6); var deviceInfoData = new Windows.SetupDeviceInfoData(); deviceInfoData.cbSize = (uint)Marshal.SizeOf(deviceInfoData); success = Windows.SetupDiGetDeviceInterfaceDetailW( deviceInfoSetHandle, ref deviceInterfaceData, deviceInterfaceDetailData, bufferSize, ref bufferSize, ref deviceInfoData ); if (!success) { var win32Error = Marshal.GetLastWin32Error(); Logger.GetInstance(typeof(UsbManager)).Error("Can not get USB device interface detail on index: " + deviceIndex + ", error code: " + win32Error); break; } var devicePath = Marshal.PtrToStringUni(deviceInterfaceDetailData + 4); Marshal.FreeHGlobal(deviceInterfaceDetailData); var deviceInfo = new DeviceInfo { Path = devicePath, Description = GetUsbDeviceStringPropertyInWindows( deviceInfoSetHandle, ref deviceInfoData, Windows.SetupDeviceRegistryProperty.DeviceDesc ), Manufacturer = GetUsbDeviceStringPropertyInWindows( deviceInfoSetHandle, ref deviceInfoData, Windows.SetupDeviceRegistryProperty.Mfg ), SerialNumber = GetHidDeviceSerialNumberInWindows(devicePath) }; var hardwareIds = GetUsbDeviceMultiStringPropertyInWindows( deviceInfoSetHandle, ref deviceInfoData, Windows.SetupDeviceRegistryProperty.HardwareId ); var regex = new Regex("^(\\w{3})\\\\VID_([0-9A-F]{4})&PID_([0-9A-F]{4})", RegexOptions.IgnoreCase); foreach (var hardwareId in hardwareIds) { var match = regex.Match(hardwareId); if (!match.Success) { continue; } deviceInfo.Optional["type"] = match.Groups[1].Value; deviceInfo.VendorId = match.Groups[2].Value; deviceInfo.ProductId = match.Groups[3].Value; break; } if (!string.IsNullOrWhiteSpace(deviceInfo.VendorId) && !string.IsNullOrWhiteSpace(deviceInfo.ProductId)) { deviceInfos.Add(deviceInfo); } deviceIndex++; } return(deviceInfos); } }