//Try the following codes to stop/start a service SC_HANDLE hSCM = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); SC_HANDLE hService = ::OpenService(hSCM, "ServiceName", SERVICE_STOP ); SERVICE_STATUS status; ::ControlService(hService, SERVICE_CONTROL_STOP, &status); ::CloseServiceHandle(hService); ::CloseServiceHandle(hSCM); //Replace the ControlService line with the following to start the service StartService( hService,0, NULL); //Wish it works ! //Good Lucks!
SC_HANDLE hSCM = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
SC_HANDLE hService = ::OpenService(hSCM, "ServiceName", SERVICE_STOP );
SERVICE_STATUS status;
::ControlService(hService, SERVICE_CONTROL_STOP, &status);
::CloseServiceHandle(hService);
::CloseServiceHandle(hSCM);
//Replace the ControlService line with the following to start the service
StartService( hService,0, NULL);
//Wish it works !
//Good Lucks!
1.OpenSCManager
2.OpenService
3.用ContrlService函数向指定服务发控制代码(如启动、暂停、继续、停止等)
也可以发送用户定义控制码以完成用户特定功能。上述控制代码由服务端的控制请求处理函数负责响应处理。参见Win32API : RegisterServiceCtrlHandler