Search code examples
c#.netwindows-servicessetup-project

how to remove/kill/delete windows service completly from OS?


Hi i have prepared a windows service which setup on server by batch file (.bat file). Also i can remove it from system completely after running Uninstall.bat There is no myservice on Services.Every thing is look good.

after clicking Setup.exe , error message appears: Error 1001 The specified service already exists:

How to remove old windows service from my os completely. Or how to solve this setup problem. My uninstall.bat exe working good.


Solution

  • My guess based on the description is that the service (or rather the registry key) exists indeed. You will not be able to work around this without manually intervening. What seems to happen is that you indeed remove the service, but seemingly don't stop it (or it can't be stopped).

    When a service is still running when it gets removed from the SCM database, the service key will be marked for deletion internally plus there will be an additional value in the key telling the system to remove the key upon boot. The idea is that if the system gets shut down normally, the internal linked list of services to delete will be used to remove the service key and that will be done before the system restarts. If, however, the system crashes the key will be removed upon boot and before the SCM starts to bring up the services.

    The internal linked list is likely what causes the trouble for you here. Because when you go through the SCM APIs that is what gets done. One way to work around this (and I have done this in the past) is to rename the key in the registry at a strategically good point in time and set it to be removed upon boot instead of using the SCM APIs.


    Side note: inside an MSI you should always use the ServiceInstall/ServiceControl and Registry tables instead of using code (be it a batch file or your custom action). These actions already exist and merely need to be sequenced.