0

我想创建将在启动时运行批处理文件的 Windows 服务。我知道 API 之类的createservice,但我想要的是当我说我想用参数调用我的批处理文件时Start service,当我说停止时我想用参数调用同一个批处理文件Service Control Managerstartstop

4

1 回答 1

1

每当您执行任何 Windows 服务操作(例如启动/停止/暂停服务)时,您通过 RegisterServiceCtrlHandler() 注册的服务主控制器处理程序函数都会接收诸如 SERVICE_CONTROL_PAUSE、SERVICE_CONTROL_CONTINUE、SERVICE_CONTROL_STOP 等消息...

您可以创建单独的函数来调用具有不同输入参数的 bat 文件,并且可以在收到适当的服务消息时调用这些函数。

void ServiceMainCntrlHandler(unsigned long request) { switch(request) { /* 收到服务暂停信号 */ case SERVICE_CONTROL_PAUSE: // 将服务当前状态改为暂停 ServiceStatus.dwCurrentState = SERVICE_PAUSED; SetServiceStatus (hStatus, &ServiceStatus);

  // TODO: Call appropriate function
  break;

/* Received service continue signal */
case SERVICE_CONTROL_CONTINUE:
  // Change the service current status to started
  ServiceStatus.dwCurrentState  = SERVICE_RUNNING;
  SetServiceStatus (hStatus, &ServiceStatus);

  // TODO: Call appropriate function
  break;

/* Received service stop signal */
case SERVICE_CONTROL_STOP:
  // Change the service current status to stopped
  ServiceStatus.dwWin32ExitCode = 0;
  ServiceStatus.dwCurrentState  = SERVICE_STOPPED;
  ServiceStatus.dwCheckPoint        = 0;
  ServiceStatus.dwWaitHint      = 0;
  SetServiceStatus (hStatus, &ServiceStatus);

  // TODO: Call the function which will invoke the bat file with input parameter as "stop"
  break;         
default:  break;

}
}

于 2012-07-04T12:19:59.533 回答