windows 服务的安装、启动、状态查询 c++实现

#ifndef SERVICEMANAGE_H

#define SERVICEMANAGE_H

#include <windows.h>

#include <string>

class ServerManage

{

public:

ServerManage() {}

~ServerManage() {}

/*

@ 根据服务名启动服务

*/

inline bool Start_Server(const std::string& strServiceName)

{

bool bResult = false;

if (strServiceName.empty())

{

return bResult;

}

SC_HANDLE sc_Manager = ::OpenSCManagerA(NULL, NULL, GENERIC_EXECUTE);

if (sc_Manager)

{

SC_HANDLE sc_service = ::OpenServiceA(sc_Manager, strServiceName.c_str(), SERVICE_ALL_ACCESS);

if (sc_service)

{

SERVICE_STATUS_PROCESS service_status;

ZeroMemory(&service_status, sizeof(SERVICE_STATUS_PROCESS));

DWORD dwpcbBytesNeeded = sizeof(SERVICE_STATUS_PROCESS);

if (::QueryServiceStatusEx(sc_service, SC_STATUS_PROCESS_INFO,

(LPBYTE)&service_status,

dwpcbBytesNeeded,

&dwpcbBytesNeeded))

{

if (service_status.dwCurrentState == SERVICE_STOPPED)

{

if (!::StartService(sc_service, NULL, NULL))

{

::CloseServiceHandle(sc_service);

::CloseServiceHandle(sc_Manager);

return bResult;

}

while (::QueryServiceStatusEx(sc_service, SC_STATUS_PROCESS_INFO,

(LPBYTE)&service_status,

dwpcbBytesNeeded,

&dwpcbBytesNeeded))

{

Sleep(service_status.dwWaitHint);

if (service_status.dwCurrentState == SERVICE_RUNNING)

{

bResult = true;

break;

}

}

}

}

::CloseServiceHandle(sc_service);

}

::CloseServiceHandle(sc_Manager);

}

return bResult;

}

/*

@根据服务名停止服务

*/

inline bool Stop_Server(const std::string& strServiceName)

{

bool bResult = false;

if (strServiceName.empty())

{

return bResult;

}

SC_HANDLE sc_Manager = ::OpenSCManagerA(NULL, NULL, GENERIC_EXECUTE);

if (sc_Manager)

{

SC_HANDLE sc_service = ::OpenServiceA(sc_Manager, strServiceName.c_str(), SERVICE_ALL_ACCESS);

if (sc_service)

{

SERVICE_STATUS_PROCESS service_status;

ZeroMemory(&service_status, sizeof(SERVICE_STATUS_PROCESS));

DWORD dwpcbBytesNeeded = sizeof(SERVICE_STATUS_PROCESS);

if (::QueryServiceStatusEx(sc_service, SC_STATUS_PROCESS_INFO,

(LPBYTE)&service_status,

dwpcbBytesNeeded,

&dwpcbBytesNeeded))

{

SERVICE_CONTROL_STATUS_REASON_PARAMSA service_control_status;

DWORD dwerror = NULL;

ZeroMemory(&service_control_status, sizeof(SERVICE_CONTROL_STATUS_REASON_PARAMSA));

if (service_status.dwCurrentState == SERVICE_RUNNING)

{

service_control_status.dwReason = SERVICE_STOP_REASON_FLAG_PLANNED | SERVICE_STOP_REASON_MAJOR_APPLICATION | SERVICE_STOP_REASON_MINOR_NONE;;

if (!::ControlServiceExA(sc_service, SERVICE_CONTROL_STOP, SERVICE_CONTROL_STATUS_REASON_INFO, &service_control_status))

{

dwerror = ::GetLastError();

::CloseServiceHandle(sc_service);

::CloseServiceHandle(sc_Manager);

return bResult;

}

while (::QueryServiceStatusEx(sc_service, SC_STATUS_PROCESS_INFO,

(LPBYTE)&service_status,

dwpcbBytesNeeded,

&dwpcbBytesNeeded))

{

Sleep(service_status.dwWaitHint);

if (service_status.dwCurrentState == SERVICE_STOPPED)

{

bResult = true;

break;

}

}

}

}

::CloseServiceHandle(sc_service);

}

::CloseServiceHandle(sc_Manager);

}

return bResult;

}

/*

@根据服务名查询服务状态

*/

inline DWORD Query_Server_Status(const std::string& strServiceName)

{

DWORD nResult = 0;

if (strServiceName.empty())

{

return nResult;

}

SC_HANDLE sc_Manager = ::OpenSCManagerA(NULL, NULL, GENERIC_EXECUTE);

if (sc_Manager)

{

SC_HANDLE sc_service = ::OpenServiceA(sc_Manager, strServiceName.c_str(), SERVICE_ALL_ACCESS);

if (sc_service)

{

SERVICE_STATUS_PROCESS service_status;

ZeroMemory(&service_status, sizeof(SERVICE_STATUS_PROCESS));

DWORD dwpcbBytesNeeded = sizeof(SERVICE_STATUS_PROCESS);

if (::QueryServiceStatusEx(sc_service, SC_STATUS_PROCESS_INFO,

(LPBYTE)&service_status,

dwpcbBytesNeeded,

&dwpcbBytesNeeded))

{

nResult = service_status.dwCurrentState;

}

::CloseServiceHandle(sc_service);

}

::CloseServiceHandle(sc_Manager);

}

return nResult;

}

/*

@ 根据服务名 安装服务

*/

inline bool SerivceInstall(const std::string& strServiceName) {

bool bResult = false;

SC_HANDLE schSCManger;

SC_HANDLE schService;

CHAR szPath[MAX_PATH];

ZeroMemory(&szPath, MAX_PATH);

if (!::GetModuleFileNameA(NULL, szPath, MAX_PATH)) {

//LOG_INFO << "不能安装服务 (" << ::GetLastError() << ")\r\n";

return;

}

//获取SCM数据库的句柄。

schSCManger = ::OpenSCManagerA(NULL, NULL, SC_MANAGER_ALL_ACCESS);

if (!schSCManger) {

//LOG_INFO << "打开服务管理器失败 (" << ::GetLastError() << ")\r\n";

return;

}

//创建服务

schService = ::CreateServiceA(schSCManger, strServiceName.c_str(),

strServiceName.c_str(),

SERVICE_ALL_ACCESS,

SERVICE_WIN32_OWN_PROCESS,

SERVICE_AUTO_START,

SERVICE_ERROR_NORMAL,

szPath,

NULL,

NULL,

NULL,

NULL,

NULL);

if (!schService)

{

//LOG_INFO << "创建服务失败 (" << ::GetLastError() << ")\r\n";

::CloseServiceHandle(schSCManger);

return bResult;

}

else

{

//LOG_INFO << "创建服务成功\r\n";

//::StartService(schService, NULL, NULL);

bResult = true;

}

::CloseServiceHandle(schService);

::CloseServiceHandle(schSCManger);

return bResult;

}

private:

};

#endif //SERVICEMANAGE_H

相关推荐
我不会编程55517 小时前
Python Cookbook-5.1 对字典排序
开发语言·数据结构·python
李少兄17 小时前
Unirest:优雅的Java HTTP客户端库
java·开发语言·http
懒羊羊大王&17 小时前
模版进阶(沉淀中)
c++
无名之逆17 小时前
Rust 开发提效神器:lombok-macros 宏库
服务器·开发语言·前端·数据库·后端·python·rust
似水এ᭄往昔17 小时前
【C语言】文件操作
c语言·开发语言
啊喜拔牙17 小时前
1. hadoop 集群的常用命令
java·大数据·开发语言·python·scala
owde18 小时前
顺序容器 -list双向链表
数据结构·c++·链表·list
xixixin_18 小时前
为什么 js 对象中引用本地图片需要写 require 或 import
开发语言·前端·javascript
GalaxyPokemon18 小时前
Muduo网络库实现 [九] - EventLoopThread模块
linux·服务器·c++
W_chuanqi18 小时前
安装 Microsoft Visual C++ Build Tools
开发语言·c++·microsoft