HOWTO: Pass Arguments to a Service

ID: Q171143

The information in this article applies to:

SUMMARY

A Windows NT service can receive arguments. A demand-started service may receive arguments (also known as service arguments) through the StartService API. The service receives the arguments via it's Service_Main(), which can be defined as follows:

   void Service_Main(DWORD dwArgc, LPTSTR *lpszArgv)

NOTE: The Service_Main() entry point is user defined through the SERVICE_TABLE_ENTRY structure. The first argument passed to the Service_Main() indicates the number of parameters passed to the service while the second parameter contains an array of strings containing the arguments. The first string in the array will always be the name of the service. Any additional strings in the array will contain the arguments passed to the service through the StartService API.

MORE INFORMATION

Sometimes an auto-started service may want to receive arguments. Unfortunately, there is no method to pass arguments to a service through it's Service_Main(). These arguments can only be passed to a service through it's main(). (This entry point is defined by the user via the compiler. The default entry point is named main().) You can setup the arguments when you install the service through the CreateService API. For example, to pass the arguments "franki" and "hosun" to an auto-started service, you would pass the following string as the lpBinaryPathName parameter of CreateService():

   "d:\\foo\\foo.exe franki hosun"

When the service starts, it can obtain these arguments via it's main() through argc and argv. To modify the arguments to a service, you call the ChangeServiceConfig API and modify the lpBinaryPathName parameter.

If you have configured your service to run as a shared service, each service originating from the service process will receive the same arguments when it is auto-started. You cannot pass unique arguments to each of the services using main(). The services will either have to create a file or registry key to pass unique arguments to each of the services being shared.

Keywords          : kbprg kbKernBase kbService kbGrpKernBase 
Version           : 3.51 4.0
Platform          : NT WINDOWS
Issue type        : kbhowto

Last Reviewed: July 30, 1998