Create a Custom Windows Service
This article describes how to create a service on either Windows NT, 2000 or XP (untested on Vista). The service can run any application required and can be configured as any other service on the system can.
This information has been extracted from the Microsoft article KB137890, and requires two programs that can be found in the Windows NT resource kit (or here)
- SRVANY.EXE – Allows any application to run as a service
- INSTSRV.EXE – Creates the system service
Create the service
Use the INSTSRV.EXE application to create the system service, it requires two parameters; the first is the name of the service you want to create, the second is always the path to the SRVANY.EXE application.
INSTSRV.EXE Notepad c:\SRVANY.EXE
Set the application as a service
Next edit the registry and enter the path to the application that is required to run as a service. The location is the registry depends on the name of the service that was defined above; in this case Notepad
Run regedit.exe
Navigate to HKLM\SYSTEM\CurrentControlSet\Services\Notepad
Create a new key called Parameters
Under the new key create a new string value called Application and set its value to the path of the application c:\WINDOWS\NOTEPAD.EXE
Once created this service can be managed through the services mmc to set its startup type, recovery response etc.
To make this process easier, the following code can be placed into a batch file and run (with the correct parameters) to create the service and congfiure the registry. It assumes that INSTSRV.EXE is already in a search path and that SRVANY.EXE is located in the root of the C drive.
@echo off
rem #################################
rem ## This batch file created a new system service ##
rem ## ensure the correct parameters are used      ##
rem #################################rem Parameter 1 should be the name of the service
rem Parameter 2 should be the path to the executableecho Creating Service
INSTSRV.EXE %1 c:\SRVANY.EXEecho Creating Registry Keys
reg add “HKLM\SYSTEM\CurrentControlSet\Services\%1\Parameters”
reg add “HKLM\SYSTEM\CurrentControlSet\Services\%1\Parameters” /v Application /d %2