// Find the index of the particular DeviceInfoData for the instanceId.
    static int GetIndexOfInstance(SafeDeviceInfoSetHandle handle, DeviceInfoData[] diData,
                                  string instanceId)
    {
        const int ERROR_INSUFFICIENT_BUFFER = 122;

        for (int index = 0; index <= diData.Length - 1; index++)
        {
            StringBuilder sb           = new StringBuilder(1);
            int           requiredSize = 0;
            bool          result       = NativeMethods.SetupDiGetDeviceInstanceId(handle.DangerousGetHandle(), ref diData[index],
                                                                                  sb, sb.Capacity, out requiredSize);
            if (result == false && Marshal.GetLastWin32Error() == ERROR_INSUFFICIENT_BUFFER)
            {
                sb.Capacity = requiredSize;
                result      = NativeMethods.SetupDiGetDeviceInstanceId(handle.DangerousGetHandle(), ref diData[index],
                                                                       sb, sb.Capacity, out requiredSize);
            }
            if (result == false)
            {
                throw new Win32Exception();
            }
            if (instanceId.Equals(sb.ToString()))
            {
                return(index);
            }
        }
        // not found
        return(-1);
    }
示例#2
0
        // Find the index of the particular DeviceInfoData for the instanceId.
        private static int GetIndexOfInstance(SafeDeviceInfoSetHandle deviceInfoSetHandle,
                                              DeviceInfoData[] deviceInfoData, string instanceId)
        {
            const int insufficientBufferError = 122;

            for (int deviceInfoIndex = 0; deviceInfoIndex <= deviceInfoData.Length - 1; deviceInfoIndex++)
            {
                StringBuilder buffer = new StringBuilder(1);
                int           requiredSize;
                bool          result = Win32.SetupDiGetDeviceInstanceId(deviceInfoSetHandle.DangerousGetHandle(),
                                                                        ref deviceInfoData[deviceInfoIndex], buffer,
                                                                        buffer.Capacity, out requiredSize);
                if (result == false && Marshal.GetLastWin32Error() == insufficientBufferError)
                {
                    buffer.Capacity = requiredSize;
                    result          = Win32.SetupDiGetDeviceInstanceId(deviceInfoSetHandle.DangerousGetHandle(),
                                                                       ref deviceInfoData[deviceInfoIndex], buffer,
                                                                       buffer.Capacity, out requiredSize);
                }
                if (result == false)
                {
                    throw new Win32Exception();
                }
                if (instanceId.Equals(buffer.ToString()))
                {
                    return(deviceInfoIndex);
                }
            }
            // not found
            return(-1);
        }
        public static void RemoveDevice(string instanceId, 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++)
                {
                    var needReboot = false;
                    var result     = NativeMethods.DiUninstallDevice(IntPtr.Zero, diSetHandle.DangerousGetHandle(), ref diData[i], 0, out needReboot);

                    if (result == false)
                    {
                        int err = Marshal.GetLastWin32Error();
                        throw new Win32Exception();
                    }
                }
            }
            finally
            {
                if (diSetHandle != null)
                {
                    if (diSetHandle.IsClosed == false)
                    {
                        diSetHandle.Close();
                    }
                    diSetHandle.Dispose();
                }
            }
        }