/// <summary> /// Attempts to enable or disable a device driver. /// </summary> /// <param name="deviceInfoSet">Pointer to device.</param> /// <param name="deviceInfoData"></param> /// <param name="bEnable"></param> /// <returns>State of success.</returns> /// <remarks> /// IMPORTANT NOTE!!! /// This code currently does not check the reboot flag. /// Some devices require you reboot the OS for the change /// to take affect. If this describes your device, you /// will need to look at the SDK call: /// SetupDiGetDeviceInstallParams. You can call it /// directly after ChangeIt to see whether or not you need /// to reboot the OS for you change to go into effect. /// Errors: This method may throw the following exceptions. /// Unable to change device state! /// </remarks> private static bool SetDeviceState(IntPtr deviceInfoSet, SP_DEVINFO_DATA deviceInfoData, bool bEnable) { try { SP_CLASSINSTALL_HEADER header = new SP_CLASSINSTALL_HEADER(); header.cbSize = (UInt32)Marshal.SizeOf(header); header.InstallFunction = DIF_PROPERTYCHANGE; SP_PROPCHANGE_PARAMS classInstallParams = new SP_PROPCHANGE_PARAMS(); classInstallParams.ClassInstallHeader = header; classInstallParams.StateChange = bEnable ? DICS_ENABLE : DICS_DISABLE; classInstallParams.Scope = DICS_FLAG_GLOBAL; classInstallParams.HwProfile = 0; var classInstallParamsSize = (UInt32)Marshal.SizeOf(classInstallParams); bool result = SetupDiSetClassInstallParams(deviceInfoSet, ref deviceInfoData, classInstallParams, classInstallParamsSize); if (result) result = SetupDiChangeState(deviceInfoSet, ref deviceInfoData); if (!result) { var ex = new Win32Exception(); SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, deviceInfoSet, ref deviceInfoData); } return result; } catch (Exception) { return false; } }
static extern bool SetupDiSetClassInstallParams(IntPtr DeviceInfoSet, ref SP_DEVINFO_DATA DeviceInfoData, SP_PROPCHANGE_PARAMS ClassInstallParams, UInt32 ClassInstallParamsSize);
public static extern bool SetupDiSetClassInstallParams(IntPtr deviceInfoSet, ref SP_DEVINFO_DATA deviceInfoData, SP_PROPCHANGE_PARAMS classInstallParams, UInt32 classInstallParamsSize);