SetupDiDestroyDeviceInfoList() private method

private SetupDiDestroyDeviceInfoList ( IntPtr deviceInfoSet ) : int
deviceInfoSet System.IntPtr
return int
Beispiel #1
0
        private bool TryReEnableDevice(string deviceInstanceId)
        {
            try
            {
                bool success;

                Guid hidGuid = new Guid();
                NativeMethods.HidD_GetHidGuid(ref hidGuid);
                IntPtr deviceInfoSet = NativeMethods.SetupDiGetClassDevs(ref hidGuid, deviceInstanceId, 0,
                                                                         NativeMethods.DIGCF_PRESENT | NativeMethods.DIGCF_DEVICEINTERFACE);
                NativeMethods.SP_DEVINFO_DATA deviceInfoData = new NativeMethods.SP_DEVINFO_DATA();
                deviceInfoData.cbSize = Marshal.SizeOf(deviceInfoData);
                success = NativeMethods.SetupDiEnumDeviceInfo(deviceInfoSet, 0, ref deviceInfoData);
                success = NativeMethods.SetupDiEnumDeviceInfo(deviceInfoSet, 1,
                                                              ref deviceInfoData); // Checks that we have a unique device

                NativeMethods.SP_PROPCHANGE_PARAMS propChangeParams = new NativeMethods.SP_PROPCHANGE_PARAMS();
                propChangeParams.classInstallHeader.cbSize          = Marshal.SizeOf(propChangeParams.classInstallHeader);
                propChangeParams.classInstallHeader.installFunction = NativeMethods.DIF_PROPERTYCHANGE;
                propChangeParams.stateChange = NativeMethods.DICS_DISABLE;
                propChangeParams.scope       = NativeMethods.DICS_FLAG_GLOBAL;
                propChangeParams.hwProfile   = 0;
                success = NativeMethods.SetupDiSetClassInstallParams(deviceInfoSet, ref deviceInfoData,
                                                                     ref propChangeParams, Marshal.SizeOf(propChangeParams));
                if (!success)
                {
                    return(false);
                }

                success = NativeMethods.SetupDiCallClassInstaller(NativeMethods.DIF_PROPERTYCHANGE, deviceInfoSet,
                                                                  ref deviceInfoData);
                if (!success)
                {
                    return(false);
                }

                propChangeParams.stateChange = NativeMethods.DICS_ENABLE;
                success = NativeMethods.SetupDiSetClassInstallParams(deviceInfoSet, ref deviceInfoData,
                                                                     ref propChangeParams, Marshal.SizeOf(propChangeParams));
                if (!success)
                {
                    return(false);
                }

                success = NativeMethods.SetupDiCallClassInstaller(NativeMethods.DIF_PROPERTYCHANGE, deviceInfoSet,
                                                                  ref deviceInfoData);
                if (!success)
                {
                    return(false);
                }

                NativeMethods.SetupDiDestroyDeviceInfoList(deviceInfoSet);

                return(true);
            }
            catch
            {
                return(false);
            }
        }
Beispiel #2
0
        private static IEnumerable <string> EnumerateHidDevices()
        {
            var hidClass      = HidClassGuid;
            var deviceInfoSet = NativeMethods.SetupDiGetClassDevs(ref hidClass, null, 0, NativeMethods.DIGCF_PRESENT | NativeMethods.DIGCF_DEVICEINTERFACE);

            if (deviceInfoSet.ToInt64() != NativeMethods.INVALID_HANDLE_VALUE)
            {
                var devices        = new List <string>();
                var deviceInfoData = CreateDeviceInfoData();
                var deviceIndex    = 0;

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

                    var deviceInterfaceData = new NativeMethods.SP_DEVICE_INTERFACE_DATA();
                    deviceInterfaceData.cbSize = Marshal.SizeOf(deviceInterfaceData);
                    var deviceInterfaceIndex = 0;

                    while (NativeMethods.SetupDiEnumDeviceInterfaces(deviceInfoSet, 0, ref hidClass, deviceInterfaceIndex, ref deviceInterfaceData))
                    {
                        deviceInterfaceIndex++;
                        var devicePath = GetDevicePath(deviceInfoSet, deviceInterfaceData);
                        if (devices.Any(x => x == devicePath))
                        {
                            continue;
                        }
                        devices.Add(devicePath);
                    }
                }
                NativeMethods.SetupDiDestroyDeviceInfoList(deviceInfoSet);

                foreach (string devicePath in devices)
                {
                    yield return(devicePath);
                }
            }
        }