Qt重启windows服务

日常开发中,会遇到改变某个服务的参数,并进行重启(例如Redis断电恢复机制)

需要程序拥有UAC权限,并且调用如下API才能对windows服务进行重启:

cpp 复制代码
#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 computer
        nullptr ,                           // ServicesActive database
        SC_MANAGER_ALL_ACCESS);             // full access rights

    if(schSCManager == nullptr) {
        qDebug() << "OpenSCManager fail" << endl;
        return bServiceStatus;
    }

    //! 获取该服务在服务控制管理器数据库中的句柄
    schService = OpenServiceA(
        schSCManager,                       // SCM database
        szSvcName,                          // name of service
        SERVICE_ALL_ACCESS/* | DELETE*/);   // full access

    if(schService == nullptr) {
        CloseServiceHandle(schSCManager);
        qDebug() << "Get a handle to the service fail" << endl;
        return bServiceStatus;
    }

    //! 查询该服务的当前状态
    if(!QueryServiceStatusEx(
        schService,                         // handle to service
        SC_STATUS_PROCESS_INFO,             // information level
        (LPBYTE) &ssStatus,                 // address of structure
        sizeof (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

相关推荐
gywl16 分钟前
openEuler VM虚拟机操作(期末考试)
linux·服务器·网络·windows·http·centos
轻口味20 分钟前
命名空间与模块化概述
开发语言·前端·javascript
某柚啊1 小时前
Windows开启IIS后依然出现http error 503.the service is unavailable
windows·http
晓纪同学1 小时前
QT-简单视觉框架代码
开发语言·qt
威桑1 小时前
Qt SizePolicy详解:minimum 与 minimumExpanding 的区别
开发语言·qt·扩张策略
飞飞-躺着更舒服1 小时前
【QT】实现电子飞行显示器(简易版)
开发语言·qt
fyzy1 小时前
Qt获取本地计算的CPU温度
qt
cbdg37571 小时前
Qt 6 QML Settings location 不创建指定路径文件
qt
了一li1 小时前
Qt中的QProcess与Boost.Interprocess:实现多进程编程
服务器·数据库·qt
杨德杰1 小时前
QT网络(一):主机信息查询
网络·qt