Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error 1001: The Specified Service Already Exists. Cannot remove existing service

I have a service. I installed it a while ago. I need to do an update to the service. I went to the Add/Remove Programs and looked for my service, and it is not installed there. I looked at services.msc and it is there, stopped. I was able to start it and stop it. I ran a command prompt as administrator and ran sc delete [Service Name], and recieved "The specified service does not exist as an installed service." I did a sc query in the command prompt, and it is not returned. I right clicked on the installer, clicked on uninstall and recieved "This action is only valid for products that are currently installed." I tried repair as well, and got the same message.

I have restarted the machine a few times, and no luck getting this service to uninstall. I'm using the basic Setup Project template installed with Visual Studio. I've tried changing the name of the program, and increasing the version number.

How do I uninstall the service that apparently exists, and prevent this happening in the future?

like image 811
Larry Gasik Avatar asked Mar 27 '12 18:03

Larry Gasik


2 Answers

** If it is required to be done using the setup only, please follow:

This can be handled by explicit implementation of existing service removal (uninstall) and then allowing newer version to install. For this, we need to update ProjectInstaller.Designer.cs as below:

Consider adding following line at the beginning of InitializeComponent() which triggers an event for uninstalling the existing service before your current installer tries to install the service again. Here we uninstall the service if it already exists.

Add following namespaces:

using System.Collections.Generic;
using System.ServiceProcess;

Add below line of code as described before:

this.BeforeInstall += new
System.Configuration.Install.InstallEventHandler(ProjectInstaller_BeforeInstall);

Example:

private void InitializeComponent()
{
    this.BeforeInstall += new System.Configuration.Install.InstallEventHandler(ProjectInstaller_BeforeInstall);

    this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
    this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
    // 
    // serviceProcessInstaller1
    // 
    this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
    this.serviceProcessInstaller1.Password = null;
    this.serviceProcessInstaller1.Username = null;
    // 
    // serviceInstaller1
    // 
    this.serviceInstaller1.Description = "This is my service name description";
    this.serviceInstaller1.ServiceName = "MyServiceName";
    this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
    // 
    // ProjectInstaller
    // 
    this.Installers.AddRange(new System.Configuration.Install.Installer[]{
            this.serviceProcessInstaller1,
            this.serviceInstaller1
        }
    );
}

The below code called by the event will then uninstall the service if it exists.

void ProjectInstaller_BeforeInstall(object sender, System.Configuration.Install.InstallEventArgs e)
{
    List<ServiceController> services = new List<ServiceController>(ServiceController.GetServices());

    foreach (ServiceController s in services)
    {
        if (s.ServiceName == this.serviceInstaller1.ServiceName)
        {
            ServiceInstaller ServiceInstallerObj = new ServiceInstaller();
            ServiceInstallerObj.Context = new System.Configuration.Install.InstallContext();
            ServiceInstallerObj.Context = Context;
            ServiceInstallerObj.ServiceName = "MyServiceName";
            ServiceInstallerObj.Uninstall(null);

            break;
        }
    }
}

PS: Along with the above changes, also please consider updating the setup Version, ProductCode (, and optionall UpgradeCode) for good practice, better version management, tracking and maintenance

like image 181
ShivanandSK Avatar answered Oct 12 '22 11:10

ShivanandSK


It is completely normal that the service is not listed in Add/Remove Programs, that listing is for software packages, not services. (One package, or program, may contain multiple services, but it typically installs none.)

Apparently, the service was installed manually, not as part of the product, even if this one in particular would normally install with a product whose installation package you have got.

Using sc delete is correct. You will need to include the (short) name of the service in double quotes (unless it is just a single word), but nothing else.

Failing that, visit HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services in your registry, both 32 bit and 64 bit (regedt32.exe and regedit.exe, respectively). You can even delete the service there directly, but you should obviously start by reversible changes to diagnose how is your service exactly named and why sc does not see its name and only use direct registry access after everything else has failed and after you have backed up your registry (google this procedure up specifying your operating system).

like image 44
Jirka Hanika Avatar answered Oct 12 '22 13:10

Jirka Hanika