HOWTO: Pass Arguments to a Service

Last reviewed: July 11, 1997
Article ID: Q171143
The information in this article applies to:
  • Microsoft Win32 Application Programming Interface (API) included with: - Microsoft Windows NT versions 3.51, 4.0

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 : BseService kbprg
Version : 3.51 4.0
Platform : NT WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 11, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.