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

相关推荐
Bona Sun3 分钟前
单片机手搓掌上游戏机(十四)—pico运行fc模拟器之电路连接
c语言·c++·单片机·游戏机
oioihoii12 分钟前
性能提升11.4%!C++ Vector的reserve()方法让我大吃一惊
开发语言·c++
毕设源码-朱学姐13 分钟前
【开题答辩全过程】以 基于JAVA的恒星酒店客房管理系统为例,包含答辩的问题和答案
java·开发语言
思密吗喽17 分钟前
景区行李寄存管理系统
java·开发语言·spring boot·毕业设计·课程设计
Rust语言中文社区19 分钟前
【Rust日报】Dioxus 用起来有趣吗?
开发语言·后端·rust
小灰灰搞电子23 分钟前
Rust Slint实现颜色选择器源码分享
开发语言·后端·rust
小狗爱吃黄桃罐头1 小时前
《C++ Primer Plus》模板类 Template 课本实验
c++
无限进步_1 小时前
C语言数组元素删除算法详解:从基础实现到性能优化
c语言·开发语言·windows·git·算法·github·visual studio
月殇_木言1 小时前
Python期末复习
开发语言·python
松涛和鸣1 小时前
16、C 语言高级指针与结构体
linux·c语言·开发语言·数据结构·git·算法