// 启动服务
SC_HANDLE schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); // 创建到服务控制管理器的连接
if (schSCManager == NULL)
{
return FALSE;
}
SC_HANDLE schService = OpenService(schSCManager, _T("*"), SERVICE_ALL_ACCESS | DELETE); // 打开服务, *表示服务名称
if (schService == NULL)
{
return FALSE;
}
SERVICE_STATUS service;
QueryServiceStatus(schService, &service);
if (service.dwCurrentState == SERVICE_STOPPED)
{
StartService(schService, 0, NULL);
CloseServiceHandle(schSCManager);
CloseServiceHandle(schService);
}
// 停止服务
SC_HANDLE schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (schSCManager == NULL)
{
return FALSE;
}
SC_HANDLE schService = OpenService(schSCManager, _T("*"), SERVICE_ALL_ACCESS | DELETE);
if (schService == NULL)
{
return FALSE;
}
SERVICE_STATUS service;
QueryServiceStatus(schService, &service);
if (service.dwCurrentState == SERVICE_RUNNING)
{
ControlService(schService, SERVICE_CONTROL_STOP, &service);
CloseServiceHandle(schSCManager);
CloseServiceHandle(schService);
}