赞
踩
Windows服务是在Windows操作系统上运行的后台应用程序,它们在系统启动时自动启动,并在后台持续运行,不需要用户交互。Windows服务的作用包括但不限于以下几个方面:1. 提供系统功能:许多Windows服务提供了系统级的功能和服务,如网络连接、文件共享、打印服务、安全认证、远程管理等。这些服务为用户和其他应用程序提供了基础设施和功能支持。2. 自动化任务:Windows服务可以用于执行自动化任务,如定期备份、数据同步、日志记录、定时任务等。它们可以在后台运行,无需用户干预,提供可靠的自动化功能。3. 后台通信和消息传递:Windows服务可以用于实现后台通信和消息传递,如消息队列、进程间通信等。它们可以在不同的应用程序之间传递数据和消息,实现应用程序之间的协作和集成。4. 系统监控和管理:Windows服务可以用于监控和管理系统状态和资源,如性能监控、事件日志、服务管理等。它们可以收集系统信息、监测系统性能,并提供管理接口和功能,帮助管理员维护和管理系统。
Windows服务是在后台运行的应用程序,提供了系统级的功能和服务,执行自动化任务,实现后台通信和消息传递,以及监控和管理系统。它们为Windows操作系统提供了稳定、可靠和高效的功能支持。
需要注意程序需要使用管理员权限启动才可以操作服务
QT 操作Windows系统服务目录
本文作者原创,转载请附上文章出处与本文链接。
- ///@ 创建服务
- void MainWindow::newService()
- {
- SC_HANDLE schandle = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); //打开服务管理
- SC_HANDLE h = ::CreateService(schandle,
- L"ServiceAPI", //服务名
- L"ServiceAPI-Test", //显示用的服务名
- SERVICE_ALL_ACCESS, //所有访问权限
- SERVICE_WIN32_OWN_PROCESS, //私有类型
- SERVICE_AUTO_START, //自启动类型
- SERVICE_ERROR_NORMAL, //忽略错误处理
- L"D:/ServiceAPI.exe", //应用程序路径
- nullptr, nullptr, nullptr, nullptr, nullptr);
-
- ::CloseServiceHandle(h); //关闭
- ::CloseServiceHandle(schandle);//关闭
- ::getchar(); //暂停
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
- ///@ 删除服务
- void MainWindow::deleteService()
- {
- SC_HANDLE schandle = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); //打开服务管理
- SC_HANDLE h = ::OpenService(schandle, L"ServiceAPI", SERVICE_ALL_ACCESS); //打开要删除的服务
-
- if (h != NULL)
- {
- if (::DeleteService(h))
- {
- qDebug() << "Service deleted successfully.";
- }
- else
- {
- if(GetLastError() == 1072)
- qDebug() << "Please restart Windows .....";
- else
- qDebug() << "Failed to delete service. Error code: " << GetLastError();
- }
- ::CloseServiceHandle(h); //关闭服务句柄
- }
- else
- {
- qDebug() << "Failed to open service. Error code: " << GetLastError();
- }
-
- ::CloseServiceHandle(schandle); //关闭服务管理句柄
- ::getchar(); //暂停
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
- ///@ 打开服务
- void MainWindow::openService()
- {
- // 打开服务控制管理器
- hSCM = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
-
- if (hSCM == NULL) {
- qDebug() << "OpenSCManager failed with error:" << GetLastError();
- return;
- }
-
- // 打开Windows 服务
- hService = OpenService(hSCM, TEXT("SQLWriter"), SERVICE_ALL_ACCESS);
-
- if (hService == NULL) {
- qDebug() << "OpenService failed with error:" << GetLastError();
- CloseServiceHandle(hSCM);
- return;
- }
- }
-
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
- ///@ 关闭服务
- void MainWindow::closeService()
- {
- // 关闭服务和服务控制管理器的句柄
- CloseServiceHandle(hService);
- CloseServiceHandle(hSCM);
- }
- ///@ 启动服务
- void MainWindow::startService()
- {
- if (!StartService(hService, 0, NULL)) {
- qDebug() << "StartService failed with error:" << GetLastError();
- }
- }
- ///@ 停止服务
- void MainWindow::stopService()
- {
- SERVICE_STATUS status;
- if (!ControlService(hService, SERVICE_CONTROL_STOP, &status)) {
- qDebug() << "ControlService failed with error:" << GetLastError();
- }
- }
- ///@ 自动启用服务
- void MainWindow::enableService()
- {
- enable = true;
- if (!ChangeServiceConfig(
- hService, // handle of service
- SERVICE_NO_CHANGE, // service type: no change
- enable ? SERVICE_DEMAND_START : SERVICE_DISABLED,
- SERVICE_NO_CHANGE, // error control: no change
- NULL, // binary path: no change
- NULL, // load order group: no change
- NULL, // tag ID: no change
- NULL, // dependencies: no change
- NULL, // service start name: no change
- NULL, // password: no change
- NULL)) // display name: no change
- {
- qDebug() << "ChangeServiceConfig failed with error:" << GetLastError();
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
- ///@ 手动启用服务
- void MainWindow::manualService()
- {
- enable = true;
- if (!ChangeServiceConfig(
- hService, // handle of service
- SERVICE_NO_CHANGE, // service type: no change
- enable ? SERVICE_AUTO_START : SERVICE_DISABLED, // service start type SERVICE_DEMAND_START
- SERVICE_NO_CHANGE, // error control: no change
- NULL, // binary path: no change
- NULL, // load order group: no change
- NULL, // tag ID: no change
- NULL, // dependencies: no change
- NULL, // service start name: no change
- NULL, // password: no change
- NULL)) // display name: no change
- {
- qDebug() << "ChangeServiceConfig failed with error:" << GetLastError();
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
- void MainWindow::findService()
- {
- QSettings servicesSettings("HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services", QSettings::NativeFormat);
- QStringList services = servicesSettings.childGroups();
-
- for (const QString& service : services) {
- printServiceInfo(service);
- }
- }
- void MainWindow::printServiceInfo(const QString& serviceName)
- {
- qDebug() << "服务名称: " << serviceName; //Service Name
- QSettings serviceSettings("HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\" + serviceName, QSettings::NativeFormat);
- QStringList keys = serviceSettings.allKeys();
-
-
- if(serviceSettings.value("DisplayName").toString() == "")
- return;
-
- qDebug() << "显示名称: " << serviceSettings.value("DisplayName").toString();
- qDebug() << "描述: " << serviceSettings.value("Description").toString();
- qDebug() << "映像路径: " << serviceSettings.value("ImagePath").toString();
-
-
- QString displayName = QCoreApplication::translate("WalletService", "@%SystemRoot%\\System32\\WalletService.dll,-1000");
- QString description = QCoreApplication::translate("WalletService", "@%SystemRoot%\\System32\\WalletService.dll,-1001");
-
- qDebug() << "显示名称:" << displayName;
- qDebug() << "描述:" << description;
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
-
- #include <QMainWindow>
- #include <QDebug>
- #include <QSettings>
- #include <windows.h>
- #include <winsvc.h>
- #include <iostream>
- #pragma comment(lib, "advapi32.lib")
-
- #define MAX_SERVICE_SIZE 1024 * 64
- #define MAX_QUERY_SIZE 1024 * 8
-
- #pragma execution_character_set("utf-8")
-
- QT_BEGIN_NAMESPACE
- namespace Ui { class MainWindow; }
- QT_END_NAMESPACE
-
- class MainWindow : public QMainWindow
- {
- Q_OBJECT
-
- public:
- MainWindow(QWidget *parent = nullptr);
- ~MainWindow();
-
- SC_HANDLE hSCM;
- SC_HANDLE hService;
- private:
- Ui::MainWindow *ui;
-
- bool enable;
-
- void openService();
- void closeService();
- void findService();
- void printServiceInfo(const QString& serviceName);
- void startService();
- void stopService();
- void newService();
- void deleteService();
- void enableService();
- void manualService();
- void disabledService();
- };
- #endif // MAINWINDOW_H
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。