public static void DisableDeviceByID(string hardwareId, bool disable = true) { ListDevicesAdvanced( action: (IntPtr info, SP_DEVINFO_DATA devdata, Device device) => { SP_CLASSINSTALL_HEADER header = new SP_CLASSINSTALL_HEADER(); header.cbSize = (UInt32)Marshal.SizeOf(header); header.InstallFunction = DIF_PROPERTYCHANGE; SP_PROPCHANGE_PARAMS propchangeparams = new SP_PROPCHANGE_PARAMS(); propchangeparams.ClassInstallHeader = header; propchangeparams.StateChange = disable ? DICS_DISABLE : DICS_ENABLE; propchangeparams.Scope = DICS_FLAG_GLOBAL; propchangeparams.HwProfile = 0; SetupDiSetClassInstallParams(info, ref devdata, ref propchangeparams, (UInt32)Marshal.SizeOf(propchangeparams)); CheckError("SetupDiSetClassInstallParams"); SetupDiChangeState( info, ref devdata); CheckError("SetupDiChangeState"); }, filter: device => device.ID.Contains(hardwareId) ); }
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)); } }
/// <summary> /// Part in charge to really change the device's state /// </summary> /// <param name="info"></param> /// <param name="devdata"></param> /// <param name="status"></param> private static void ChangeReaderStatus(IntPtr info, SP_DEVINFO_DATA devdata, DeviceState status) { string devicepathTemp = GetStringPropertyForDevice(info, devdata, 0); SP_CLASSINSTALL_HEADER header = new SP_CLASSINSTALL_HEADER(); header.cbSize = (UInt32)Marshal.SizeOf(header); header.InstallFunction = DIF_PROPERTYCHANGE; SP_PROPCHANGE_PARAMS propchangeparams = new SP_PROPCHANGE_PARAMS(); propchangeparams.ClassInstallHeader = header; if (status == DeviceState.On || status == DeviceState.Off) { propchangeparams.StateChange = (status == DeviceState.Off) ? DICS_DISABLE : DICS_ENABLE; propchangeparams.Scope = DICS_FLAG_GLOBAL; } else // reset { propchangeparams.StateChange = DICS_PROPCHANGE; propchangeparams.Scope = DICS_FLAG_CONFIGSPECIFIC; } propchangeparams.HwProfile = 0; SetupDiSetClassInstallParams(info, ref devdata, ref propchangeparams, (UInt32)Marshal.SizeOf(propchangeparams)); CheckError("SetupDiSetClassInstallParams"); SetupDiChangeState(info, ref devdata); CheckError("SetupDiChangeState"); }
/// <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); } }
public static void ChangeDeviceState(string filter, bool disable) { IntPtr info = IntPtr.Zero; Guid NullGuid = Guid.Empty; try { info = SetupDiGetClassDevsW(ref NullGuid, null, IntPtr.Zero, DIGCF_ALLCLASSES); CheckError("SetupDiGetClassDevs"); SP_DEVINFO_DATA devdata = new SP_DEVINFO_DATA(); devdata.cbSize = (UInt32)Marshal.SizeOf(devdata); // Get first device matching device criterion. for (uint i = 0;; i++) { SetupDiEnumDeviceInfo(info, i, out devdata); // if no items match filter, throw if (Marshal.GetLastWin32Error() == ERROR_NO_MORE_ITEMS) { CheckError("No device found matching filter.", 0xcffff); } CheckError("SetupDiEnumDeviceInfo"); string devicepath = GetStringPropertyForDevice(info, devdata, 1); // SPDRP_HARDWAREID if (devicepath != null && devicepath.Contains(filter)) { break; } } SP_CLASSINSTALL_HEADER header = new SP_CLASSINSTALL_HEADER(); header.cbSize = (UInt32)Marshal.SizeOf(header); header.InstallFunction = DIF_PROPERTYCHANGE; SP_PROPCHANGE_PARAMS propchangeparams = new SP_PROPCHANGE_PARAMS { ClassInstallHeader = header, StateChange = disable ? DICS_DISABLE : DICS_ENABLE, Scope = DICS_FLAG_GLOBAL, HwProfile = 0 }; SetupDiSetClassInstallParams(info, ref devdata, ref propchangeparams, (UInt32)Marshal.SizeOf(propchangeparams)); CheckError("SetupDiSetClassInstallParams"); SetupDiChangeState(info, ref devdata); //SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, info, ref devdata); CheckError("SetupDiChangeState"); } finally { if (info != IntPtr.Zero) { SetupDiDestroyDeviceInfoList(info); } } }
public static bool removeDevice() { bool result = false; int error = 0; result = CloseHandle(file); error = Marshal.GetLastWin32Error(); SP_REMOVEDEVICE_PARAMS rmdParams = new SP_REMOVEDEVICE_PARAMS(); SP_CLASSINSTALL_HEADER classInstallHeader = new SP_CLASSINSTALL_HEADER(); rmdParams.ClassInstallHeader.cbSize = (uint)Marshal.SizeOf(classInstallHeader); rmdParams.ClassInstallHeader.InstallFunction = DIF_REMOVE; rmdParams.Scope = DI_REMOVEDEVICE_GLOBAL; rmdParams.HwProfile = 0; Guid hidGuid; HidD_GetHidGuid(out hidGuid); IntPtr hardwareDeviceInfo = SetupDiGetClassDevs(ref f3flightGuid, IntPtr.Zero, IntPtr.Zero, 0); SP_DEVICE_INTERFACE_DATA deviceInterfaceData = new SP_DEVICE_INTERFACE_DATA(); deviceInterfaceData.cbSize = (uint)Marshal.SizeOf(deviceInterfaceData); UInt32 i = 0; SP_DEVINFO_DATA devInfoData = new SP_DEVINFO_DATA(); devInfoData.cbSize = (uint)Marshal.SizeOf(devInfoData); while (SetupDiEnumDeviceInfo(hardwareDeviceInfo, i, out devInfoData)) { // SP_DEVICE_INTERFACE_DETAIL_DATA didd = new SP_DEVICE_INTERFACE_DETAIL_DATA(); // if (IntPtr.Size == 8) // for 64 bit operating systems // didd.cbSize = 8; // else // didd.cbSize = 4 + (uint)Marshal.SystemDefaultCharSize; // UInt32 bufferSize = 0; // UInt32 requiredSize = 0; // SP_DEVINFO_DATA devInfoData = new SP_DEVINFO_DATA(); // devInfoData.cbSize = (uint)Marshal.SizeOf(devInfoData); // result = SetupDiGetDeviceInterfaceDetail(hardwareDeviceInfo, ref deviceInterfaceData, IntPtr.Zero, bufferSize, out requiredSize, out devInfoData); // error = Marshal.GetLastWin32Error(); // didd.DevicePath = new string(char.MinValue, 256); // uint nBytes = (uint)didd.DevicePath.Length; // result = SetupDiGetDeviceInterfaceDetail(hardwareDeviceInfo, ref deviceInterfaceData, ref didd, nBytes, out requiredSize, out devInfoData); // if (didd.DevicePath.Contains(ClassName.ToLower())) // { // result = SetupDiSetClassInstallParams(hardwareDeviceInfo, ref devInfoData, ref rmdParams.ClassInstallHeader, (uint)Marshal.SizeOf(rmdParams)); // error = Marshal.GetLastWin32Error(); // result = SetupDiCallClassInstaller(DIF_REMOVE, hardwareDeviceInfo, ref devInfoData); // error = Marshal.GetLastWin32Error(); // } i++; } devInfoData.DevInst = 4276; result = SetupDiSetClassInstallParams(hardwareDeviceInfo, ref devInfoData, ref rmdParams.ClassInstallHeader, (uint)Marshal.SizeOf(rmdParams)); error = Marshal.GetLastWin32Error(); result = SetupDiCallClassInstaller(DIF_REMOVE, hardwareDeviceInfo, ref devInfoData); error = Marshal.GetLastWin32Error(); SetupDiDestroyDeviceInfoList(hardwareDeviceInfo); return(true); }
/// <summary> /// 启用鼠标类设备 /// </summary> public static void EnableMouse() { IntPtr info = IntPtr.Zero; Guid NullGuid = Guid.Empty; try { info = SetupDiGetClassDevsW(ref NullGuid, null, IntPtr.Zero, DIGCF.ALLCLASSES); SP_DEVINFO_DATA devdata = new SP_DEVINFO_DATA(); devdata.cbSize = (UInt32)Marshal.SizeOf(devdata); ///遍历设备 for (uint i = 0; SetupDiEnumDeviceInfo(info, i, out devdata); i++) { if (devdata.ClassGuid == new Guid(Mouse)) { SP_CLASSINSTALL_HEADER header = new SP_CLASSINSTALL_HEADER(); header.cbSize = (UInt32)Marshal.SizeOf(header); header.InstallFunction = DIF.PROPERTYCHANGE; SP_PROPCHANGE_PARAMS propchangeparams = new SP_PROPCHANGE_PARAMS { ClassInstallHeader = header, StateChange = DICS.ENABLE, Scope = DICS_FLAG.GLOBAL, HwProfile = 0 }; SetupDiSetClassInstallParams(info, ref devdata, ref propchangeparams, (UInt32)Marshal.SizeOf(propchangeparams)); SetupDiChangeState(info, ref devdata); } } } catch (Exception ex) { throw new Exception(string.Format("ChangeMouseState failed,the reason is {0}", ex.Message)); } finally { if (info != IntPtr.Zero) { SetupDiDestroyDeviceInfoList(info); } } }
public static extern bool SetupDiSetClassInstallParams(IntPtr DeviceInfoSet, ref SP_DEVINFO_DATA DeviceInfoData, ref SP_CLASSINSTALL_HEADER ClassInstallParams, uint ClassInstallParamsSize);
public static void DisableDevice(Func<string, bool> filter, bool disable = true) { IntPtr info = IntPtr.Zero; Guid NullGuid = Guid.Empty; try { info = SetupDiGetClassDevsW( ref NullGuid, null, IntPtr.Zero, DIGCF_ALLCLASSES); CheckError("SetupDiGetClassDevs"); SP_DEVINFO_DATA devdata = new SP_DEVINFO_DATA(); devdata.cbSize = (UInt32)Marshal.SizeOf(devdata); // Get first device matching device criterion. for (uint i = 0; ; i++) { SetupDiEnumDeviceInfo(info, i, out devdata); // if no items match filter, throw if (Marshal.GetLastWin32Error() == ERROR_NO_MORE_ITEMS) CheckError("No device found matching filter.", 0xcffff); CheckError("SetupDiEnumDeviceInfo"); string devicepath = GetStringPropertyForDevice(info, devdata, 1); // SPDRP_HARDWAREID // Uncomment to print name/path //Console.WriteLine(GetStringPropertyForDevice(info, // devdata, DEVPKEY_Device_DeviceDesc)); //Console.WriteLine(" {0}", devicepath); if (devicepath != null && filter(devicepath)) break; } SP_CLASSINSTALL_HEADER header = new SP_CLASSINSTALL_HEADER(); header.cbSize = (UInt32)Marshal.SizeOf(header); header.InstallFunction = DIF_PROPERTYCHANGE; SP_PROPCHANGE_PARAMS propchangeparams = new SP_PROPCHANGE_PARAMS(); propchangeparams.ClassInstallHeader = header; propchangeparams.StateChange = disable ? DICS_DISABLE : DICS_ENABLE; propchangeparams.Scope = DICS_FLAG_GLOBAL; propchangeparams.HwProfile = 0; SetupDiSetClassInstallParams(info, ref devdata, ref propchangeparams, (UInt32)Marshal.SizeOf(propchangeparams)); CheckError("SetupDiSetClassInstallParams"); SetupDiChangeState( info, ref devdata); CheckError("SetupDiChangeState"); } finally { if (info != IntPtr.Zero) SetupDiDestroyDeviceInfoList(info); } }
public static void SetDeviceState(string instanceId, DICS state, DeviceClass?deviceClass = null) { SafeDeviceInfoSetHandle diSetHandle = null; try { GCHandle guid = new GCHandle(); if (deviceClass.HasValue) { var classGuid = Device.DeviceClassGuids[(int)deviceClass]; guid = GCHandle.Alloc(classGuid, GCHandleType.Pinned); diSetHandle = NativeMethods.SetupDiGetClassDevs(guid.AddrOfPinnedObject(), null, IntPtr.Zero, SetupDiGetClassDevsFlags.Null); } else { diSetHandle = NativeMethods.SetupDiGetClassDevs(IntPtr.Zero, null, IntPtr.Zero, SetupDiGetClassDevsFlags.AllClasses); } if (diSetHandle.IsInvalid) { throw new Win32Exception(Marshal.GetLastWin32Error(), "Error calling SetupDiGetClassDevs"); } //Get the device information data for each matching device. var diData = GetDeviceInfoData(diSetHandle); if (!string.IsNullOrEmpty(instanceId)) { // Find the index of of the instance int index = GetIndexOfInstance(diSetHandle, diData, instanceId); if (index == -1) { throw new IndexOutOfRangeException(string.Format("The device '{0}' could not be found", instanceId)); } diData = new SP_DEVINFO_DATA[] { diData[index] }; } for (int i = 0; i < diData.Length; i++) { SP_CLASSINSTALL_HEADER installHeader = default(SP_CLASSINSTALL_HEADER); installHeader.InstallFunction = DIF.DIF_PROPERTYCHANGE; installHeader.cbSize = (uint)Marshal.SizeOf(installHeader); SP_PROPCHANGE_PARAMS propertyChangeParameters = default(SP_PROPCHANGE_PARAMS); propertyChangeParameters.ClassInstallHeader = installHeader; propertyChangeParameters.HwProfile = 0; propertyChangeParameters.Scope = DICS.DICS_FLAG_CONFIGSPECIFIC; propertyChangeParameters.StateChange = state; if (NativeMethods.SetupDiSetClassInstallParams(diSetHandle, ref diData[i], ref propertyChangeParameters, (uint)Marshal.SizeOf(propertyChangeParameters))) { if (!NativeMethods.SetupDiCallClassInstaller(DIF.DIF_PROPERTYCHANGE, diSetHandle, ref diData[i])) { throw new Win32Exception(Marshal.GetLastWin32Error(), "Error calling SetupDiCallClassInstaller"); } } } } finally { if (diSetHandle != null) { if (diSetHandle.IsClosed == false) { diSetHandle.Close(); } diSetHandle.Dispose(); } } }
public static void DisableDevice(Func <string, bool> filter, bool disable = true) { IntPtr info = IntPtr.Zero; Guid NullGuid = Guid.Empty; try { info = SetupDiGetClassDevsW( ref NullGuid, null, IntPtr.Zero, DIGCF_ALLCLASSES); CheckError("SetupDiGetClassDevs"); SP_DEVINFO_DATA devdata = new SP_DEVINFO_DATA(); devdata.cbSize = (UInt32)Marshal.SizeOf(devdata); // Get first device matching device criterion. for (uint i = 0; ; i++) { SetupDiEnumDeviceInfo(info, i, out devdata); // if no items match filter, throw if (Marshal.GetLastWin32Error() == ERROR_NO_MORE_ITEMS) { CheckError("No device found matching filter.", 0xcffff); } CheckError("SetupDiEnumDeviceInfo"); // By Hardware ID //string devicepath = GetStringPropertyForDevice(info, devdata, 1); // SPDRP_HARDWAREID // By Instance Path string devicepath = GetDeviceInstanceId(info, devdata); // Uncomment to print name/path //Console.WriteLine(GetStringPropertyForDevice(info, // devdata, DEVPKEY_Device_DeviceDesc)); //Console.WriteLine(" {0}", devicepath); if (devicepath != null && filter(devicepath)) { break; } } SP_CLASSINSTALL_HEADER header = new SP_CLASSINSTALL_HEADER(); header.cbSize = (UInt32)Marshal.SizeOf(header); header.InstallFunction = DIF_PROPERTYCHANGE; SP_PROPCHANGE_PARAMS propchangeparams = new SP_PROPCHANGE_PARAMS(); propchangeparams.ClassInstallHeader = header; propchangeparams.StateChange = disable ? DICS_DISABLE : DICS_ENABLE; propchangeparams.Scope = DICS_FLAG_GLOBAL; propchangeparams.HwProfile = 0; SetupDiSetClassInstallParams(info, ref devdata, ref propchangeparams, (UInt32)Marshal.SizeOf(propchangeparams)); CheckError("SetupDiSetClassInstallParams"); SetupDiChangeState( info, ref devdata); CheckError("SetupDiChangeState"); } finally { if (info != IntPtr.Zero) { SetupDiDestroyDeviceInfoList(info); } } }
public void Init() { ClassInstallHeader = new SP_CLASSINSTALL_HEADER(); }
public static Win32Exception RemoveDevice(string deviceId, int method, out bool needReboot) { Guid classGuid = System.Guid.Empty; IntPtr deviceInfoSet = SetupDiGetClassDevs(classGuid, null, IntPtr.Zero, DIGCF.DIGCF_ALLCLASSES); Win32Exception ex = null; needReboot = false; var success = false; if (deviceInfoSet.ToInt32() != ERROR_INVALID_HANDLE_VALUE) { var deviceInfoData = GetDeviceInfo(deviceInfoSet, deviceId); if (deviceInfoData.HasValue) { var di = deviceInfoData.Value; switch (method) { case 1: SP_CLASSINSTALL_HEADER header = new SP_CLASSINSTALL_HEADER(); header.cbSize = (UInt32)Marshal.SizeOf(header); header.InstallFunction = DIF_REMOVE; var classInstallParams = new SP_REMOVEDEVICE_PARAMS(); classInstallParams.ClassInstallHeader = header; classInstallParams.Scope = DICS_FLAG_GLOBAL; classInstallParams.HwProfile = 0; var classInstallParamsSize = (UInt32)Marshal.SizeOf(classInstallParams); success = SetupDiSetClassInstallParams(deviceInfoSet, ref di, classInstallParams, classInstallParamsSize); if (success) { success = SetupDiSetSelectedDevice(deviceInfoSet, ref di); if (success) { success = SetupDiCallClassInstaller(DIF_REMOVE, deviceInfoSet, ref di); // ex.ErrorCode = 0xE0000235: SetupDiCallClassInstaller throws ERROR_IN_WOW64 when compiled for 32 bit on a 64 bit machine. if (!success) { ex = new Win32Exception(); } } else { ex = new Win32Exception(); } } else { ex = new Win32Exception(); } break; case 2: success = SetupDiRemoveDevice(deviceInfoSet, ref di); if (!success) { ex = new Win32Exception(); } break; case 3: success = DiUninstallDevice(IntPtr.Zero, deviceInfoSet, ref di, 0, out needReboot); if (!success) { ex = new Win32Exception(); } break; default: break; } } SetupDiDestroyDeviceInfoList(deviceInfoSet); } else { ex = new Win32Exception(); } return(ex); }
/// <summary> /// 通过设备唯一标识PnpDeviceID,启用设备 /// </summary> /// <param name="pnpDeviceId"></param> /// <returns></returns> public static bool EnableDeviceByPnpDeviceId(string pnpDeviceId) { IntPtr info = IntPtr.Zero; Guid NullGuid = Guid.Empty; bool isSuccess = false; try { info = SetupDiGetClassDevsW(ref NullGuid, pnpDeviceId, IntPtr.Zero, DIGCF.DEVICEINTERFACE); SP_DEVINFO_DATA devdata = new SP_DEVINFO_DATA(); devdata.cbSize = (UInt32)Marshal.SizeOf(devdata); ///遍历设备 for (uint i = 0; SetupDiEnumDeviceInfo(info, i, out devdata); i++) { int size = 512; StringBuilder stringBuilder = new StringBuilder(size); uint result = Cfgmgr32.Cfgmgr32Base.CM_Get_Device_ID(devdata.DevInst, stringBuilder, size); if (result == 0) { string currentPnpDeviceID = stringBuilder.ToString(); if (currentPnpDeviceID == pnpDeviceId) { SP_CLASSINSTALL_HEADER header = new SP_CLASSINSTALL_HEADER(); header.cbSize = (UInt32)Marshal.SizeOf(header); header.InstallFunction = DIF.PROPERTYCHANGE; SP_PROPCHANGE_PARAMS propchangeparams = new SP_PROPCHANGE_PARAMS { ClassInstallHeader = header, StateChange = DICS.ENABLE, Scope = DICS_FLAG.GLOBAL, HwProfile = 0 }; SetupDiSetClassInstallParams(info, ref devdata, ref propchangeparams, (UInt32)Marshal.SizeOf(propchangeparams)); SetupDiChangeState(info, ref devdata); isSuccess = true; } } continue; } return(isSuccess); } catch (Exception ex) { Console.WriteLine(string.Format("EnableDeviceByPnpDeviceId failed,the reason is {0}", ex.Message)); return(isSuccess); } finally { if (info != IntPtr.Zero) { SetupDiDestroyDeviceInfoList(info); } } }
public static void DisableDevice(Func <string, bool> filter, bool disable = true, bool toggle = false) { IntPtr info = IntPtr.Zero; Guid NullGuid = Guid.Empty; try { info = SetupDiGetClassDevs( ref NullGuid, null, IntPtr.Zero, DIGCF_ALLCLASSES); CheckError("SetupDiGetClassDevs"); // Get first device matching device criterion. SP_DEVINFO_DATA devdata; for (uint i = 0; ; i++) { devdata = new SP_DEVINFO_DATA(); devdata.cbSize = (UInt32)Marshal.SizeOf(devdata); SetupDiEnumDeviceInfo(info, i, out devdata); if (Marshal.GetLastWin32Error() == ERROR_NO_MORE_ITEMS) { throw new ApplicationException("No device matching that Hardware Id found."); } CheckError("SetupDiEnumDeviceInfo"); var hardwareIds = GetStringPropertyForDevice(info, devdata, propId: SPDRP.SPDRP_HARDWAREID); if (hardwareIds != null && filter(hardwareIds)) { try { SP_CLASSINSTALL_HEADER header = new SP_CLASSINSTALL_HEADER(); header.cbSize = (UInt32)Marshal.SizeOf(header); header.InstallFunction = DIF_PROPERTYCHANGE; SP_PROPCHANGE_PARAMS propchangeparams = new SP_PROPCHANGE_PARAMS(); propchangeparams.ClassInstallHeader = header; propchangeparams.StateChange = disable ? DICS_DISABLE : DICS_ENABLE; propchangeparams.Scope = DICS_FLAG_GLOBAL; propchangeparams.HwProfile = 0; SetupDiSetClassInstallParams(info, ref devdata, ref propchangeparams, (UInt32)Marshal.SizeOf(propchangeparams)); CheckError("SetupDiSetClassInstallParams"); SetupDiChangeState( info, ref devdata); CheckError("SetupDiChangeState"); if (toggle) { propchangeparams.StateChange = !disable ? DICS_DISABLE : DICS_ENABLE; SetupDiSetClassInstallParams(info, ref devdata, ref propchangeparams, (UInt32)Marshal.SizeOf(propchangeparams)); CheckError("SetupDiSetClassInstallParams"); SetupDiChangeState( info, ref devdata); CheckError("SetupDiChangeState"); } break; } catch { } } } } finally { if (info != IntPtr.Zero) { SetupDiDestroyDeviceInfoList(info); } } }
public static Exception RemoveDevice(string deviceId, int method, out bool needReboot) { Exception ex = null; needReboot = false; bool needRebootAny = false; var success = false; try { _EnumDeviceInfo(null, null, deviceId, (infoSet, infoData) => { switch (method) { case 1: var header = new SP_CLASSINSTALL_HEADER(); header.cbSize = (uint)Marshal.SizeOf(header); header.InstallFunction = DIF_REMOVE; var classInstallParams = new SP_REMOVEDEVICE_PARAMS { ClassInstallHeader = header, Scope = DICS_FLAG_GLOBAL, HwProfile = 0 }; var classInstallParamsSize = (uint)Marshal.SizeOf(classInstallParams); success = NativeMethods.SetupDiSetClassInstallParams(infoSet, ref infoData, classInstallParams, classInstallParamsSize); if (success) { success = NativeMethods.SetupDiSetSelectedDevice(infoSet, ref infoData); if (success) { success = NativeMethods.SetupDiCallClassInstaller(DIF_REMOVE, infoSet, ref infoData); // ex.ErrorCode = 0xE0000235: SetupDiCallClassInstaller throws ERROR_IN_WOW64 when compiled for 32 bit on a 64 bit machine. // Most of the SetupDi APIs run fine in a WOW64 process, but co-installer have to run from 64-bit process. if (!success) { ex = new Win32Exception(); } } else { ex = new Win32Exception(); } } else { ex = new Win32Exception(); } break; case 2: success = NativeMethods.SetupDiRemoveDevice(infoSet, ref infoData); if (!success) { ex = new Win32Exception(); } break; case 3: bool _needReboot; success = NativeMethods.DiUninstallDevice(IntPtr.Zero, infoSet, ref infoData, 0, out _needReboot); needRebootAny |= _needReboot; if (!success) { ex = new Win32Exception(); } break; default: break; } return(true); }); needReboot = needRebootAny; } catch (Exception ex2) { ex = ex2; } return(ex); }
public void DisableDevice(Func <string, bool> filter, bool disable = true, bool toggle = false) { IntPtr info = IntPtr.Zero; Guid NullGuid = Guid.Empty; try { info = SetupDiGetClassDevs( ref NullGuid, null, IntPtr.Zero, DIGCF_ALLCLASSES); CheckError("SetupDiGetClassDevs"); // Get first device matching device criterion. SP_DEVINFO_DATA devdata; for (uint i = 0; ; i++) { devdata = new SP_DEVINFO_DATA(); devdata.cbSize = (UInt32)Marshal.SizeOf(devdata); SetupDiEnumDeviceInfo(info, i, out devdata); if (Marshal.GetLastWin32Error() == ERROR_NO_MORE_ITEMS) { break; } CheckError("SetupDiEnumDeviceInfo"); //var hardwareIds = GetStringPropertyForDevice(info, devdata, propId: SPDRP.SPDRP_HARDWAREID); var devId = GetDeviceId(devdata.devInst); if (devId == null || !filter(devId)) { continue; } // Skip devices that aren't working with no issues (e.g. disconnected devices) uint status; uint probNum; var succ = CM_Get_DevNode_Status(out status, out probNum, devdata.devInst, 0); if (succ != 0) { continue; } SP_CLASSINSTALL_HEADER header = new SP_CLASSINSTALL_HEADER(); header.cbSize = (UInt32)Marshal.SizeOf(header); header.InstallFunction = DIF_PROPERTYCHANGE; SP_PROPCHANGE_PARAMS propchangeparams = new SP_PROPCHANGE_PARAMS(); propchangeparams.ClassInstallHeader = header; propchangeparams.StateChange = disable ? DICS_DISABLE : DICS_ENABLE; propchangeparams.Scope = DICS_FLAG_GLOBAL; propchangeparams.HwProfile = 0; SetupDiSetClassInstallParams(info, ref devdata, ref propchangeparams, (UInt32)Marshal.SizeOf(propchangeparams)); CheckError("SetupDiSetClassInstallParams"); SetupDiChangeState( info, ref devdata); CheckError("SetupDiChangeState"); if (toggle) { propchangeparams.StateChange = !disable ? DICS_DISABLE : DICS_ENABLE; SetupDiSetClassInstallParams(info, ref devdata, ref propchangeparams, (UInt32)Marshal.SizeOf(propchangeparams)); CheckError("SetupDiSetClassInstallParams"); SetupDiChangeState( info, ref devdata); CheckError("SetupDiChangeState"); } } } finally { if (info != IntPtr.Zero) { SetupDiDestroyDeviceInfoList(info); } } }
/// <summary> /// Part in charge to really change the device's state /// </summary> /// <param name="info"></param> /// <param name="devdata"></param> /// <param name="status"></param> private static void ChangeReaderStatus(IntPtr info, SP_DEVINFO_DATA devdata, DeviceState status) { string devicepathTemp = GetStringPropertyForDevice(info, devdata, 0); SP_CLASSINSTALL_HEADER header = new SP_CLASSINSTALL_HEADER(); header.cbSize = (UInt32)Marshal.SizeOf(header); header.InstallFunction = DIF_PROPERTYCHANGE; SP_PROPCHANGE_PARAMS propchangeparams = new SP_PROPCHANGE_PARAMS(); propchangeparams.ClassInstallHeader = header; if(status == DeviceState.On || status == DeviceState.Off) { propchangeparams.StateChange = (status == DeviceState.Off) ? DICS_DISABLE : DICS_ENABLE; propchangeparams.Scope = DICS_FLAG_GLOBAL; } else // reset { propchangeparams.StateChange = DICS_PROPCHANGE; propchangeparams.Scope = DICS_FLAG_CONFIGSPECIFIC; } propchangeparams.HwProfile = 0; SetupDiSetClassInstallParams(info, ref devdata, ref propchangeparams, (UInt32)Marshal.SizeOf(propchangeparams)); CheckError("SetupDiSetClassInstallParams"); SetupDiChangeState(info, ref devdata); CheckError("SetupDiChangeState"); }