I recently had to convert a C++ application to run as a service, so that it could always run in the background. This application was supposed to already be capable of doing this, but instead would crash instantly.
Rather than going into the code and figuring out why that would be, I decided that someone would have written a wrapper to convert any application into a service. It turns out there is such a wrapper, and it’s called srvany.exe.
Here is how to convert any Windows application to run as a service.
By the way, if your application is Java based, there is a specific Java Service Wrapper so you probably should go there instead of reading these instructions. This is for generic executable or batch files.
Download and install the NT Resource Kit
The files you are going to need from this:
- srvany.exe
- instsrv.exe
Install the srvany service
- Run the command: instsrv service_name [path]\srvany.exe, where service_name is the name of your service
Configure the new service
- Run regedit
- Browse to HKEY_LOCAL_MACHINE\ SYSTEM\ CurrentControlSet\ Services\ service_name
- Add key “Parameters”
- Right click on “Parameters”
- New string value, “Application”, with the value as the executable you want to run, including any parameters.
- If your application is not an executable, bat a batch file, set the value to “C:\Windows\system32\cmd.exe /k full_path_to_batch_file.bat”
- Of course the batch file or executable can’t exit, as this will end the service.
And some more configuration
- Control Panel, Administration, Services, find your service.
- The default is “Automatic”, which means as long as nothing goes wrong, your service will always be running. You might want to change this.
- You might want to set the service to run as something other than “Local Service”, e.g. have it run under your name.
Test It
- Start the service
- Your application should appear in task manager
Celebrate
Now you can have something run all the time, behind the scenes, as a service. Hurrah!