Windows service custom commands from command line

2020-07-24 23:38发布

问题:

I've implemented a Windows Service with custom commands overriding OnCustomCommand method.

I can access these custom commands from another .net application with:

ServiceController Controller = new ServiceController("MyWindowsService");
if (Controller.Status == ServiceControllerStatus.Running)
  {
      Controller.ExecuteCommand(128);
  }

But, can I access these custom commands from the command line (cmd) the same way I can start/stop/... the service?

EDIT: (Without creating a middleware application to handle the service, just with standard tools)

回答1:

You can use the Service Control command line tool sc

> sc control MyWindowsService 128

(Under the hood everything is using Win32's ControlService API.)