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

相关推荐
xcyxiner21 小时前
DicomViewer (dcmtk读取dcm文件)5
qt
xcyxiner2 天前
DicomViewer (后台线程处理文件)4
qt
xcyxiner2 天前
DicomViewer (添加模型类)3
qt
xcyxiner3 天前
DicomViewer (目录调整) 2
qt
xcyxiner3 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
LDR0065 天前
Type-C 快充全面升级!LDR6601 赋能个人护理便携电机,重塑剃须刀 / 理发器新体验
c语言·开发语言
雪碧聊技术5 天前
Tree.js是什么?一文讲透
开发语言·javascript·ecmascript
码云数智-园园5 天前
C++20 Modules 模块详解
java·开发语言·spring
swordbob5 天前
NIO的channel中什么是 fd(File Descriptor,文件描述符)
java·开发语言·nio
qq_369224335 天前
Windows全系通用!ntdll.dll文件丢失、报错、闪退问题的完整排查与修复教程
windows·dll·dll修复·dll丢失·dll错误