How To Make Sure That a Service is Removed When The Driver is Uninstalled or Upgraded (822445)



The information in this article applies to:

  • Microsoft Windows 2000 Server
  • Microsoft Windows 2000 Professional
  • Microsoft Windows 2000 Advanced Server
  • Microsoft Windows XP Professional

SUMMARY

This article, and the sample code contained herein, explains a technique to make sure that a service that is associated with your driver is removed when you install or upgrade the driver.

MORE INFORMATION

The driver writer can handle removal of associated services in their device coInstaller by handling the Dif_DestroyPrivateData function:
HKEY hKey;
BOOL bNeedToUninstallService = FALSE;

switch(InstallFunction) {

    case DIF_DESTROYPRIVATEDATA :

        //
        // If the co-installer is being unloaded, and is no longer in the list of
        // device-specific co-installers for this device, then the driver is
        // being replaced (or the device is being uninstalled altogether).
        //
        hKey = SetupDiOpenDevRegKey(DeviceInfoSet,
                                    DeviceInfoData,
                                    DICS_FLAG_GLOBAL,
                                    0,
                                    DIREG_DRV,
                                    KEY_READ
                                   );

        if(hKey == INVALID_HANDLE_VALUE) {
            //
            // If you cannot open the key for read access, this most likely
            // means that the device's driver key has been deleted (typically,
            // because the device is being uninstalled).  Therefore, treat this 
            // the same as the scenario where the co-installer is no longer in 
            // the list.
            //
            bNeedToUninstallService = TRUE;

        } else {
            //
            // Retrieve the REG_MULTI_SZ list of co-installers from the 
            // CoInstallers32 value entry, and then look for the co-installer 
            // module/entry-point in the list.
            //
            bNeedToUninstallService = !CoinstallerStillInList(hKey, "CoInstallers32");

            RegCloseKey(hKey);
        }

        if(bNeedToUninstallService) {

            <TO-DO: check service ref-count and uninstall service if necessary>
        }

        return NO_ERROR;
}

Modification Type:MinorLast Reviewed:8/30/2004
Keywords:kbhowto KB822445 kbAudDeveloper