当前位置:   article > 正文

QT 操作Windows系统服务_qt windows服务

qt windows服务

        Windows服务是在Windows操作系统上运行的后台应用程序,它们在系统启动时自动启动,并在后台持续运行,不需要用户交互。Windows服务的作用包括但不限于以下几个方面:1. 提供系统功能:许多Windows服务提供了系统级的功能和服务,如网络连接、文件共享、打印服务、安全认证、远程管理等。这些服务为用户和其他应用程序提供了基础设施和功能支持。2. 自动化任务:Windows服务可以用于执行自动化任务,如定期备份、数据同步、日志记录、定时任务等。它们可以在后台运行,无需用户干预,提供可靠的自动化功能。3. 后台通信和消息传递:Windows服务可以用于实现后台通信和消息传递,如消息队列、进程间通信等。它们可以在不同的应用程序之间传递数据和消息,实现应用程序之间的协作和集成。4. 系统监控和管理:Windows服务可以用于监控和管理系统状态和资源,如性能监控、事件日志、服务管理等。它们可以收集系统信息、监测系统性能,并提供管理接口和功能,帮助管理员维护和管理系统。
        Windows服务是在后台运行的应用程序,提供了系统级的功能和服务,执行自动化任务,实现后台通信和消息传递,以及监控和管理系统。它们为Windows操作系统提供了稳定、可靠和高效的功能支持。

        需要注意程序需要使用管理员权限启动才可以操作服务

QT 操作Windows系统服务目录

1 创建服务

2 删除服务

3 打开服务

4 关闭服务

5 启动服务

6 停止服务

7 自动启动

8 手动启动

9 查询服务

10 .h源文件


 本文作者原创,转载请附上文章出处与本文链接。

1 创建服务

  1. ///@ 创建服务
  2. void MainWindow::newService()
  3. {
  4. SC_HANDLE schandle = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); //打开服务管理
  5. SC_HANDLE h = ::CreateService(schandle,
  6. L"ServiceAPI", //服务名
  7. L"ServiceAPI-Test", //显示用的服务名
  8. SERVICE_ALL_ACCESS, //所有访问权限
  9. SERVICE_WIN32_OWN_PROCESS, //私有类型
  10. SERVICE_AUTO_START, //自启动类型
  11. SERVICE_ERROR_NORMAL, //忽略错误处理
  12. L"D:/ServiceAPI.exe", //应用程序路径
  13. nullptr, nullptr, nullptr, nullptr, nullptr);
  14. ::CloseServiceHandle(h); //关闭
  15. ::CloseServiceHandle(schandle);//关闭
  16. ::getchar(); //暂停
  17. }

2 删除服务

  1. ///@ 删除服务
  2. void MainWindow::deleteService()
  3. {
  4. SC_HANDLE schandle = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); //打开服务管理
  5. SC_HANDLE h = ::OpenService(schandle, L"ServiceAPI", SERVICE_ALL_ACCESS); //打开要删除的服务
  6. if (h != NULL)
  7. {
  8. if (::DeleteService(h))
  9. {
  10. qDebug() << "Service deleted successfully.";
  11. }
  12. else
  13. {
  14. if(GetLastError() == 1072)
  15. qDebug() << "Please restart Windows .....";
  16. else
  17. qDebug() << "Failed to delete service. Error code: " << GetLastError();
  18. }
  19. ::CloseServiceHandle(h); //关闭服务句柄
  20. }
  21. else
  22. {
  23. qDebug() << "Failed to open service. Error code: " << GetLastError();
  24. }
  25. ::CloseServiceHandle(schandle); //关闭服务管理句柄
  26. ::getchar(); //暂停
  27. }

3 打开服务

  1. ///@ 打开服务
  2. void MainWindow::openService()
  3. {
  4. // 打开服务控制管理器
  5. hSCM = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
  6. if (hSCM == NULL) {
  7. qDebug() << "OpenSCManager failed with error:" << GetLastError();
  8. return;
  9. }
  10. // 打开Windows 服务
  11. hService = OpenService(hSCM, TEXT("SQLWriter"), SERVICE_ALL_ACCESS);
  12. if (hService == NULL) {
  13. qDebug() << "OpenService failed with error:" << GetLastError();
  14. CloseServiceHandle(hSCM);
  15. return;
  16. }
  17. }

4 关闭服务

  1. ///@ 关闭服务
  2. void MainWindow::closeService()
  3. {
  4. // 关闭服务和服务控制管理器的句柄
  5. CloseServiceHandle(hService);
  6. CloseServiceHandle(hSCM);
  7. }

5 启动服务

  1. ///@ 启动服务
  2. void MainWindow::startService()
  3. {
  4. if (!StartService(hService, 0, NULL)) {
  5. qDebug() << "StartService failed with error:" << GetLastError();
  6. }
  7. }

6 停止服务

  1. ///@ 停止服务
  2. void MainWindow::stopService()
  3. {
  4. SERVICE_STATUS status;
  5. if (!ControlService(hService, SERVICE_CONTROL_STOP, &status)) {
  6. qDebug() << "ControlService failed with error:" << GetLastError();
  7. }
  8. }

7 自动启动

  1. ///@ 自动启用服务
  2. void MainWindow::enableService()
  3. {
  4. enable = true;
  5. if (!ChangeServiceConfig(
  6. hService, // handle of service
  7. SERVICE_NO_CHANGE, // service type: no change
  8. enable ? SERVICE_DEMAND_START : SERVICE_DISABLED,
  9. SERVICE_NO_CHANGE, // error control: no change
  10. NULL, // binary path: no change
  11. NULL, // load order group: no change
  12. NULL, // tag ID: no change
  13. NULL, // dependencies: no change
  14. NULL, // service start name: no change
  15. NULL, // password: no change
  16. NULL)) // display name: no change
  17. {
  18. qDebug() << "ChangeServiceConfig failed with error:" << GetLastError();
  19. }
  20. }

8 手动启动

  1. ///@ 手动启用服务
  2. void MainWindow::manualService()
  3. {
  4. enable = true;
  5. if (!ChangeServiceConfig(
  6. hService, // handle of service
  7. SERVICE_NO_CHANGE, // service type: no change
  8. enable ? SERVICE_AUTO_START : SERVICE_DISABLED, // service start type SERVICE_DEMAND_START
  9. SERVICE_NO_CHANGE, // error control: no change
  10. NULL, // binary path: no change
  11. NULL, // load order group: no change
  12. NULL, // tag ID: no change
  13. NULL, // dependencies: no change
  14. NULL, // service start name: no change
  15. NULL, // password: no change
  16. NULL)) // display name: no change
  17. {
  18. qDebug() << "ChangeServiceConfig failed with error:" << GetLastError();
  19. }
  20. }

9 查询服务

  1. void MainWindow::findService()
  2. {
  3. QSettings servicesSettings("HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services", QSettings::NativeFormat);
  4. QStringList services = servicesSettings.childGroups();
  5. for (const QString& service : services) {
  6. printServiceInfo(service);
  7. }
  8. }
  1. void MainWindow::printServiceInfo(const QString& serviceName)
  2. {
  3. qDebug() << "服务名称: " << serviceName; //Service Name
  4. QSettings serviceSettings("HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\" + serviceName, QSettings::NativeFormat);
  5. QStringList keys = serviceSettings.allKeys();
  6. if(serviceSettings.value("DisplayName").toString() == "")
  7. return;
  8. qDebug() << "显示名称: " << serviceSettings.value("DisplayName").toString();
  9. qDebug() << "描述: " << serviceSettings.value("Description").toString();
  10. qDebug() << "映像路径: " << serviceSettings.value("ImagePath").toString();
  11. QString displayName = QCoreApplication::translate("WalletService", "@%SystemRoot%\\System32\\WalletService.dll,-1000");
  12. QString description = QCoreApplication::translate("WalletService", "@%SystemRoot%\\System32\\WalletService.dll,-1001");
  13. qDebug() << "显示名称:" << displayName;
  14. qDebug() << "描述:" << description;
  15. }

10 .h源文件

  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3. #include <QMainWindow>
  4. #include <QDebug>
  5. #include <QSettings>
  6. #include <windows.h>
  7. #include <winsvc.h>
  8. #include <iostream>
  9. #pragma comment(lib, "advapi32.lib")
  10. #define MAX_SERVICE_SIZE 1024 * 64
  11. #define MAX_QUERY_SIZE 1024 * 8
  12. #pragma execution_character_set("utf-8")
  13. QT_BEGIN_NAMESPACE
  14. namespace Ui { class MainWindow; }
  15. QT_END_NAMESPACE
  16. class MainWindow : public QMainWindow
  17. {
  18. Q_OBJECT
  19. public:
  20. MainWindow(QWidget *parent = nullptr);
  21. ~MainWindow();
  22. SC_HANDLE hSCM;
  23. SC_HANDLE hService;
  24. private:
  25. Ui::MainWindow *ui;
  26. bool enable;
  27. void openService();
  28. void closeService();
  29. void findService();
  30. void printServiceInfo(const QString& serviceName);
  31. void startService();
  32. void stopService();
  33. void newService();
  34. void deleteService();
  35. void enableService();
  36. void manualService();
  37. void disabledService();
  38. };
  39. #endif // MAINWINDOW_H

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/895164
推荐阅读
相关标签
  

闽ICP备14008679号