/// <summary> /// Removes all devices with a matching hardware ID /// </summary> /// <param name="hardwareID">ComponenetID</param> /// <returns></returns> public static bool UninstallDevice(string hardwareID) { if (IntPtr.Size == 4 && Environment.Is64BitOperatingSystem) { MessageBox.Show("This process is 32-bit but the OS is 64-bit. Only 64-bit processes can uninstall 64-bit direvers.", "Driver Setup", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button2, MessageBoxOptions.ServiceNotification); return(false); } IntPtr deviceInfoSet = SetupDiGetClassDevsEx(IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, DIGCF_ALLCLASSES, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); if (deviceInfoSet == Kernel32.INVALID_HANDLE_VALUE) { return(false); } //SP_DEVINFO_LIST_DETAIL_DATA deviceInfoListDetailData = new SP_DEVINFO_LIST_DETAIL_DATA(); //if (Environment.Is64BitProcess) // deviceInfoListDetailData.cbSize = 560; //else // deviceInfoListDetailData.cbSize = 550; //if (!SetupDiGetDeviceInfoListDetail(deviceInfoSet, ref deviceInfoListDetailData)) // return false; uint index = 0; SP_DEVINFO_DATA deviceInfoData = new SP_DEVINFO_DATA(); deviceInfoData.cbSize = (uint)Marshal.SizeOf(deviceInfoData); while (SetupDiEnumDeviceInfo(deviceInfoSet, index, ref deviceInfoData)) { index++; uint requiredSize = 0; SetupDiGetDeviceRegistryProperty(deviceInfoSet, ref deviceInfoData, SPDRP_HARDWAREID, IntPtr.Zero, IntPtr.Zero, 0, ref requiredSize); if (requiredSize == 0) { continue; } IntPtr pPropertyBuffer = Marshal.AllocHGlobal((int)requiredSize); if (!SetupDiGetDeviceRegistryProperty(deviceInfoSet, ref deviceInfoData, SPDRP_HARDWAREID, IntPtr.Zero, pPropertyBuffer, requiredSize, IntPtr.Zero)) { continue; } string currHardwareID = Marshal.PtrToStringAuto(pPropertyBuffer); Marshal.FreeHGlobal(pPropertyBuffer); if (currHardwareID == hardwareID) { SP_REMOVEDEVICE_PARAMS classInstallParams = new SP_REMOVEDEVICE_PARAMS(); classInstallParams.ClassInstallHeader.cbSize = (uint)Marshal.SizeOf(classInstallParams.ClassInstallHeader); classInstallParams.ClassInstallHeader.InstallFunction = DI_FUNCTION.DIF_REMOVE; classInstallParams.Scope = DI_REMOVEDEVICE_GLOBAL; if (!SetupDiSetClassInstallParams(deviceInfoSet, ref deviceInfoData, ref classInstallParams, (uint)Marshal.SizeOf(classInstallParams))) { return(false); } if (!SetupDiCallClassInstaller(DI_FUNCTION.DIF_REMOVE, deviceInfoSet, ref deviceInfoData)) { return(false); } } } SetupDiDestroyDeviceInfoList(deviceInfoSet); return(true); }
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); }
public static bool Remove(Guid classGuid, string path, string instanceId) { var deviceInfoSet = IntPtr.Zero; try { var deviceInterfaceData = new SP_DEVINFO_DATA(); deviceInterfaceData.cbSize = Marshal.SizeOf(deviceInterfaceData); deviceInfoSet = SetupDiGetClassDevs(ref classGuid, IntPtr.Zero, IntPtr.Zero, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); if (SetupDiOpenDeviceInfo(deviceInfoSet, instanceId, IntPtr.Zero, 0, ref deviceInterfaceData)) { var props = new SP_REMOVEDEVICE_PARAMS(); props.ClassInstallHeader = new SP_CLASSINSTALL_HEADER(); props.ClassInstallHeader.cbSize = Marshal.SizeOf(props.ClassInstallHeader); props.ClassInstallHeader.InstallFunction = DIF_REMOVE; props.Scope = DI_REMOVEDEVICE_GLOBAL; props.HwProfile = 0x00; if (SetupDiSetClassInstallParams(deviceInfoSet, ref deviceInterfaceData, ref props, Marshal.SizeOf(props))) { return(SetupDiCallClassInstaller(DIF_REMOVE, deviceInfoSet, ref deviceInterfaceData)); } } } catch (Exception ex) { throw ex; } finally { if (deviceInfoSet != IntPtr.Zero) { SetupDiDestroyDeviceInfoList(deviceInfoSet); } } return(false); }
public static Boolean Remove(Guid ClassGuid, String Path, String InstanceId) { IntPtr deviceInfoSet = IntPtr.Zero; try { SP_DEVINFO_DATA deviceInterfaceData = new SP_DEVINFO_DATA(); deviceInterfaceData.cbSize = Marshal.SizeOf(deviceInterfaceData); deviceInfoSet = SetupDiGetClassDevs(ref ClassGuid, IntPtr.Zero, IntPtr.Zero, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); if (SetupDiOpenDeviceInfo(deviceInfoSet, InstanceId, IntPtr.Zero, 0, ref deviceInterfaceData)) { SP_REMOVEDEVICE_PARAMS props = new SP_REMOVEDEVICE_PARAMS(); props.ClassInstallHeader = new SP_CLASSINSTALL_HEADER(); props.ClassInstallHeader.cbSize = Marshal.SizeOf(props.ClassInstallHeader); props.ClassInstallHeader.InstallFunction = DIF_REMOVE; props.Scope = DI_REMOVEDEVICE_GLOBAL; props.HwProfile = 0x00; if (SetupDiSetClassInstallParams(deviceInfoSet, ref deviceInterfaceData, ref props, Marshal.SizeOf(props))) { return(SetupDiCallClassInstaller(DIF_REMOVE, deviceInfoSet, ref deviceInterfaceData)); } } } catch (Exception ex) { Console.WriteLine("{0} {1}", ex.HelpLink, ex.Message); throw; } finally { if (deviceInfoSet != IntPtr.Zero) { SetupDiDestroyDeviceInfoList(deviceInfoSet); } } return(false); }
public static bool DeviceRemove(Guid classGuid, string instanceId) { IntPtr deviceInfoList = IntPtr.Zero; try { SP_DEVICE_INFO_DATA deviceInfoData = new SP_DEVICE_INFO_DATA(); deviceInfoData.cbSize = Marshal.SizeOf(deviceInfoData); //Get device information deviceInfoList = SetupDiGetClassDevs(classGuid, null, IntPtr.Zero, DiGetClassFlag.DIGCF_DEVICEINTERFACE); if (!SetupDiOpenDeviceInfo(deviceInfoList, instanceId, IntPtr.Zero, 0, ref deviceInfoData)) { Debug.WriteLine("SetupDi: Failed getting device info."); return(false); } SP_REMOVEDEVICE_PARAMS removeParams = new SP_REMOVEDEVICE_PARAMS(); removeParams.classInstallHeader = new SP_CLASSINSTALL_HEADER(); removeParams.classInstallHeader.cbSize = Marshal.SizeOf(removeParams.classInstallHeader); removeParams.classInstallHeader.installFunction = DiFunction.DIF_REMOVE; removeParams.removeDevice = DiRemoveDevice.DI_REMOVEDEVICE_GLOBAL; if (SetupDiSetClassInstallParams(deviceInfoList, ref deviceInfoData, ref removeParams, Marshal.SizeOf(removeParams))) { return(SetupDiCallClassInstaller(DiFunction.DIF_REMOVE, deviceInfoList, ref deviceInfoData)); } return(false); } catch (Exception ex) { Debug.WriteLine("Failed to remove device: " + ex.Message); return(false); } finally { if (deviceInfoList != IntPtr.Zero) { SetupDiDestroyDeviceInfoList(deviceInfoList); } } }
public static bool Remove(Guid classGuid, string path, string instanceId) { var deviceInfoSet = IntPtr.Zero; try { var deviceInterfaceData = new SP_DEVINFO_DATA(); deviceInterfaceData.cbSize = Marshal.SizeOf(deviceInterfaceData); deviceInfoSet = SetupDiGetClassDevs(ref classGuid, IntPtr.Zero, IntPtr.Zero, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); if (SetupDiOpenDeviceInfo(deviceInfoSet, instanceId, IntPtr.Zero, 0, ref deviceInterfaceData)) { var props = new SP_REMOVEDEVICE_PARAMS(); props.ClassInstallHeader = new SP_CLASSINSTALL_HEADER(); props.ClassInstallHeader.cbSize = Marshal.SizeOf(props.ClassInstallHeader); props.ClassInstallHeader.InstallFunction = DIF_REMOVE; props.Scope = DI_REMOVEDEVICE_GLOBAL; props.HwProfile = 0x00; if (SetupDiSetClassInstallParams(deviceInfoSet, ref deviceInterfaceData, ref props, Marshal.SizeOf(props))) { return SetupDiCallClassInstaller(DIF_REMOVE, deviceInfoSet, ref deviceInterfaceData); } } } catch (Exception ex) { throw ex; } finally { if (deviceInfoSet != IntPtr.Zero) { SetupDiDestroyDeviceInfoList(deviceInfoSet); } } return false; }
protected static extern bool SetupDiSetClassInstallParams(IntPtr DeviceInfoSet, ref SP_DEVINFO_DATA DeviceInterfaceData, ref SP_REMOVEDEVICE_PARAMS ClassInstallParams, int ClassInstallParamsSize);
/// <summary> /// 开启或者关闭指定的设备驱动 /// 注意:该方法目前没有检查是否需要重新启动计算机。^.^ /// </summary> /// <param name="hDevInfo"></param> /// <param name="devInfoData"></param> /// <param name="bEnable"></param> /// <returns></returns> private bool OpenClose(IntPtr hDevInfo, Externs.SP_DEVINFO_DATA devInfoData, bool bEnable, bool isRemove = false) { try { int szOfPcp; IntPtr ptrToPcp; int szDevInfoData; IntPtr ptrToDevInfoData; Externs.SP_PROPCHANGE_PARAMS SP_PROPCHANGE_PARAMS1 = new Externs.SP_PROPCHANGE_PARAMS(); SP_REMOVEDEVICE_PARAMS SP_REMOVEDEVICE_PARAMS1 = new SP_REMOVEDEVICE_PARAMS(); if (bEnable) { SP_PROPCHANGE_PARAMS1.ClassInstallHeader.cbSize = Marshal.SizeOf(typeof(Externs.SP_CLASSINSTALL_HEADER)); SP_PROPCHANGE_PARAMS1.ClassInstallHeader.InstallFunction = Externs.DIF_PROPERTYCHANGE; SP_PROPCHANGE_PARAMS1.StateChange = Externs.DICS_ENABLE; SP_PROPCHANGE_PARAMS1.Scope = Externs.DICS_FLAG_GLOBAL; SP_PROPCHANGE_PARAMS1.HwProfile = 0; szOfPcp = Marshal.SizeOf(SP_PROPCHANGE_PARAMS1); ptrToPcp = Marshal.AllocHGlobal(szOfPcp); Marshal.StructureToPtr(SP_PROPCHANGE_PARAMS1, ptrToPcp, true); szDevInfoData = Marshal.SizeOf(devInfoData); ptrToDevInfoData = Marshal.AllocHGlobal(szDevInfoData); if (Externs.SetupDiSetClassInstallParams(hDevInfo, ptrToDevInfoData, ptrToPcp, Marshal.SizeOf(typeof(Externs.SP_PROPCHANGE_PARAMS)))) { Externs.SetupDiCallClassInstaller(Externs.DIF_PROPERTYCHANGE, hDevInfo, ptrToDevInfoData); } SP_PROPCHANGE_PARAMS1.ClassInstallHeader.cbSize = Marshal.SizeOf(typeof(Externs.SP_CLASSINSTALL_HEADER)); SP_PROPCHANGE_PARAMS1.ClassInstallHeader.InstallFunction = Externs.DIF_PROPERTYCHANGE; SP_PROPCHANGE_PARAMS1.StateChange = Externs.DICS_ENABLE; SP_PROPCHANGE_PARAMS1.Scope = Externs.DICS_FLAG_CONFIGSPECIFIC; SP_PROPCHANGE_PARAMS1.HwProfile = 0; } else { SP_PROPCHANGE_PARAMS1.ClassInstallHeader.cbSize = Marshal.SizeOf(typeof(Externs.SP_CLASSINSTALL_HEADER)); if (isRemove == true) { SP_REMOVEDEVICE_PARAMS1.ClassInstallHeader.cbSize = Marshal.SizeOf(typeof(Externs.SP_CLASSINSTALL_HEADER)); SP_REMOVEDEVICE_PARAMS1.ClassInstallHeader.InstallFunction = Externs.DIF_REMOVE; SP_REMOVEDEVICE_PARAMS1.Scope = Externs.DI_REMOVEDEVICE_GLOBAL; SP_REMOVEDEVICE_PARAMS1.HwProfile = 0; } else { SP_PROPCHANGE_PARAMS1.ClassInstallHeader.InstallFunction = Externs.DIF_PROPERTYCHANGE; SP_PROPCHANGE_PARAMS1.StateChange = Externs.DICS_DISABLE; SP_PROPCHANGE_PARAMS1.Scope = Externs.DICS_FLAG_CONFIGSPECIFIC; SP_PROPCHANGE_PARAMS1.HwProfile = 0; } } szDevInfoData = Marshal.SizeOf(devInfoData); ptrToDevInfoData = Marshal.AllocHGlobal(szDevInfoData); Marshal.StructureToPtr(devInfoData, ptrToDevInfoData, true); bool rslt1 = false; bool rstl2 = false; if (isRemove == true && bEnable == false) { szOfPcp = Marshal.SizeOf(SP_REMOVEDEVICE_PARAMS1); ptrToPcp = Marshal.AllocHGlobal(szOfPcp); Marshal.StructureToPtr(SP_REMOVEDEVICE_PARAMS1, ptrToPcp, true); rslt1 = Externs.SetupDiSetClassInstallParams(hDevInfo, ptrToDevInfoData, ptrToPcp, Marshal.SizeOf(typeof(Externs.SP_REMOVEDEVICE_PARAMS))); rstl2 = Externs.SetupDiCallClassInstaller(Externs.DIF_REMOVE, hDevInfo, ptrToDevInfoData); } else { szOfPcp = Marshal.SizeOf(SP_PROPCHANGE_PARAMS1); ptrToPcp = Marshal.AllocHGlobal(szOfPcp); Marshal.StructureToPtr(SP_PROPCHANGE_PARAMS1, ptrToPcp, true); rslt1 = Externs.SetupDiSetClassInstallParams(hDevInfo, ptrToDevInfoData, ptrToPcp, Marshal.SizeOf(typeof(Externs.SP_PROPCHANGE_PARAMS))); rstl2 = Externs.SetupDiCallClassInstaller(Externs.DIF_PROPERTYCHANGE, hDevInfo, ptrToDevInfoData); } if ((!rslt1) || (!rstl2)) { throw new Exception("不能更改设备状态。"); } else { return(true); } } catch (Exception ex) { return(false); } }
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); }
static extern bool SetupDiSetClassInstallParams(IntPtr DeviceInfoSet, ref SP_DEVINFO_DATA DeviceInfoData, SP_REMOVEDEVICE_PARAMS ClassInstallParams, UInt32 ClassInstallParamsSize);
/// <summary> /// Removes all devices with a matching hardware ID /// </summary> /// <param name="hardwareID">ComponenetID</param> /// <returns></returns> public static bool UninstallDevice(string hardwareID) { if (IntPtr.Size == 4 && Environment.Is64BitOperatingSystem) { MessageBox.Show("This process is 32-bit but the OS is 64-bit. Only 64-bit processes can uninstall 64-bit direvers.", "Driver Setup", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button2, MessageBoxOptions.ServiceNotification); return false; } IntPtr deviceInfoSet = SetupDiGetClassDevsEx(IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, DIGCF_ALLCLASSES, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); if (deviceInfoSet == Kernel32.INVALID_HANDLE_VALUE) return false; //SP_DEVINFO_LIST_DETAIL_DATA deviceInfoListDetailData = new SP_DEVINFO_LIST_DETAIL_DATA(); //if (Environment.Is64BitProcess) // deviceInfoListDetailData.cbSize = 560; //else // deviceInfoListDetailData.cbSize = 550; //if (!SetupDiGetDeviceInfoListDetail(deviceInfoSet, ref deviceInfoListDetailData)) // return false; uint index = 0; SP_DEVINFO_DATA deviceInfoData = new SP_DEVINFO_DATA(); deviceInfoData.cbSize = (uint)Marshal.SizeOf(deviceInfoData); while(SetupDiEnumDeviceInfo(deviceInfoSet, index, ref deviceInfoData)) { index++; uint requiredSize = 0; SetupDiGetDeviceRegistryProperty(deviceInfoSet, ref deviceInfoData, SPDRP_HARDWAREID, IntPtr.Zero, IntPtr.Zero, 0, ref requiredSize); if (requiredSize == 0) continue; IntPtr pPropertyBuffer = Marshal.AllocHGlobal((int)requiredSize); if (!SetupDiGetDeviceRegistryProperty(deviceInfoSet, ref deviceInfoData, SPDRP_HARDWAREID, IntPtr.Zero, pPropertyBuffer, requiredSize, IntPtr.Zero)) continue; string currHardwareID = Marshal.PtrToStringAuto(pPropertyBuffer); Marshal.FreeHGlobal(pPropertyBuffer); if (currHardwareID == hardwareID) { SP_REMOVEDEVICE_PARAMS classInstallParams = new SP_REMOVEDEVICE_PARAMS(); classInstallParams.ClassInstallHeader.cbSize = (uint)Marshal.SizeOf(classInstallParams.ClassInstallHeader); classInstallParams.ClassInstallHeader.InstallFunction = DI_FUNCTION.DIF_REMOVE; classInstallParams.Scope = DI_REMOVEDEVICE_GLOBAL; if (!SetupDiSetClassInstallParams(deviceInfoSet, ref deviceInfoData, ref classInstallParams, (uint)Marshal.SizeOf(classInstallParams))) return false; if (!SetupDiCallClassInstaller(DI_FUNCTION.DIF_REMOVE, deviceInfoSet, ref deviceInfoData)) return false; } } SetupDiDestroyDeviceInfoList(deviceInfoSet); return true; }
/// <summary> /// Removed a device node identified by class GUID, path and instance ID. /// </summary> /// <param name="classGuid">The device class GUID.</param> /// <param name="instanceId">The instance ID.</param> /// <param name="rebootRequired">True if a reboot is required to complete the uninstall action, false otherwise.</param> /// <returns>True on success, false otherwise.</returns> public static bool RemoveDeviceInstance(Guid classGuid, string instanceId, out bool rebootRequired) { var deviceInfoSet = IntPtr.Zero; var installParams = Marshal.AllocHGlobal(584); // Max struct size on x64 platform try { var deviceInfoData = new SP_DEVINFO_DATA(); deviceInfoData.cbSize = Marshal.SizeOf(deviceInfoData); deviceInfoSet = SetupDiGetClassDevs( ref classGuid, IntPtr.Zero, IntPtr.Zero, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE ); if (SetupDiOpenDeviceInfo( deviceInfoSet, instanceId, IntPtr.Zero, 0, ref deviceInfoData )) { var props = new SP_REMOVEDEVICE_PARAMS { ClassInstallHeader = new SP_CLASSINSTALL_HEADER() }; props.ClassInstallHeader.cbSize = Marshal.SizeOf(props.ClassInstallHeader); props.ClassInstallHeader.InstallFunction = DIF_REMOVE; props.Scope = DI_REMOVEDEVICE_GLOBAL; props.HwProfile = 0x00; // Prepare class (un-)installer if (SetupDiSetClassInstallParams( deviceInfoSet, ref deviceInfoData, ref props, Marshal.SizeOf(props) )) { // Invoke class installer with uninstall action if (!SetupDiCallClassInstaller(DIF_REMOVE, deviceInfoSet, ref deviceInfoData)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } // Fill cbSize field Marshal.WriteInt32( installParams, 0, // cbSize is first field, always 32 bits long IntPtr.Size == 4 ? 556 /* x86 size */ : 584 /* x64 size */ ); // Fill SP_DEVINSTALL_PARAMS struct if (!SetupDiGetDeviceInstallParams(deviceInfoSet, ref deviceInfoData, installParams)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } // Grab Flags field of SP_DEVINSTALL_PARAMS (offset of 32 bits) var flags = Marshal.ReadInt32(installParams, Marshal.SizeOf(typeof(uint))); // Test for restart/reboot flags being present rebootRequired = (flags & DI_NEEDRESTART) != 0 || (flags & DI_NEEDREBOOT) != 0; return(true); } else { throw new Win32Exception(Marshal.GetLastWin32Error()); } } else { throw new Win32Exception(Marshal.GetLastWin32Error()); } } finally { if (deviceInfoSet != IntPtr.Zero) { SetupDiDestroyDeviceInfoList(deviceInfoSet); } Marshal.FreeHGlobal(installParams); } }
public static extern bool SetupDiSetClassInstallParams(IntPtr deviceInfoList, ref SP_DEVICE_INFO_DATA DeviceInterfaceData, ref SP_REMOVEDEVICE_PARAMS ClassInstallParams, int ClassInstallParamsSize);
public static extern bool SetupDiSetClassInstallParams( IntPtr deviceInfoSet, SP_DEVINFO_DATA deviceInfoData, ref SP_REMOVEDEVICE_PARAMS classInstallParams, int classInstallParamsSize );
public static extern bool SetupDiSetClassInstallParams( IntPtr deviceInfoSet, SP_DEVINFO_DATA deviceInfoData, ref SP_REMOVEDEVICE_PARAMS classInstallParams, int classInstallParamsSize );
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); }
/// <summary> /// Removes devices with the matching hardwareId /// </summary> /// <param name="hardwareId">Identification string for the hardware to be removed</param> public static void UninstallDevice(string hardwareId) { IntPtr deviceInfoSet = IntPtr.Zero; try { deviceInfoSet = SetupDiGetClassDevsEx( IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, DIGCF_ALLCLASSES, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); SP_DEVINFO_DATA deviceInfoData = new SP_DEVINFO_DATA(); deviceInfoData.CbSize = (uint)Marshal.SizeOf(deviceInfoData); uint index = 0; while (SetupDiEnumDeviceInfo(deviceInfoSet, index, ref deviceInfoData)) { index++; uint requiredSize = 0; SetupDiGetDeviceRegistryProperty( deviceInfoSet, ref deviceInfoData, SPDRP_HARDWAREID, IntPtr.Zero, IntPtr.Zero, 0, ref requiredSize); if (requiredSize == 0) { continue; } IntPtr pPropertyBuffer = Marshal.AllocHGlobal((int)requiredSize); if (!SetupDiGetDeviceRegistryProperty( deviceInfoSet, ref deviceInfoData, SPDRP_HARDWAREID, IntPtr.Zero, pPropertyBuffer, requiredSize, IntPtr.Zero)) { continue; } string hwId = Marshal.PtrToStringAuto(pPropertyBuffer); Marshal.FreeHGlobal(pPropertyBuffer); if (hwId.Equals(hardwareId)) { SP_REMOVEDEVICE_PARAMS classInstallParams = new SP_REMOVEDEVICE_PARAMS(); classInstallParams.ClassInstallHeader.CbSize = (uint)Marshal.SizeOf(classInstallParams.ClassInstallHeader); classInstallParams.ClassInstallHeader.InstallFunction = DI_FUNCTION.DIF_REMOVE; classInstallParams.Scope = DI_REMOVEDEVICE_GLOBAL; SetupDiSetClassInstallParams( deviceInfoSet, ref deviceInfoData, ref classInstallParams, (uint)Marshal.SizeOf(classInstallParams)); SetupDiCallClassInstaller( DI_FUNCTION.DIF_REMOVE, deviceInfoSet, ref deviceInfoData); } } } finally { if (deviceInfoSet != IntPtr.Zero) { SetupDiDestroyDeviceInfoList(deviceInfoSet); } } }
private static extern bool SetupDiSetClassInstallParams(IntPtr DeviceInfoSet, ref SP_DEVINFO_DATA DeviceInterfaceData, ref SP_REMOVEDEVICE_PARAMS ClassInstallParams, int ClassInstallParamsSize);