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

具体的自己看看代码 粘贴复制即可使用 卸载也很简单自己查看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__
posted @ 2019-10-24 13:43  轻风々  阅读(1512)  评论(0编辑  收藏  举报