服务操作(Win32, C++)

文章介绍了CServiceUtils类,一个用于Windows系统中安装、卸载、运行、停止服务以及检查服务状态的实用工具类,使用C++编写。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CServiceUtils.h

#pragma once

#include <string>
#include <windows.h>
#include <tchar.h>

#ifdef _UNICODE
using _tstring = std::wstring;
#else
using _tstring = std::string;
#endif

class CServiceUtils
{
public:

    CServiceUtils(CServiceUtils&) = delete;
    CServiceUtils& operator = (const CServiceUtils&) = delete;

    /**
     * @brief 安装服务
     * @details 安装服务
     *
     * @param strSrvName 服务名称
     * @param strDisplayName 服务显示名称
     * @param strExecFile 可执行文件路径
     * @return @c true 操作成功
     *         @c false 操作失败
    */
    static bool InstallService(
        const _tstring& strSrvName, 
        const _tstring& strDisplayName, 
        const _tstring& strExecFile
    );

    /**
     * @brief 卸载服务
     * @details 卸载服务
     *
     * @param strSrvName 服务名称
     * @return @c true 操作成功
     *         @c false 操作失败
    */
    static bool UninstallService(const _tstring& strSrvName);

    /**
     * @brief 运行服务
     * @details 运行服务
     *
     * @param strSrvName 服务名称
     * @return @c true 操作成功
     *         @c false 操作失败
    */
    static bool RunService(const _tstring& strSrvName);

    /**
     * @brief 停止服务
     * @details 停止服务
     *
     * @param strSrvName 服务名称
     * @return @c true 操作成功
     *         @c false 操作失败
    */
    static bool StopService(const _tstring& strSrvName);

    /**
     * @brief 检查服务是否存在
     * @details 运行服务
     *
     * @param strSrvName 服务名称
     * @return @c true 存在
     *         @c false 不存在
    */
    static bool IsServiceExist(const _tstring& strSrvName);

    /**
     * @brief 检查服务是否运行中
     * @details 运行服务
     *
     * @param strSrvName 服务名称
     * @return @c true 运行中
     *         @c false 未运行
    */
    static bool IsServiceRunning(const _tstring& strSrvName);
};

CServiceUtils.cpp

#include "CServiceUtils.h"
#include <winsvc.h>
#include <winioctl.h>

bool CServiceUtils::InstallService(
    const _tstring& strSrvName,
    const _tstring& strDisplayName,
    const _tstring& strExecFile
)
{
    SC_HANDLE hScm = NULL;
    SC_HANDLE hSvc = NULL;
    bool bResult = false;

    do
    {
        //打开hScm数据库并返回句柄
        hScm = ::OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
        if (NULL == hScm)
        {
            break;
        }

        //创建服务
        hSvc = ::CreateService(hScm, strSrvName.c_str(),
            strDisplayName.c_str(),
            SERVICE_ALL_ACCESS,
            SERVICE_KERNEL_DRIVER,
            SERVICE_DEMAND_START, SERVICE_ERROR_IGNORE,
            strExecFile.c_str(),
            NULL, NULL, NULL, NULL, NULL);

        //如果创建服务失败并且服务不存在, 则认为失败
        if (NULL == hSvc && (ERROR_SERVICE_EXISTS != ::GetLastError()))
        {
            break;
        }

        bResult = true;

    } while (false);

    if (NULL != hSvc)
    {
        ::CloseServiceHandle(hSvc); //关闭服务句柄
    }

    if (NULL != hScm)
    {
        ::CloseServiceHandle(hScm); //关闭hScm数据库句柄
    }

    return bResult;
}

bool CServiceUtils::UninstallService(const _tstring& strSrvName)
{
    SC_HANDLE hScm = NULL;
    SC_HANDLE hSvc = NULL;
    SERVICE_STATUS ServiceStatus = { 0 };
    bool bResult = false;

    do
    {
        //指定服务控制管理器建立连接并打开服务管理数据库
        hScm = ::OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
        if (NULL == hScm)
        {
            break;
        }

        hSvc = ::OpenService(hScm, strSrvName.c_str(), SERVICE_ALL_ACCESS | DELETE);

        //打开服务失败并且服务不存在, 则不需要删除
        if (NULL == hSvc)
        {
            if (ERROR_SERVICE_DOES_NOT_EXIST != ::GetLastError())
            {
                bResult = true;
            }
            break;
        }

        //查询服务运行状态
        if (!::QueryServiceStatus(hSvc, &ServiceStatus))
        {
            break;
        }

        //服务处于运行状态先通知服务停止
        if (SERVICE_RUNNING == ServiceStatus.dwCurrentState)
        {
            if (!::ControlService(hSvc, SERVICE_CONTROL_STOP, &ServiceStatus))
            {
                break;
            }
        }

        bResult = ::DeleteService(hSvc);

    } while (false);

    if (NULL != hSvc)
    {
        ::CloseServiceHandle(hSvc);
    }

    if (NULL != hScm)
    {
        ::CloseServiceHandle(hScm);
    }

    return bResult;
}

bool CServiceUtils::RunService(const _tstring& strSrvName)
{
    SC_HANDLE hScm = NULL;
    SC_HANDLE hSvc = NULL;
    SERVICE_STATUS ServiceStatus = { 0 };
    bool bResult = false;

    do
    {
        //指定服务控制管理器建立连接并打开服务管理数据库
        hScm = ::OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
        if (NULL == hScm)
        {
            break;
        }

        //打开服务
        hSvc = ::OpenService(hScm, strSrvName.c_str(), SERVICE_ALL_ACCESS | DELETE);
        if (NULL == hSvc)
        {
            break;
        }

        //查询服务运行状态
        if (!::QueryServiceStatus(hSvc, &ServiceStatus))
        {
            break;
        }

        //服务已经处于运行状态则不用管
        if (SERVICE_RUNNING == ServiceStatus.dwCurrentState)
        {
            bResult = true;
            break;
        }

        //启动服务
        bResult = ::StartService(hSvc, 0, NULL);

    } while (false);

    if (NULL != hSvc)
    {
        ::CloseServiceHandle(hSvc);
    }

    if (NULL != hScm)
    {
        ::CloseServiceHandle(hScm);
    }

    return bResult;
}

bool CServiceUtils::StopService(const _tstring& strSrvName)
{
    SC_HANDLE hScm = NULL;
    SC_HANDLE hSvc = NULL;
    SERVICE_STATUS ServiceStatus = { 0 };
    bool bResult = false;

    do
    {
        //指定服务控制管理器建立连接并打开服务管理数据库
        hScm = ::OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
        if (NULL == hScm)
        {
            break;
        }

        //打开服务失败并且服务不存在, 则不需要停止
        hSvc = ::OpenService(hScm, strSrvName.c_str(), SERVICE_ALL_ACCESS | DELETE);
        if (NULL == hSvc)
        {
            if (ERROR_SERVICE_DOES_NOT_EXIST != ::GetLastError())
            {
                bResult = true;
            }
            break;
        }

        //查询服务运行状态
        if (!::QueryServiceStatus(hSvc, &ServiceStatus))
        {
            break;
        }

        //服务处于运行状态先通知服务停止
        if (SERVICE_RUNNING == ServiceStatus.dwCurrentState)
        {
            if (!::ControlService(hSvc, SERVICE_CONTROL_STOP, &ServiceStatus))
            {
                break;
            }
        }

        bResult = true;

    } while (false);

    if (NULL != hSvc)
    {
        ::CloseServiceHandle(hSvc);
    }

    if (NULL != hScm)
    {
        ::CloseServiceHandle(hScm);
    }

    return bResult;
}

bool CServiceUtils::IsServiceExist(const _tstring& strSrvName)
{
    SC_HANDLE hScm = NULL;
    SC_HANDLE hSvc = NULL;
    SERVICE_STATUS ServiceStatus = { 0 };
    bool bResult = false;

    do
    {
        //指定服务控制管理器建立连接并打开服务管理数据库
        hScm = ::OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
        if (NULL == hScm)
        {
            break;
        }

        //打开服务
        hSvc = ::OpenService(hScm, strSrvName.c_str(), SERVICE_ALL_ACCESS | DELETE);
        if (NULL == hSvc)
        {
            break;
        }

        bResult = true;

    } while (false);

    if (NULL != hSvc)
    {
        ::CloseServiceHandle(hSvc);
    }

    if (NULL != hScm)
    {
        ::CloseServiceHandle(hScm);
    }

    return bResult;
}

bool CServiceUtils::IsServiceRunning(const _tstring& strSrvName)
{
    SC_HANDLE hScm = NULL;
    SC_HANDLE hSvc = NULL;
    SERVICE_STATUS ServiceStatus = { 0 };
    bool bResult = false;

    do
    {
        //指定服务控制管理器建立连接并打开服务管理数据库
        hScm = ::OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
        if (NULL == hScm)
        {
            break;
        }

        //打开服务
        hSvc = ::OpenService(hScm, strSrvName.c_str(), SERVICE_ALL_ACCESS | DELETE);
        if (NULL == hSvc)
        {
            break;
        }

        //查询服务运行状态
        if (!::QueryServiceStatus(hSvc, &ServiceStatus))
        {
            break;
        }

        //服务已经处于运行状态则不用管
        if (SERVICE_RUNNING != ServiceStatus.dwCurrentState)
        {
            break;
        }

        bResult = true;

    } while (false);

    if (NULL != hSvc)
    {
        ::CloseServiceHandle(hSvc);
    }

    if (NULL != hScm)
    {
        ::CloseServiceHandle(hScm);
    }

    return bResult;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值