如何在MinGW GCC中查询Windows服务的状态?启动、停止 Windows 服务 Demo

使用 MinGW GCC 完成查询 Windows 服务的状态、停止服务和启动服务:

#pragma once
#include <windows.h>
#include <string>
#include <utility>

class ServiceManage {
public:
    explicit ServiceManage(std::string name) : serviceName(std::move(name)) {}

    void QueryServiceStatus() {
        SC_HANDLE scm, service;
        SERVICE_STATUS status;

        scm = OpenSCManager(nullptr, nullptr, SC_MANAGER_CONNECT);
        if (scm == nullptr)
        {
            printf("OpenSCManager failed (%lu)\n", GetLastError());
            return;
        }

        service = OpenService(scm, serviceName.c_str(), SERVICE_QUERY_STATUS);
        if (service == nullptr)
        {
            printf("OpenService failed (%lu)\n", GetLastError());
            CloseServiceHandle(scm);
            return;
        }

        if (!::QueryServiceStatus(service, &status))
        {
            printf("QueryServiceStatus failed (%lu)\n", GetLastError());
        }
        else
        {
            printf("Service %s status:\n", serviceName.c_str());
            printf("  State: ");
            switch ( status.dwCurrentState )
            {
                case SERVICE_STOPPED:
                    printf("Stopped\n");
                    serviceStatus = "Stopped";
                    break;
                case SERVICE_START_PENDING:
                    printf("Start Pending\n");
                    serviceStatus = "Start Pending";
                    break;
                case SERVICE_STOP_PENDING:
                    printf("Stop Pending\n");
                    serviceStatus = "Stop Pending";
                    break;
                case SERVICE_RUNNING:
                    printf("Running\n");
                    serviceStatus = "Running";
                    break;
            }
        }

        CloseServiceHandle(service);
        CloseServiceHandle(scm);
    }

    void StartService() {
        SC_HANDLE scm, service;

        scm = OpenSCManager(nullptr, nullptr, SC_MANAGER_CONNECT);
        if (scm == nullptr)
        {
            printf("OpenSCManager failed (%lu)\n", GetLastError());
            return;
        }

        service = OpenService(scm, serviceName.c_str(), SERVICE_START);
        if (service == nullptr)
        {
            printf("OpenService failed (%lu)\n", GetLastError());
            CloseServiceHandle(scm);
            return;
        }

        if (!::StartService(service, 0, nullptr))
        {
            printf("StartService failed (%lu)\n", GetLastError());
        }
        else
        {
            printf("Service %s started\n", serviceName.c_str());
        }

        CloseServiceHandle(service);
        CloseServiceHandle(scm);
    }

    void StopService() {
        SC_HANDLE scm, service;
        SERVICE_STATUS status;

        scm = OpenSCManager(nullptr, nullptr, SC_MANAGER_CONNECT);
        if (scm == nullptr)
        {
            printf("OpenSCManager failed, error=%d\n", GetLastError());
            return;
        }

        service = OpenService(scm, serviceName.c_str(), SERVICE_STOP | SERVICE_QUERY_STATUS);
        if (service == nullptr)
        {
            printf("OpenService failed, error=%lu\n", GetLastError());
            CloseServiceHandle(scm);
            return;
        }

        if (!::QueryServiceStatus(service, &status))
        {
            printf("QueryServiceStatus failed, error=%lu\n", GetLastError());
        }
        else
        {
            if (status.dwCurrentState == SERVICE_RUNNING)
            {
                if (!ControlService(service, SERVICE_CONTROL_STOP, &status))
                {
                    printf("ControlService failed, error=%lu\n", GetLastError());
                }
                else
                {
                    printf("Service %s stopped successfully\n", serviceName.c_str());
                }
            }
            else
            {
                printf("Service %s is not running\n", serviceName.c_str());
            }
        }

        CloseServiceHandle(service);
        CloseServiceHandle(scm);
    }

    bool status() {
        return serviceStatus == "Running";
    }

private:
    std::string serviceName;
    std::string serviceStatus;
};
posted @ 2024-03-05 15:52  RioTian  阅读(18)  评论(0编辑  收藏  举报