Пример #1
0
        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);
        }
Пример #2
0
        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());
        }
Пример #3
0
        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
                       ));
        }