Components that run automatically with Windows on boot up often establish themselves as a system service. Other options are to add programs into the registry in places like HKCU\Software\Microsoft\Windows\CurrentVersion\Run and HKLM\Software\Microsoft\Windows\CurrentVersion\Run, or into the “All Users” Startup group folder (C:\Documents and Settings\All Users\Start Menu\Programs\Startup, C:\Users\All Users\Start Menu\Programs\Startup). There are pros and cons to each method, for example Startup group items will not begin until a user has logged on to the system.
To register a program as a service you can use the SC.EXE command which should be part of your Windows operating system.
(C) Copyright 1985-2001 Microsoft Corp.
C:\user>sc create MyProgramName binpath= "C:\MyFolder\MyProgram.exe" type= own start= auto DisplayName= "My Sample Program"
[SC] CreateService SUCCESS
Sometimes programs require running interactively with the Desktop, you can modify your service in the Services control panel applet, (or with SC) to reflect that. To specify via SC that a program should run interactively add “type= interact” in the arguments (note, you can more than one “type=” statements at a time).
The problem we quickly run into is that MyProgram.exe is not aware of how to behave like a service, so the Service Control Manager (SCM) will shut it down shortly after it starts. We can get around this by making a different program call MyProgram.exe in the binpath configuration.
[SC] ChangeServiceConfig SUCCESS
Notice two things here, first instead of saying “sc create” I said “sc config“, that is because we already had a service named “MyProgramName” from our previous attempt. The “config” argument edits an existing service, where as the “create” argument makes a new service from scratch. You can also delete an existing service with “sc delete” followed by the service name (i.e. MyProgramName). The second thing you should notice is that I’ve added “cmd.exe /c” to the binpath config.
CMD.EXE is also not service-aware and so SCM will stop it shortly after it begins, however SCM will not kill the child program (MyProgram.exe), only it’s parent (cmd.exe). The problem with this approach is that SCM will still report the service as failing to start, and will reflect that status in the Services applet as well.
The work around is to use SRVANY.EXE that comes with the Windows Resource Kit, it is a service-aware application that will play nice with SCM, and will start up our program in the process.
You can download the Windows Resource Kit from:
http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=17657
Once downloaded and ran, the Windows Resource Kit installs to
Installs to:
Copy SRVANY.EXE to a different location. In the example above, the MyProgram.exe application was in the C:\MyFolder directory, you could copy SRVANY.EXE to that location as well.
Delete any previous attempts at creating a service you might have made jumping ahead and not reading all of this article before starting
[SC] DeleteService SUCCESS
Now recreate the service, by use SRVANY.EXE in the BinPath statement.
[SC] CreateService SUCCESS
This will give us a service called “MyProgramName“, the name that displays in the Services control panel application will be “My Sample Program“, but the name we’d use with “net start” or “net stop” is MyProgramName. The problem is that
this doesn’t have anything to do with MyProgram.exe, no where is MyProgram.exe actually being started.
SRVANY.EXE looks at the name of the service it’s being ran as. In this way you can have multiple services configured to run SRVANY.EXE, and depending on the name of the service starting SRVANY, it will figure out what to do next. SRVANY gets its instructions by looking at the Windows registry. When you create a new service a registry key is made at HKLM\System\CurrentControlSet\Services with the name of your newly created service.
So for this example the service named MyProgramName now has this registry entry:
Open up regedit.exe, and go to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\MyProgramName
Make a new registry key called “Parameters” by right-clicking on the MyProgramName key and choosing “New” -> “Key”
Click on the newly created “Parameters” key
In the empty space at the right, right-click and choose “New” -> “String Value”
Name the new string value “Application” and set the value to the program and command line arguments to run.
SRVANY will look for the “Application” key and run it when SCM tells it to start up.
If you are designing an application to run as a Windows service it needs to be service-aware in that it must speak and honor constructs of the Service Control Manager. The SCM is in charge of telling a service to start or stop, as well as determining if the service has started in a timely fashion or has crashed unexpectedly (and what to do in such a case). Unfortunately you may be using a third-party application as a service, or perhaps you’re just trying to be lazy, in which case I hope the above information has helped you out.
5 thoughts on “Make a Program Run as a Windows Service on Boot”
CoreTech ·
Hi, interesting article. The trick about using the command prompt “in between” the SCM and the application is clever and quite unique – never heard of that before!
Jo ·
I saw that, was just a little wary of trying it out just in case it screwed anything up. I will be doing this on my DC, so I am very cautious.
Brian ·
This still works in win 10 and I use it often, but you mistyped the name of the program as “sVRany.exe” but it is actually “sRVany.exe”
Chris ·
Thanks Brain, I’ve corrected the mistake.
Lawrence ·
Used it on a server running 2012 R2 and it works great.
Thank you!