• Qt重启windows服务


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

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

    1. #include "windows.h"
    2. #pragma comment(lib, "advapi32.lib")
    3. bool ConfigCenter::ReStartServiceByName(std::string strDestServiceName)
    4. {
    5. bool bServiceStatus = FALSE;
    6. SC_HANDLE schSCManager = nullptr ;
    7. SC_HANDLE schService = nullptr ;
    8. DWORD dwBytesNeeded = 0 ;
    9. SERVICE_STATUS_PROCESS ssStatus;
    10. char szSvcName[MAX_PATH] = {0};
    11. memcpy_s(szSvcName,MAX_PATH,strDestServiceName.c_str(),strDestServiceName.length());
    12. //! 获取一个服务控制管理器数据库的句柄
    13. schSCManager = OpenSCManager(
    14. nullptr , // local computer
    15. nullptr , // ServicesActive database
    16. SC_MANAGER_ALL_ACCESS); // full access rights
    17. if(schSCManager == nullptr) {
    18. qDebug() << "OpenSCManager fail" << endl;
    19. return bServiceStatus;
    20. }
    21. //! 获取该服务在服务控制管理器数据库中的句柄
    22. schService = OpenServiceA(
    23. schSCManager, // SCM database
    24. szSvcName, // name of service
    25. SERVICE_ALL_ACCESS/* | DELETE*/); // full access
    26. if(schService == nullptr) {
    27. CloseServiceHandle(schSCManager);
    28. qDebug() << "Get a handle to the service fail" << endl;
    29. return bServiceStatus;
    30. }
    31. //! 查询该服务的当前状态
    32. if(!QueryServiceStatusEx(
    33. schService, // handle to service
    34. SC_STATUS_PROCESS_INFO, // information level
    35. (LPBYTE) &ssStatus, // address of structure
    36. sizeof (SERVICE_STATUS_PROCESS), // size of structure
    37. &dwBytesNeeded ) ) // size needed if buffer is too small
    38. {
    39. CloseServiceHandle(schService);
    40. CloseServiceHandle(schSCManager);
    41. qDebug() << "QueryServiceStatusEx fail" << endl;
    42. return bServiceStatus;
    43. }
    44. else
    45. {
    46. switch (ssStatus.dwCurrentState)
    47. {
    48. case SERVICE_STOPPED:
    49. case SERVICE_STOP_PENDING:
    50. qDebug() << szSvcName << " Service status is Stop" << endl;
    51. break ;
    52. case SERVICE_PAUSED:
    53. case SERVICE_PAUSE_PENDING:
    54. qDebug() << szSvcName << " Service status is Pause" << endl;
    55. break ;
    56. case SERVICE_CONTINUE_PENDING:
    57. case SERVICE_RUNNING:
    58. case SERVICE_START_PENDING:
    59. qDebug() << szSvcName << " Service status is Running" << endl;
    60. bServiceStatus = TRUE;
    61. break ;
    62. }
    63. }
    64. //! 停止该服务
    65. if(bServiceStatus == TRUE) {
    66. SERVICE_STATUS status;
    67. if (!ControlService(schService, SERVICE_CONTROL_STOP, &status)) {
    68. qDebug() << "ControlService failed with error:" << GetLastError();
    69. return !bServiceStatus;
    70. }
    71. }
    72. //! 启动该服务
    73. StartService(schService,0,nullptr);
    74. Sleep(500);
    75. //! 获得服务的当前状态
    76. QueryServiceStatusEx(schService,SC_STATUS_PROCESS_INFO,(LPBYTE) &ssStatus,sizeof (SERVICE_STATUS_PROCESS),&dwBytesNeeded );
    77. if (SERVICE_RUNNING == ssStatus.dwCurrentState)
    78. {
    79. bServiceStatus = TRUE;
    80. }
    81. CloseServiceHandle(schService);
    82. CloseServiceHandle(schSCManager);
    83. return bServiceStatus;
    84. }

    参考文章:https://www.cnblogs.com/TechNomad/p/17669231.html

  • 相关阅读:
    白盒子测试总结
    计算机网络——TCP/IP网络层次模型
    谁说文艺青年开花店必亏,我用3年时间挣了20万
    【k8s总结】
    Java学习笔记 --- Collections工具类
    Java中级面试题记录(四)
    「随笔」Vue技能树测评 # CSDN 技能树评测征文
    系统kafka不消费-topic问题
    移动端开发环境 - Mac
    opencv用自适应直方图均衡化函数cv2.createCLAHE()提高对比度
  • 原文地址:https://blog.csdn.net/wisdomroc/article/details/134338559