Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


Return to article

More About Srvany
 

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.







Reader Comments

Setup of srvany Registry Keys as discussed under the "Keys to Success" will not work as Mark has listed. To make the application Notify run properly as a service, a new subkey Parameters must be created that contains the Application and AppParameters value entries. The registry locations should be as follows: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Notify\Parameters\Application: REG_SZ: D:\apps\notify\notify.exe and HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Notify\Parameters\AppDirectory: REG_SZ: E:\files. Note: Got this info from the Srvany instructions of the Resource Kit CD. thanks

Gene K. Burton -June 30, 2000

while installing a service thro srvany.exe,i can c both the files(srvany.exe and my application exe) is running in the system??.. why??

Anandhi -August 01, 2001

Both srvany and your app have to be running. Your app for obvious reasons and srvany because it is doing the work of the service (stopping/starting, etc).

whatever -October 31, 2001

Thanks Gene for your comment...it now works as it should!

Bill -April 07, 2002

Where can I get the Microsoft Windows NT Server 4.0 Resource Kit and how much does it cost?

Bryan Duncan -October 09, 2002

How can I write a C++ program that will run as a Service, without using Microsoft's compiler or Srvany? I am familiar with writing Windows application programs using the Watcom C++ Version 11 compiler, but don't know how to modify my programs to run as Services.

Marshall Solomon -December 24, 2002

One very important element that you left out: Before creating the string 'Application', you must create a sub-key called 'Parameters'. Read here: http://support.microsoft.com/default.aspx?scid=kb;en-us;137890 Of course, this is for Win2K.

Jason -March 11, 2004

We have srvany running a program on 2000 Server. My question is, can it be used on Windows 2000 Professional or Windows XP Professional.

Gary Hart -March 17, 2004

This can indeed by used on Windows 2000 Professional as I have just tested it and it works like a charm. Now all I have to do is try to unistall the service in a similar fashion :)

Gavo -April 21, 2004

Great reading, Mark. I am having a problem with my application running with srvany. When I boot the computer the application is not being run (or even located). There are no errors returned that I can find. The only problem that I can see may be one of the parameters that was enetered at installation. Namely, the AppParamerers and AppEnvironment sub keys. The appParameters string that I used was the path and filename to the exe that I want to run as a service. I did not do a AppEnvironment key. Are these correct? I hainstalled them as needed in the Parameters subkey that I made. Thanks

Ryan -April 23, 2004

The location to download (free) the srvany program is here: ftp://ftp.microsoft.com/bussys/winnt/winnt-public/reskit/nt40/i386/srvany_x86.exe. I have it working on XP Pro machine.

Rick -May 12, 2004

Hi, I have a typical problem where my installation is doing two component registration in COM+ server on destination machine. Now the registration with com+ required local admin previleges. But the users does not have local admin previleges. Can I make such installation available for user ? How SRVANY can help me in that. Please advise. (shirish.patel@gsk.com)

shirish -May 13, 2004

I am not not in admin group on a SQL Server 2000 where I want to run a program as a service. How can I install a service or do I have to be added to the adming group? Thanks. Jazz

Jazz -May 20, 2004

I have 20 or so programs that I want to run under SRVANY. I want to run these services under a specific user account. After setting all this up I try to start all the services. Only about 10-15 servies will start this way. I think there is a bug in SRVANY when running under a user account. It work fine when running under the system account. Any ideas?

Jim -May 24, 2004

You may have hot an issue with the Windows desktop heap. Every service that starts with an account OTHER THAN "local system" will cause the creation of a new desktop heap for EACH instance. You can run out of desktop heap if it's set wrong. Do a search on "SharedSection" on the MS Knowledge Base for more informaiton.

Brian Cowan -June 03, 2004

Nicely explained topic! I'm trying to run an app as a service under the local system account. I've created the service and added the registry entry, but I keep getting an "Error 5: Access denied" message when trying to start the service. I also tried running the service under my own account (local admin), and I get the same error. Any suggestion? Thanks!!

Bob -June 04, 2004

Hi - a very useful article. The MS documentation for srvany warns that when the service is stopped the application will be uncermoniously terminated using TerminateProcess. I need my application to be allowed to close down cleanly if the service is stopped or the pc is shutdown. Is there a way of achieving this?

Colin Law -June 08, 2004

There is a better way... Use google.com

some1 -July 06, 2004

Hey some1, could you be a bit more specific? Can you give us a hint? I also need my service to terminate properly, but none of the WM_CLOSE or WM_DESTROY messages are coming!

Michel -July 08, 2004

I have installed and are running srvany successfully, but when trying to start the service from another application which interacts with the Windows services I receive an error "Terminated Unexpectedly", I presume the application calling is expecting a paramenter back from the service that it Started. I have tested , and successfully stopped and started other services, but the service created via srvany simply does not want to start unless directly from the services console.

eduardv -July 26, 2004

How can I make the service run only on a subset of processors in a multi CPU machine without using the task manager? Is there a registry entry which can help?

nrajivk -October 06, 2004

how to use this to run my java rmi server class as nt service ?

Anonymous User -November 09, 2004

I have this all working as long as I specify the local D:\path when in reality I need it to run from y:\path in which y: is really a map to the D: drive (dont ask why). I have also installed autoexnt.exe as a service to allow the drive mapping to occur before the service, and it appears to work fine, as the DLLs used by the service are found okay, but I cannot use y:\path\myservice.exe instead of d:\path\myservice.exe. I have also tried specifying a network account for the service, to no avail.

Anonymous User -December 03, 2004

Is there anyway you know of to run Srvany.exe with a user account that has less than Administrative security access? Need to be able to run Srvany.exe with a minimal level of security.

Anonymous User -December 17, 2004

If you want your process to terminate in a neat way, try this (works for me) 1. let the service start a launch app 2. have the launch app start your actual application (with a wait option) 3. have your application check for the service status. If service is stopped, close down application properly. 4. have the launch app stop the service in a proper manner, if the application itself is killed or crashes. Hope this helps. Mike-ro-soft

Anonymous User -January 21, 2005

I created a generic service similar to SRVANY and it signals a named event when it recieves a stop control. So the application that my service spawns can open and monitor that named event and quit when it is signaled. (Named event have cross process scope) http://abblett.co.uk/projects/GenSvc.php

Anonymous User -January 23, 2005

U cannot use mapped drives as a path to your application. U need to use the UNC eg: \\host\share\path\app.exe

Anonymous User -January 23, 2005

I wrote a Script and installed it on all Hosts. The Problem is, that it needs Admin Rights to work well. Is there a chance to change the Log On Account of a Service with a scipt ? Hope someone can help. Thanks Bernie

Anonymous User -February 25, 2005

ChangeServiceConfig() http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/changeserviceconfig.asp can alter the configuration of a service including the log on account. If ur using a script such as VB you could use rundll32 to access that function.

Anonymous User -February 25, 2005

I want to start more than one command with srvany. e.g. c: cd \programs\... application.exe but there is only one row in registriy (Parameters)! What can I do?

Anonymous User -May 19, 2005

Regarding security for the srvany user: You can use any user, as long as it has authority to the directories you need to use, and the user right "Log on as a service" has been granted to the account. --Joe Netwacker

Anonymous User -June 29, 2005

Can you configure srvany to send a WM_CLOSE window to the child process. I think it send a WM_TERMINATE

Anonymous User -June 29, 2005
Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement