internal static extern bool SetupDiGetDeviceInterfaceDetail(
     SafeDevInfoHandle deviceInfoSet,
     [MarshalAs(UnmanagedType.LPStruct), In] SP_DEVICE_INTERFACE_DATA deviceInterfaceData,
     [MarshalAs(UnmanagedType.LPStruct), In, Out] SP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetailData,
     int deviceInterfaceDetailDataSize,
     ref int requiredSize,
     IntPtr deviceInfoData);
        private string GetBusReportedDeviceDescription(SafeDevInfoHandle deviceInfoSet, SP_DEVINFO_DATA devinfoData)
        {
            var    descriptionBuffer = new byte[1024];
            string resultString      = string.Empty;

            if (Environment.OSVersion.Version.Major > 5)
            {
                ulong propertyType = 0;
                var   requiredSize = 0;
                var   devPropKey   = new DEVPROPKEY();
                var   result       = NativeMethods.SetupDiGetDeviceProperty(
                    deviceInfoSet,
                    devinfoData,
                    devPropKey,
                    ref propertyType,
                    descriptionBuffer,
                    descriptionBuffer.Length,
                    ref requiredSize,
                    0);

                if (result)
                {
                    resultString = DescriptionEncoding.GetString(descriptionBuffer).TrimEnd('\0');
                }
            }
            return(resultString);
        }
 internal static extern bool SetupDiGetDeviceRegistryProperty(
     SafeDevInfoHandle deviceInfoSet,
     [MarshalAs(UnmanagedType.LPStruct), In] SP_DEVINFO_DATA deviceInfoData,
     int propertyVal,
     ref int propertyRegDataType,
     byte[] propertyBuffer,
     int propertyBufferSize,
     ref int requiredSize);
 internal static extern bool SetupDiGetDeviceProperty(
     SafeDevInfoHandle deviceInfo,
     [MarshalAs(UnmanagedType.LPStruct), In] SP_DEVINFO_DATA deviceInfoData,
     [MarshalAs(UnmanagedType.LPStruct), In] DEVPROPKEY propkey,
     ref ulong propertyDataType,
     byte[] propertyBuffer,
     int propertyBufferSize,
     ref int requiredSize,
     uint flags);
        private string GetDevicePath(SafeDevInfoHandle deviceInfoSet, SP_DEVICE_INTERFACE_DATA deviceInterfaceData)
        {
            var bufferSize      = 0;
            var interfaceDetail = new SP_DEVICE_INTERFACE_DETAIL_DATA();

            NativeMethods.SetupDiGetDeviceInterfaceDetailBuffer(deviceInfoSet, deviceInterfaceData, IntPtr.Zero, 0, ref bufferSize, IntPtr.Zero);

            bool result = NativeMethods.SetupDiGetDeviceInterfaceDetail(deviceInfoSet, deviceInterfaceData,
                                                                        interfaceDetail, bufferSize, ref bufferSize, IntPtr.Zero);

            if (result && interfaceDetail.DevicePath != null)
            {
                return(interfaceDetail.DevicePath);
            }

            return(string.Empty);
        }
        public IEnumerable <IHidDeviceInfo> EnumerateDeviceInfo()
        {
            var hidClass = HidClassGuid;

            using SafeDevInfoHandle deviceInfoSet = NativeMethods.SetupDiGetClassDevs(hidClass, null, IntPtr.Zero, NativeMethods.DIGCF_PRESENT | NativeMethods.DIGCF_DEVICEINTERFACE);
            if (deviceInfoSet.IsInvalid)
            {
                yield break;
            }

            SP_DEVINFO_DATA deviceInfoData = new SP_DEVINFO_DATA();
            int             deviceIndex    = 0;

            while (NativeMethods.SetupDiEnumDeviceInfo(deviceInfoSet, deviceIndex, deviceInfoData))
            {
                deviceIndex += 1;

                SP_DEVICE_INTERFACE_DATA deviceInterfaceData = new SP_DEVICE_INTERFACE_DATA();
                int deviceInterfaceIndex = 0;

                while (NativeMethods.SetupDiEnumDeviceInterfaces(deviceInfoSet, deviceInfoData, hidClass, deviceInterfaceIndex, deviceInterfaceData))
                {
                    deviceInterfaceIndex++;
                    string         devicePath = GetDevicePath(deviceInfoSet, deviceInterfaceData);
                    string         busReportedDeviceDescription = GetBusReportedDeviceDescription(deviceInfoSet, deviceInfoData);
                    string         deviceDescription            = GetDeviceDescription(deviceInfoSet, deviceInfoData);
                    string         description = string.IsNullOrEmpty(busReportedDeviceDescription) ? deviceDescription : busReportedDeviceDescription;
                    IHidDeviceInfo?info        = null;
                    try
                    {
                        info = new HidDeviceInfo(devicePath, description);
                    }
                    catch (GetHidAttributesException)
                    {
                        //Console.WriteLine("GetHidCapabilitiesException");
                    }
                    catch (GetHidCapabilitiesException) { }

                    if (info != null)
                    {
                        yield return(info);
                    }
                }
            }
        }
        private string GetDeviceDescription(SafeDevInfoHandle deviceInfoSet, SP_DEVINFO_DATA devinfoData)
        {
            var    descriptionBuffer = new byte[1024];
            string resultString      = string.Empty;
            var    requiredSize      = 0;
            var    type = 0;

            var result = NativeMethods.SetupDiGetDeviceRegistryProperty(
                deviceInfoSet,
                devinfoData,
                NativeMethods.SPDRP_DEVICEDESC,
                ref type,
                descriptionBuffer,
                descriptionBuffer.Length,
                ref requiredSize);

            if (result)
            {
                Encoding.GetEncodings();
                resultString = DescriptionEncoding.GetString(descriptionBuffer).TrimEnd('\0');
            }

            return(resultString);
        }
 internal static extern bool SetupDiEnumDeviceInterfaces(
     SafeDevInfoHandle deviceInfoSet,
     [MarshalAs(UnmanagedType.LPStruct), In] SP_DEVINFO_DATA deviceInfoData,
     [MarshalAs(UnmanagedType.LPStruct), In] Guid interfaceClassGuid,
     int memberIndex,
     [MarshalAs(UnmanagedType.LPStruct), In] SP_DEVICE_INTERFACE_DATA deviceInterfaceData);
 internal static extern bool SetupDiEnumDeviceInfo(SafeDevInfoHandle deviceInfoSet, int memberIndex, [MarshalAs(UnmanagedType.LPStruct), In] SP_DEVINFO_DATA deviceInfoData);