Run any program as a service

In my last column, I introduced the Microsoft Windows NT Server 4.0 Resource Kit's Srvany tool, which lets you run a program as an NT service, and I explained how to set up the utility. In this column, I explain how to use the tool.

Services such as DNS, DHCP, WINS, and Microsoft Internet Information Server (IIS) run regardless of whether someone is logged on because developers built the programs to do so. But modifying an application to run as a service is difficult if you can't access the application's source code. Srvany lets you run an application as a service whether or not you're logged on or have access to the application's source code. Srvany acts as a kind of envelope for almost any program (i.e., programs that don't require user input) to run in.

Keys to Success
As I explained in my last column, installing Srvany as a service is the first step to set up a program to run as a service. In the installation process, you must create a Registry key under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services. I used a fictitious application named notify.exe in my example, and I created a Registry key named HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Notify.

After you create a Registry key for the program you want to run, you need to tell the system to run the program. Open a Registry editor, and go to the Registry key you created. Within the key, create a value entry named Application as a REG_SZ string and enter the application's fully qualified pathname. For example, I'd enter D:\apps\notify\notify.exe for the Application value in HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Notify.

Some programs work only if you set the default directory to an arbitrary location. Thus, you need to know how to tell NT to point to such a location. In addition, some programs need NT to pass parameters to them. You need to know how to set up the Registry parameters. To solve both of these problems, you must create two additional Registry keys.

If you want Srvany to run an application in a particular default directory, create a value entry named AppDirectory as a REG_SZ string in Srvany's Registry key and put the directory name in that value entry. To specify a set of parameters to feed to the application, create a value entry called AppParameters as a REG_SZ string in Srvany's Registry key and put the parameters in that value entry. Most of these operations are case insensitive. However, if the program (e.g., notify.exe) requires parameters to be in a particular case, you must use the correct case when you enter those parameters in the AppParameters value entry. So, if notify.exe needed to work by default in the directory E:\files and required a parameter string such as -e -i -Aupdate, you'd have three value entries in HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Notify: Application, which would contain the string D:\apps\notify\notify.exe; AppDirectory, which would contain E:\files; and AppParameters, which would contain -e -i -Aupdate.

Console or Network?
After you create the necessary Registry keys to let an application run as a service, you have an important decision to make. You must decide whether you want the service to interact with the console (i.e., use the local keyboard, screen, and mouse) or access network resources; NT won't easily let a service do both.

Start the Control Panel Services applet, find the newly installed service, click the service, and select Startup. The dialog box that opens lets you configure the service to start automatically, manually, or not at all (i.e., to disable the service). At the bottom of the dialog box, you must specify an account to associate the service with.

To make a service interactive, select the Allow Service to Interact with Desktop check box to let the service use the LocalSystem account. The program won't be able to access network resources. To let a service use network resources, associate the service with an account that has network privileges. The LocalSystem account is powerful, but only on the account's machine. In general, the LocalSystem account has no permissions on other machines.

Run with It
After you make the necessary Registry edits and Control Panel adjustments, your program is ready to run as a service. Remember that you can't make every application into a service, so you need to experiment to determine whether Srvany can make your favorite program run as a service.