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);
Exemple #3
0
 public static extern bool SetupDiSetClassInstallParams(IntPtr deviceInfoSet, ref SP_DEVINFO_DATA deviceInfoData, SP_REMOVEDEVICE_PARAMS classInstallParams, UInt32 classInstallParamsSize);