BillStewart
Posts: 34
Joined: Sat Jul 28, 2007 1:48 am

Problems stopping service during uninstall on Windows 7

Using AI 7.5.1

I am installing a service in my package and I have the following properties for the service:

Service name: uvnc_service
Display name: UltraVNC Server
Service file: APPDIR\winvnc.exe
Service type: A Win32 service that runs in its own process
Allow the service to interact with the desktop: Enabled
Start type: Automatic
Error control: Display an error message and continue
Arguments: -service

I also have the following control operation:

Service name: uvnc_service
Event on install: Start
Event on uninstall: Stop and Delete
Service parameters: -service
Wait until the service complete: Enabled

On Windows XP the service stops and uninstalls cleanly. On Windows 7, I get the following error message when uninstalling if the service is running:

The setup must update files or services that cannot be updated while the system is running. If you choose to continue, a reboot will be required to complete the setup.

If I stop the service before starting the uninstall, the message does not appear.

Any ideas on what's going on here?

Thanks,

Bill
GabrielBarbu
Posts: 2146
Joined: Thu Jul 09, 2009 11:24 am
Contact: Website

Re: Problems stopping service during uninstall on Windows 7

Hi Bill,

This is normal behavior.
If you still want to avoid the message, you can place a custom action that stops your service somewhere before InstallValidate standard action to ensure your service is stopped before any checks for files in use are made.

Best regards,
Gabriel
Gabriel Barbu
Advanced Installer Team
http://www.advancedinstaller.com/
BillStewart
Posts: 34
Joined: Sat Jul 28, 2007 1:48 am

Re: Problems stopping service during uninstall on Windows 7

Hi Gabriel,

Why does it work in Windows XP but not in Windows 7?

Thanks,

Bill
GabrielBarbu
Posts: 2146
Joined: Thu Jul 09, 2009 11:24 am
Contact: Website

Re: Problems stopping service during uninstall on Windows 7

Hi Bill,

This is one of the differences between Windows 7 an Windows XP.

Regards,
Gabriel
Gabriel Barbu
Advanced Installer Team
http://www.advancedinstaller.com/
BillStewart
Posts: 34
Joined: Sat Jul 28, 2007 1:48 am

Re: Problems stopping service during uninstall on Windows 7

Hi Gabriel,

So you are saying that the "Event on Uninstall" check boxes named "Stop" and "Delete" do not work in Windows 7. Is this correct? If it is correct, why? What has changed in Windows 7 that it does not work?

Thanks,

Bill
BillStewart
Posts: 34
Joined: Sat Jul 28, 2007 1:48 am

Re: Problems stopping service during uninstall on Windows 7

For anyone who is interested, I have written a "stop service" script in JScript that stops a named service using WMI. It will wait for the service to be stopped up to an (approximate) number of seconds. If the service does not stop within the timeout period, the script returns error 1053 (ERROR_SERVICE_REQUEST_TIMEOUT).

Code: Select all

// Stops a service. Apparently configuring a service control action in
// the ServiceControl table doesn't work on Win 7. Sequence this script
// before the InstallValidate standard action.
//
// Author: Bill Stewart

// Creates the global SWbemServices object.
var SWbemServices = GetObject("winmgmts:{impersonationlevel=impersonate}!" +
  "root/cimv2");

// You can't use the WScript object in a Custom Action (CA), so the
// stopService function (below) can't use its Sleep method. Workaround:
// Create a temporary .js script in the temporary directory and call it.
var FSO = new ActiveXObject("Scripting.FileSystemObject");
var SystemFolder = 1, TemporaryFolder = 2, ForWriting = 2;
var TempScriptName = "";

// Creates the temporary Sleep script if it doesn't already exist and
// writes the WScript.Sleep method to it.
function createTempSleepScript(milliSeconds) {
  var result = 0;

  // Only continue if the file doesn't already exist.
  if (TempScriptName == "") {
    // Create a temporary filename with a '.js' extension.
    do {
      TempScriptName = FSO.BuildPath(FSO.GetSpecialFolder(TemporaryFolder),
        FSO.GetTempName());
      TempScriptName = TempScriptName.substr(0, TempScriptName.length - 3) +
        "js";
    }
    while (FSO.FileExists(TempScriptName));

    // Open it as a text stream, write the script line, and close it.
    try {
      var textStream = FSO.OpenTextFile(TempScriptName, ForWriting, true);
      textStream.WriteLine("WScript.Sleep(" + milliSeconds + ");");
      textStream.Close();
    }
    catch(err) {
      result = err.number;
    }
  }

  return result;
}

// Sleeps for approximately 'milliSeconds'. The WshShell object is a global
// to avoid the overhead of creating it each sleep iteration.
var WshShell = new ActiveXObject("WScript.Shell");
function sleep(milliSeconds) {
  var result = createTempSleepScript(milliSeconds);
  if (result != 0)
    return result;
  var cmdLine = FSO.BuildPath(FSO.GetSpecialFolder(SystemFolder),
    "wscript.exe") + " " + TempScriptName;
  // Run the temporary script and wait for it to complete.
  return WshShell.Run(cmdLine, 0, true);
}

// Retrieves the Win32_Service instance for the service named 'serviceName'
// using the global SWbemServices object. Returns a service object if
// successful or an error code number if failure.
function getService(serviceName) {
  var result;

  try {
    result = SWbemServices.Get("Win32_Service.Name='" + serviceName + "'");
  }
  catch(err) {
    result = err.number;
  }

  return result;
}

// Tells a service to stop and waits for up to 'waitSeconds' for it.
// Returns 0 if successful, non-zero if failure.
function stopService(serviceName, waitSeconds) {
  var ERROR_SERVICE_REQUEST_TIMEOUT = 1053;

  var service = getService(serviceName);
  if (typeof service == "number")
    return service;

  var delay = 1;
  service.StopService();
  do {
    if (service.State == "Stopped")
      break;
    sleep(1000);
    delay += 1;
    service = getService(serviceName);
  }
  while (delay <= waitSeconds);

  // Delete the temporary script.
  if (FSO.FileExists(TempScriptName))
    FSO.DeleteFile(TempScriptName);

  return delay > waitSeconds ? ERROR_SERVICE_REQUEST_TIMEOUT : 0;
}
Because you can't use the WScript object in a custom action, this script writes a temporary .js script in the Temp folder containing the "sleep" command and calls it to delay the script while while waiting for the service to stop.

To use it, add a function to the end of the script that looks like this:

Code: Select all

function stopYourService() {
  return stopService("your_service_name", 20);
}
Use 'stopYourService' as the function name in your custom action, and of course change 'your_service_name' to match your service's name and 20 is the number of seconds the script should wait before it considers the service stop request to have timed out. As Gabriel mentioned previously, set the script to execute before the InstallValidate standard action.

Seems to be a lot of work to do something that used to work in XP but doesn't work in Windows 7...I'd like to know why I have do do this.

HTH,

Bill
BillStewart
Posts: 34
Joined: Sat Jul 28, 2007 1:48 am

Re: Problems stopping service during uninstall on Windows 7

Anyone know why a CA is needed on Windows 7 to stop a service before uninstalling it? It wasn't necessary in XP.

Thanks,

Bill
GabrielBarbu
Posts: 2146
Joined: Thu Jul 09, 2009 11:24 am
Contact: Website

Re: Problems stopping service during uninstall on Windows 7

Hello Bill,

Windows 7 uses Windows Installer 5.0 which, in respect to uninstalling services, behaves differently from other versions of Windows Installer.

Regards,
Gabriel
Gabriel Barbu
Advanced Installer Team
http://www.advancedinstaller.com/

Return to “Common Problems”