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

相关推荐
重生之我在20年代敲代码6 分钟前
strncpy函数的使用和模拟实现
c语言·开发语言·c++·经验分享·笔记
爱上语文7 分钟前
Springboot的三层架构
java·开发语言·spring boot·后端·spring
编程零零七2 小时前
Python数据分析工具(三):pymssql的用法
开发语言·前端·数据库·python·oracle·数据分析·pymssql
2401_858286113 小时前
52.【C语言】 字符函数和字符串函数(strcat函数)
c语言·开发语言
铁松溜达py3 小时前
编译器/工具链环境:GCC vs LLVM/Clang,MSVCRT vs UCRT
开发语言·网络
everyStudy3 小时前
JavaScript如何判断输入的是空格
开发语言·javascript·ecmascript
C-SDN花园GGbond5 小时前
【探索数据结构与算法】插入排序:原理、实现与分析(图文详解)
c语言·开发语言·数据结构·排序算法
迷迭所归处6 小时前
C++ —— 关于vector
开发语言·c++·算法
架构文摘JGWZ6 小时前
Java 23 的12 个新特性!!
java·开发语言·学习
leon6256 小时前
优化算法(一)—遗传算法(Genetic Algorithm)附MATLAB程序
开发语言·算法·matlab