How to pass a parameter to a windows service once and for all at install instead of each start

前端 未结 8 2062
南旧
南旧 2020-12-09 04:13

We have a Windows Service application that can accept command line parameters like:

MyService -option 

So far, when we want to start the

相关标签:
8条回答
  • 2020-12-09 04:31

    Arguments passed on the command-line via ImagePath are accessible in main() or via GetCommandLine(). You could install with command-line args and then in your ServiceMain, check to see if any arguments were passed in the lpszArgs parameter. If not, call GetCommandLine and see if any were passed that way.

    0 讨论(0)
  • 2020-12-09 04:35
    sc config MyService binPath= MyService.exe -option
    

    Update

    The individual service parameters are stored in the the registry at the key HKLM\SYSTEM\CurrentControlSet\Services\<serviceName>\Parameters. I'm not sure though how the parameters are passed to the service. I believe SCM reads these values then when it calls StartService it passes them to the ServiceMain callback.

    0 讨论(0)
  • 2020-12-09 04:36

    How about putting the parameter in a config file?

    0 讨论(0)
  • 2020-12-09 04:38

    Powershell can do this but you have to use .Net to achieve it.

    new-Object System.ServiceProcess.ServiceController("$ServiceName",$ComputerName)).Start("$Parameter")
    
    0 讨论(0)
  • 2020-12-09 04:38

    Use the SC (service control) command, it gives you a lot more options than just start & stop.

    DESCRIPTION:
              SC is a command line program used for communicating with the
              NT Service Controller and services.
      USAGE:
          sc <server> [command] [service name]  ...
    
          The option <server> has the form "\\ServerName"
          Further, help on commands can be obtained by typing: "sc [command]"
          Commands:
            query-----------Queries the status for a service, or
                            enumerates the status for types of services.
            queryex---------Queries the extended status for a service, or
                            enumerates the status for types of services.
            start-----------Starts a service.
            pause-----------Sends a PAUSE control request to a service.
            interrogate-----Sends an INTERROGATE control request to a service.
            continue--------Sends a CONTINUE control request to a service.
            stop------------Sends a STOP request to a service.
            config----------Changes the configuration of a service (persistent).
            description-----Changes the description of a service.
            failure---------Changes the actions taken by a service upon failure.
            qc--------------Queries the configuration information for a service.
            qdescription----Queries the description for a service.
            qfailure--------Queries the actions taken by a service upon failure.
            delete----------Deletes a service (from the registry).
            create----------Creates a service. (adds it to the registry).
            control---------Sends a control to a service.
            sdshow----------Displays a service's security descriptor.
            sdset-----------Sets a service's security descriptor.
            GetDisplayName--Gets the DisplayName for a service.
            GetKeyName------Gets the ServiceKeyName for a service.
            EnumDepend------Enumerates Service Dependencies.
    
          The following commands don't require a service name:
          sc <server> <command> <option>
            boot------------(ok | bad) Indicates whether the last boot should
                            be saved as the last-known-good boot configuration
            Lock------------Locks the Service Database
            QueryLock-------Queries the LockStatus for the SCManager Database
      EXAMPLE:
              sc start MyService
    
    0 讨论(0)
  • 2020-12-09 04:42

    If there is more than one service with the same executable then you would be installing them with different service names. You could refer to the service name instead of the parameters.

    To get the service name you can use this How can a Windows Service determine its ServiceName?

    0 讨论(0)
提交回复
热议问题