Configuring an Executable to Run as a Service on Windows Server.

Being able to run an executable as a service can be very useful. This example uses ip_relay.exe, a command-line tool (with a Win32 equivalent) that can be used to relay one IP/Port to another.

Two tools are required to install a standard executable as a service, INSTSRV and SRVANY. Both are available in the Windows 2003 Server Resource Kit which can be downloaded from Microsoft here.

If you don't want to install all of the tools - or you want to run them on Windows 2000 Server - you can download the MS Resource Kit and extract the individual files using an archiving tool such as 7-Zip.

The paths for the files (once you have extracted the rktools.exe file) are:

  • rktools.exe\rktools.msi\Cabs.winrk.cab\srvany.exe
  • rktools.exe\rktools.msi\Cabs.winrk.cab\instsrv.exe

Srvany.exe is the executable that will run your executable as a service. You could try running the executable directly but depending on the executable, it could result in orphaned processes or it might just return an error. The worst (or maybe best) case is that the executable will not run at all.

Instsrv.exe is the executable that used to install srvany.exe as a service.


Installing the Service.

First, install srvany.exe as a service. From a command prompt, run
%path-to%\INSTSRV.EXE ServiceName %path-to%\SRVANY.EXE.

For this example: c:\INSTSRV.EXE "IP Relay" c:\WINNT\SRVANY.EXE. I've wrapped the service name in quotes as this service name has a space in it.

Once you've installed the service, you'll need to edit the system registry to point to the actual executable file, add any switches or options, and add a nice description so that when you come back to it in six months time you'll remember what it's for.

Serious problems might occur if you modify the registry incorrectly. Back up the registry before you modify it, then you can restore the registry if a problem occurs. For more information about how to back up and restore the registry, click the following link to view an article in the Microsoft Knowledge Base on this subject: KB322756 (http://support.microsoft.com/kb/322756/) How to back up and restore the registry in Windows.

Use Regedit.exe to edit the registry key:
"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\IP Relay-(Your service name here!)"

You can now use the Services MMC snap-in or NET START SERVICE NAME from a console to start your new service!


Useful Links.

Related Content.

Notes on Windows Servers

Notes on Windows Clients


Find me on...




Support Wikipedia.
Support Wikipedia