46

My current project involves deploying an upgraded .exe file that runs as a Windows Service. In order to overwrite the existing .exe with the new version, I currently need to:

  1. Stop the service
  2. Uninstall the service
  3. Reboot the system (so Windows releases it's hold on the file)
  4. Deploy the new .exe
  5. Reinstall the service
  6. Start the upgraded service.

I'd like to avoid the reboot, so that this can be a fully scripted/automated upgrade.

Is there any way to avoid rebooting? Maybe a command-line tool that will force Windows to give up it's death grip on the old .exe?

3
  • Thanks everyone for your help! I found that the source of my problem was a handle on the .exe by WMI. See my new question at: stackoverflow.com/questions/302315/… Nov 19, 2008 at 15:44
  • 5
    FYI, if you have the Services manager open, you need to close before re-installing the service or else you will get a 'service is already marked for deletion' error. Jul 3, 2012 at 21:20
  • In my experience "[SC] CreateService FAILED 1072: The specified service has been marked for deletion" hinges on whether the service being uninstalled has not been stopped first. I tried to reproduce the error simply having the Services administrative tool open and was unable to.
    – J0e3gan
    May 1, 2013 at 5:16

10 Answers 10

78
sc delete "service name"

will delete a service. I find that the sc utility is much easier to locate than digging around for installutil. Remember to stop the service if you have not already.

4
  • 15
    sc delete "service name" < at least on windows 7. Sep 24, 2010 at 7:21
  • 1
    yes... and eventually ms will go to "--" like *nix. One step at a time =)
    – StingyJack
    Dec 27, 2010 at 14:33
  • in my case, the command is sc delete "serviceName" note that you must excute command line as administrator
    – k.honsali
    Apr 2, 2013 at 15:22
  • 6
    I have found that sc delete does not stop a running service when uninstalling it but rather marks it for deletion, which is what its documentation also describes.
    – J0e3gan
    May 1, 2013 at 8:47
15

I had sort of the same problem as you. I have a system service that i want to uninstall and afterwards reinstall as part of an update. On certain systems this would not work without a reboot. The problem was that a call to DeleteService() would return ok, but the following call to CreateService() would tell me the service was still there, but marked for deletion (error code 1072). The registry would reflect that, since the subkey was still there (under HKLM\System\CurrentControlSet\Services), but "DeleteFlag" was set to 1. From that point on, only a reboot could fix the situation.

Some things that don't work:

  • Using "sc delete": it had the same problems as I. The call would return ok, but the service was not really gone and still in the registry with DeleteFlag = 1.
  • Deleting the key in the registry. The Service Manager seems to keep a database in memory and the registry is just a copy of it for the next boot.
  • Adding wait loops, waiting for .exe files to be ready to be overwritten, killing the process, etc.
  • Closing handles to the service. Which ones??

But here is what worked:

I noticed in some articles here on stackoverflow that net.exe has start/stop features as well (I only knew of sc.exe utility). And strangely enough, a "net stop svcname" plus a "sc delete svcname" worked! So net.exe must do something I don't do.

But net.exe doesn't contain an import to ControlService(), so how does it stop the service? I found out that net.exe spawns net1.exe, but net1.exe doesn't import ControlService() as well. I used the great API Monitor utility ( http://www.rohitab.com/apimonitor ) to see what net1.exe is doing, but it never called anything that looked promising.

But then I saw that it imports NetServiceControl() from NETAPI32.DLL (that had at least "Service" in its name!). MSDN says that this function is obsolete. Nevertheless, I found the prototype in LMSvc.h and some parameter description here: http://cyberkinetica.homeunix.net/os2tk45/srvfpgr/369_L2_NetServiceControlorN.html . When you load NETAPI32.DLL and use NetServiceControl(NULL, service_name, 3, 0, 0) (3 is for SERVICE_CTRL_UNINSTALL, which is used to stop) the service is stopped afterwards. And it can be deleted and reinstalled afterwards without DeleteFlag or reboot!

So it was never a problem of deleting, but of stopping the service properly. And NetServiceControl() does the trick. Sorry for the long post, but I thought it might help someone with similar problems. (Just for reference, I use Win7 SP1 x64.)

3
  • How do you reliably simulate the "marked for deletion" state? What I did to simulate it is just open a handle to the service with the normal services API. with the handle open, this method does not work. When the handle is closed, the service disappears automatically.
    – shoosh
    Jan 22, 2015 at 15:51
  • Thanx! i was looking for the difference between 'net stop' and 'sc stop'
    – Nerielle
    Jul 5, 2016 at 15:09
  • Great analysis!
    – Max
    Mar 8, 2017 at 14:36
14

Are you not able to stop the service before the update (and restart after the update) using the commands below?

net stop <service name>
net start <service name>

Whenever I'm testing/deploying a service I'm able to upload files without reinstalling as long as the service is stopped. I'm not sure if the issue you are having is different.

2
  • 4
    This has always been my experience - I have endlessly updated services merely by stopping them and replacing the .EXE. It's only an ordinary process anyway.
    – Will Dean
    Dec 5, 2008 at 22:08
  • Same here - worked for me as described for months of development on a suite of Windows services (running on Windows 7).
    – J0e3gan
    May 1, 2013 at 5:26
3

If in .net ( I'm not sure if it works for all windows services)

  • Stop the service (THis may be why you're having a problem.)
  • InstallUtil -u [name of executable]
  • Installutil -i [name of executable]
  • Start the service again...

Unless I'm changing the service's public interface, I often deploy upgraded versions of my services without even unistalling/reinstalling... ALl I do is stop the service, replace the files and restart the service again...

1
  • In my case, I cannot do that due to some permission issue
    – k.honsali
    Apr 2, 2013 at 15:22
3

As noted by StingyJack and mcbala, and in reference to comments made by Mike L, my experience is that on a Windows 2000 machine, when uninstalling / reinstalling .Net services, "installutil /u" does require a reboot, even when the service was previously stopped. "sc /delete", on the other hand, does not require a reboot - it deletes the service right away (as long as it is stopped).

I have often wondered, actually, whether there is a good reason "installutil /u" requires a reboot... Is "sc /delete" actually doing something wrong / leaving something hanging?

1
  • +1 for the qualification "(as long as it is stopped)". This is consistent with my experience - albeit on Windows 7.
    – J0e3gan
    May 1, 2013 at 5:20
2

Both Jonathan and Charles are right... you've got to stop the service first, then uninstall/reinstall. Combining their two answers makes the perfect batch file or PowerShell script.

I will make mention of a caution learned the hard way -- Windows 2000 Server (possibly the client OS as well) will require a reboot before the reinstall no matter what. There must be a registry key that is not fully cleared until the box is rebooted. Windows Server 2003, Windows XP and later OS versions do not suffer that pain.

2

(so Windows releases it's hold on the file)

Instead, do Ctrl+Alt+Del right after the Stop of the service and kill the .exe of the service. Than, you can uninstall the service without rebooting. This happened to me in the past and it solves the part that you need to reboot.

2

Should it be necessary to manually remove a service:

  1. Run Regedit or regedt32.
  2. Find the registry key entry for your service under the following key: HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services
  3. Delete the Registry Key

You will have to reboot before the list gets updated in services

0

I am using the InstallUtil.exe packed with .NET Framework.

The usage to uninstall is: InstallUtil '\path\to\assembly\with\the\installer\classes' /u so for example: installutil MyService.HostService.exe /u

The /u switch stands for uninstall, without it the util performs normal installation of the service. The utility stops the service if it is running and I never had problems with Windows keeping lock on the service files. You can read about other options of InstallUtil on MSDN.

P.S.:if you don't have installutil in your path variable use full path like this: C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe "C:\MyServiceFolder\MyService.HostService.exe" /u or if you need 64bit version it can be found in 'C:\Windows\Microsoft.NET\Framework64\v4.0.30319\' .The version number in path varies depending on .NET version.

0

My batch file to stop and delete service

@echo off
title Service Uninstaller
color 0A
set blank=
set service=blank
:start
echo.&echo.&echo.
SET /P service=Enter the name of the service you want to uninstall:  

IF "%service%"=="" (ECHO Nothing is entered
GoTo :start)
cls
echo.&echo.&echo We will delete the service: %service%
ping -n 5 -w 1 127.0.0.1>nul
::net stop %service%
ping -n 2 -w 1 127.0.0.1>nul
sc delete %service%
pause
:end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.