How to remove a Windows service and delete its files without rebooting

My current project involves deploying an updated .exe file that runs as a Windows service. To overwrite an existing .exe with a new version, I currently need to:

  • Stop service
  • Delete Service
  • Reboot the system (so that Windows releases it to a file)
  • Expand the new .exe
  • Reinstall the service
  • Start the updated service.

I would like to avoid a reboot, so this could be a completely rewritten / automatic update.

Is there a way to avoid a reboot? Maybe a command line tool that will force Windows to abandon the deadly battle on the old .exe?

+42
windows scripting filesystems operating-system windows-services
Nov 18 '08 at 18:06
source share
9 answers
+76
Nov 19 '08 at 13:47
source share

Can't stop the service before the upgrade (and restart after the upgrade) using the commands below?

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

Whenever I test / deploy a service, I can upload files without reinstalling while the service is stopped. I'm not sure you have a different problem.

+14
Nov 18 '08 at 18:10
source share

I had the same problem as you. I have a system service that I want to uninstall and then reinstall as part of the update. On some systems, this will not work without a reboot. The problem was that the DeleteService () call would return to normal, but the next CreateService () call would tell me that the service still exists but was marked for deletion (error code 1072). The registry will reflect this because the subkey was still there (under HKLM \ System \ CurrentControlSet \ Services), but the "DeleteFlag" parameter was set to 1. From now on, only a reboot can fix the situation.

Some things that do not work:

  • Using "sc delete": it had the same problems as me. The call will return to normal, but the service has not actually disappeared and is still in the registry using DeleteFlag = 1.
  • Removing a key in the registry. The service manager seems to store the database in memory, and the registry is a copy of it for the next boot.
  • Adding wait loops waiting for .exe files to be ready to overwrite, kill a process, etc.
  • Closing service descriptors. Which of them?

But here is what worked:

In some articles in stackoverflow, I noticed that net.exe has start / stop functions (I only knew about sc.exe utility). And oddly enough, "net stop svcname" worked, plus "sc delete svcname"! Therefore net.exe should do what I am not doing.

But net.exe does not contain an import for ControlService (), so how to stop the service? I found out that net.exe starts net1.exe, but net1.exe does not import ControlService (). I used the great API Monitor utility ( http://www.rohitab.com/apimonitor ) to find out what net1.exe does, but it never called anything promising.

But then I saw that he was importing NetServiceControl () from NETAPI32.DLL (which had at least “Service” in his name!). MSDN says this feature is deprecated. However, I found a prototype in LMSvc.h and some parameter descriptions 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 for SERVICE_CTRL_UNINSTALL, which is used to stop), the service stops afterwards. And it can be uninstalled and reinstalled later without DeleteFlag or reboot!

Thus, it was never a removal problem, but to stop the service properly. And NetServiceControl () does the trick. Sorry for the long post, but I thought it might help someone with similar problems. (For reference only, I am using Win7 SP1 x64.)

+13
Jul 09 '12 at 16:15
source share

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

  • Stop the service (this may be due to a problem.)
  • InstallUtil -u [executable file name]
  • Installutil -i [executable file name]
  • Start the service again ...

If I do not change the public interface of the service, I often deploy updated versions of my services without even memorizing / reinstalling ... ALl I do this, stop the service, replace files, and restart the service again ...

+3
Nov 18 '08 at 18:28
source share

As noted by StingyJack and mcbala, as well as regarding comments made by Mike L, my experience is that on a Windows 2000 machine, when uninstalling / reinstalling .Net services, “installutil / u” does > a reboot is required, even if the service was stopped Earlier. "sc / delete", on the other hand, does not require a reboot - it deletes the service immediately (while it is stopped).

I often wondered if there really is a good reason why "installutil / u" requires a reboot ... "sc / delete" actually does something wrong / leaves something hanging?

+3
Mar 11 '09 at 17:31
source share

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

I will tell you about a warning that has been studied hard - Windows 2000 Server (and possibly the client OS) will require a reboot before reinstalling, no matter what. There should be a registry key that is not completely cleaned until it reboots. Windows Server 2003, Windows XP, and later do not suffer from this pain.

+2
Nov 18 '08 at 18:41
source share

(so Windows releases it to a file)

Instead, do Ctrl + Alt + Del right after stopping the service and kill the .exe service. Then you can remove the service without rebooting. This happened to me in the past, and she decides the part that you need to reboot.

+2
Nov 19 '08 at 13:25
source share

If you need to manually remove the service:

  • Run regedit or regedt32.
  • Locate the entry in the registry key for your service under the following key: HKEY_LOCAL_MACHINE / SYSTEM / CurrentControlSet / Services
  • Delete registry key

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

+2
Sep 22 2018-10-22
source share

I am using InstallUtil.exe, packaged in the .NET Framework.

Usage for removal: InstallUtil '\ path \ to \ assembly \ with \ the \ installer \ classes' / u, for example: installutil MyService.HostService.exe /u

The /u switch means uninstall, without which the utility performs a normal service installation. The utility stops the service if it is running, and I have never had problems with Windows, which keeps the lock in the service files. You can read about other InstallUtil options on MSDN .

PS: if you do not have installutil in your path variable, use the full path as follows: C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe "C:\MyServiceFolder\MyService.HostService.exe" /u or if you need a 64-bit version, it can be found in 'C: \ Windows \ Microsoft.NET \ Framework64 \ v4. 0.30319 \ '. The version number in the path depends on the .NET version.

0
Apr 17 '14 at 6:48
source share



All Articles