我想创建一个Windows服务,它将在启动时运行批处理文件。
我知道API就像createservice
,但我想要的是当我从Start service
说Service Control Manager
时我想用参数start
调用我的批处理文件,当我说停止时我想用stop
参数
答案 0 :(得分:1)
每当您执行任何Windows服务操作(如启动/停止/暂停服务)时,您通过RegisterServiceCtrlHandler()注册的服务主控制器处理函数都会收到SERVICE_CONTROL_PAUSE,SERVICE_CONTROL_CONTINUE,SERVICE_CONTROL_STOP等消息...
您可以创建单独的函数来调用具有不同输入参数的bat文件,并在收到适当的服务消息时调用这些函数。
void ServiceMainCntrlHandler(unsigned long request) { 开关(请求) { / *收到服务暂停信号* / 案例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;
}
}