Using SetupAPI to Uninstall Devices and Driver Packages
SetupAPI is a system component that provides two sets of functions:
Device installation applications, co-installers, and class installers can use these functions to perform custom operations for device installation. SetupAPI also supports uninstalling the devices and driver packages that it installs.
This topic describes the procedures that you can follow to uninstall devices and driver packages by using the SetupAPI functions.
For more information about uninstalling driver and driver packages, see How Devices and Driver Packages are Uninstalled.
A device installation application can request that a device be uninstalled by calling the SetupDiCallClassInstaller function. When the application calls this function to uninstall a device, it must set the InstallFunction parameter to the DIF_REMOVE device installation function (DIF) code.
If a class installer is registered for the device setup class of the device, SetupDiCallClassInstaller calls the class installer to handle the DIF_REMOVE DIF code. The class installer can do one of the following when handling this DIF code:
- The class installer can indirectly uninstall the device by returning ERROR_DI_DO_DEFAULT. This return code instructs the Plug and Play (PnP) manager to perform the default action for the DIF request. For DIF_REMOVE, the default action is to call the SetupDiRemoveDevice function.
- The class installer can directly uninstall the device (under certain conditions) by calling the SetupDiRemoveDevice function itself. In this case, the class installer must not return ERROR_DI_DO_DEFAULT.
- The class installer can reject the DIF_REMOVE request and return the appropriate Win32 error code.
If SetupDiRemoveDevice is called during the processing of the DIF_REMOVE request, the function removes the device's devnode from the system. It also deletes the device's hardware and software registry keys, together with any hardware-profile-specific registry keys (configuration-specific registry keys).Note SetupDiRemoveDevice must only be called by a class installer and not by a device installation application.
For more information about class installers, see Writing a Class Installer.
For more information about DIF codes, see Handling DIF Codes.
Starting with Windows 7, a device installation application can uninstall a device by calling the DiUninstallDevice function. This function is similar to calling SetupDiCallClassInstaller with the InstallFunction parameter set to DIF_REMOVE. However, in addition to removing the devnode of the specified device, this function attempts to remove all child devnodes of the device that are present on the system at the time of the call.
SetupAPI cannot be used to perform this action.