public static void EnableDevice(Func <string, bool> hardwareIdFilter, bool enable)
    {
        Guid nullGuid = Guid.Empty;

        using (SafeDeviceInformationSetHandle infoSet = SetupDiGetClassDevs(ref nullGuid, null, IntPtr.Zero, DIGCF.ALLCLASSES))
        {
            CheckWin32CallSuccess(!infoSet.IsInvalid);
            SP_DEVINFO_DATA devInfo = new SP_DEVINFO_DATA();
            devInfo.cbSize = (UInt32)Marshal.SizeOf(devInfo);
            for (uint index = 0; ; ++index)
            {
                CheckWin32CallSuccess(SetupDiEnumDeviceInfo(infoSet, index, ref devInfo));
                string hardwareId = GetStringPropertyForDevice(infoSet, ref devInfo, SPDRP.HARDWAREID);

                if ((!string.IsNullOrEmpty(hardwareId)) && (hardwareIdFilter(hardwareId)))
                {
                    break;
                }
            }
            SP_CLASSINSTALL_HEADER classinstallHeader = new SP_CLASSINSTALL_HEADER();
            classinstallHeader.cbSize          = (UInt32)Marshal.SizeOf(classinstallHeader);
            classinstallHeader.InstallFunction = DIF.PROPERTYCHANGE;
            SP_PROPCHANGE_PARAMS propchangeParams = new SP_PROPCHANGE_PARAMS
            {
                ClassInstallHeader = classinstallHeader,
                StateChange        = enable ? DICS.ENABLE : DICS.DISABLE,
                Scope     = DICS_FLAG.GLOBAL,
                HwProfile = 0,
            };
            CheckWin32CallSuccess(SetupDiSetClassInstallParams(infoSet, ref devInfo, ref propchangeParams, (UInt32)Marshal.SizeOf(propchangeParams)));
            CheckWin32CallSuccess(SetupDiChangeState(infoSet, ref devInfo));
        }
    }
Esempio n. 2
0
        private static string GetStringPropertyForDevice(SafeDeviceInformationSetHandle infoSet, ref SP_DEVINFO_DATA devInfo, SPDRP property)
        {
            RegistryValueKind regType;
            UInt32            requiredSize;

            if (!SetupDiGetDeviceRegistryProperty(infoSet, ref devInfo, property, out regType, null, 0, out requiredSize))
            {
                switch (Marshal.GetLastWin32Error())
                {
                case ERROR_INSUFFICIENT_BUFFER:
                    break;

                case ERROR_INVALID_DATA:
                    return(string.Empty);

                default:
                    throw new Win32Exception();
                }
            }

            byte[] propertyBuffer = new byte[requiredSize];
            CheckWin32CallSuccess(SetupDiGetDeviceRegistryProperty(infoSet, ref devInfo, property, out regType, propertyBuffer, (uint)propertyBuffer.Length, out requiredSize));

            return(Encoding.Unicode.GetString(propertyBuffer));
        }
 private static extern bool SetupDiGetDeviceRegistryProperty(
     SafeDeviceInformationSetHandle DeviceInfoSet,
     [In] ref SP_DEVINFO_DATA DeviceInfoData,
     SPDRP Property,
     out RegistryValueKind PropertyRegDataType,
     [Out] byte[] PropertyBuffer,
     UInt32 PropertyBufferSize,
     out UInt32 RequiredSize
     );
 private static extern bool SetupDiChangeState(
     SafeDeviceInformationSetHandle DeviceInfoSet,
     [In, Out] ref SP_DEVINFO_DATA DeviceInfoData
     );
 private static extern bool SetupDiSetClassInstallParams(
     SafeDeviceInformationSetHandle DeviceInfoSet,
     [In] ref SP_DEVINFO_DATA deviceInfoData,
     [In] ref SP_PROPCHANGE_PARAMS classInstallParams,
     UInt32 ClassInstallParamsSize
     );
 private static extern bool SetupDiEnumDeviceInfo(
     SafeDeviceInformationSetHandle DeviceInfoSet,
     UInt32 MemberIndex,
     ref SP_DEVINFO_DATA DeviceInfoData
     );