具体的自己看看代码 粘贴复制即可使用 卸载也很简单自己查看MSDN 加上就是#ifndef __SERVICEMANAGE_H__#define __SERVICEMANAGE_H__#include windows.h#include stringclass ServerManage{public:ServerMa...
具体的自己看看代码 粘贴复制即可使用 卸载也很简单自己查看MSDN 加上就是
#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__
沃梦达教程
本文标题为:windows 服务的安装、启动、状态查询、停止操作c++实现
基础教程推荐
猜你喜欢
- 如何C++使用模板特化功能 2023-03-05
- C++中的atoi 函数简介 2023-01-05
- C语言基础全局变量与局部变量教程详解 2022-12-31
- 详解c# Emit技术 2023-03-25
- C++详细实现完整图书管理功能 2023-04-04
- C利用语言实现数据结构之队列 2022-11-22
- C++使用easyX库实现三星环绕效果流程详解 2023-06-26
- 一文带你了解C++中的字符替换方法 2023-07-20
- C/C++编程中const的使用详解 2023-03-26
- C语言 structural body结构体详解用法 2022-12-06