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

相关推荐
心之语歌6 分钟前
LiteFlow Spring boot使用方式
java·开发语言
人才程序员31 分钟前
【C++拓展】vs2022使用SQlite3
c语言·开发语言·数据库·c++·qt·ui·sqlite
OKkankan1 小时前
实现二叉树_堆
c语言·数据结构·c++·算法
梁雨珈1 小时前
PL/SQL语言的图形用户界面
开发语言·后端·golang
励志的小陈1 小时前
C语言-----扫雷游戏
c语言·开发语言·游戏
martian6651 小时前
第19篇:python高级编程进阶:使用Flask进行Web开发
开发语言·python
纸飞机√※1 小时前
windows下部署安装 ELK,nginx,tomcat日志分析
windows·nginx·elk·tomcat
gis收藏家2 小时前
利用 SAM2 模型探测卫星图像中的农田边界
开发语言·python
Ciderw2 小时前
MySQL为什么使用B+树?B+树和B树的区别
c++·后端·b树·mysql·面试·golang·b+树
yerennuo2 小时前
windows第七章 MFC类CWinApp介绍
c++·windows·mfc