日常开发中,会遇到改变某个服务的参数,并进行重启(例如Redis断电恢复机制)
需要程序拥有UAC权限,并且调用如下API才能对windows服务进行重启:
#include "windows.h"#pragma comment(lib, "advapi32.lib")bool ConfigCenter::ReStartServiceByName(std::string strDestServiceName)
{bool bServiceStatus = FALSE;SC_HANDLE schSCManager = nullptr ;SC_HANDLE schService = nullptr ;DWORD dwBytesNeeded = 0 ;SERVICE_STATUS_PROCESS ssStatus;char szSvcName[MAX_PATH] = {0};memcpy_s(szSvcName,MAX_PATH,strDestServiceName.c_str(),strDestServiceName.length());//! 获取一个服务控制管理器数据库的句柄schSCManager = OpenSCManager(nullptr , // local computernullptr , // ServicesActive databaseSC_MANAGER_ALL_ACCESS); // full access rightsif(schSCManager == nullptr) {qDebug() << "OpenSCManager fail" << endl;return bServiceStatus;}//! 获取该服务在服务控制管理器数据库中的句柄schService = OpenServiceA(schSCManager, // SCM databaseszSvcName, // name of serviceSERVICE_ALL_ACCESS/* | DELETE*/); // full accessif(schService == nullptr) {CloseServiceHandle(schSCManager);qDebug() << "Get a handle to the service fail" << endl;return bServiceStatus;}//! 查询该服务的当前状态if(!QueryServiceStatusEx(schService, // handle to serviceSC_STATUS_PROCESS_INFO, // information level(LPBYTE) &ssStatus, // address of structuresizeof (SERVICE_STATUS_PROCESS), // size of structure&dwBytesNeeded ) ) // size needed if buffer is too small{CloseServiceHandle(schService);CloseServiceHandle(schSCManager);qDebug() << "QueryServiceStatusEx fail" << endl;return bServiceStatus;}else{switch (ssStatus.dwCurrentState){case SERVICE_STOPPED:case SERVICE_STOP_PENDING:qDebug() << szSvcName << " Service status is Stop" << endl;break ;case SERVICE_PAUSED:case SERVICE_PAUSE_PENDING:qDebug() << szSvcName << " Service status is Pause" << endl;break ;case SERVICE_CONTINUE_PENDING:case SERVICE_RUNNING:case SERVICE_START_PENDING:qDebug() << szSvcName << " Service status is Running" << endl;bServiceStatus = TRUE;break ;}}//! 停止该服务if(bServiceStatus == TRUE) {SERVICE_STATUS status;if (!ControlService(schService, SERVICE_CONTROL_STOP, &status)) {qDebug() << "ControlService failed with error:" << GetLastError();return !bServiceStatus;}}//! 启动该服务StartService(schService,0,nullptr);Sleep(500);//! 获得服务的当前状态QueryServiceStatusEx(schService,SC_STATUS_PROCESS_INFO,(LPBYTE) &ssStatus,sizeof (SERVICE_STATUS_PROCESS),&dwBytesNeeded );if (SERVICE_RUNNING == ssStatus.dwCurrentState){bServiceStatus = TRUE;}CloseServiceHandle(schService);CloseServiceHandle(schSCManager);return bServiceStatus;
}
参考文章:https://www.cnblogs.com/TechNomad/p/17669231.html