系统服务便捷管理助手

#include <tchar.h>
#include <wtypes.h>
#include <winsvc.h>
#include <AFXCOM_.H>

#include <vector>
#include <string>

// 设置连接器选项,不弹出控制台窗口  
#pragma comment(linker, "/subsystem:windows /entry:mainCRTStartup")

// 枚举系统服务项状态信息
typedef struct _ENUM_SERVICE_INFO
{
	std::string strServiceName;
	std::string strDisplayName;
	SERVICE_STATUS ServiceStatus;
}ENUM_SERVICE_INFO, *LPENUM_SERVICE_INFO;


//////////////////////////////////////////////////////////////////////////
// 系统服务管理助手
class CSystemServiceHelper
{
public:
	CSystemServiceHelper();
	~CSystemServiceHelper();

public:
	// 通过服务名称取得服务的操作句柄
	SC_HANDLE GetServiceHandle(LPCTSTR lpszServiceName);

	// 启动服务
	BOOL StartService(SC_HANDLE hscService);

	// 停止服务
	// 注:必须先停止服务的依赖服务项才能工程停止服务
	BOOL StopService(SC_HANDLE hscService);

	// 重启服务
	BOOL RestartService(SC_HANDLE hscService);

	// 获取依赖的所有服务项列表
	BOOL GetDependentServices(SC_HANDLE hscService, std::vector<ENUM_SERVICE_INFO>& vecServiceStatus);

private:
	// 系统服务控制管理器句柄
	SC_HANDLE m_hscSCManager;

	// 服务句柄
	SC_HANDLE m_hscService;
};

CSystemServiceHelper::CSystemServiceHelper()
{
	// 取得服务控制管理器的操作句柄
	m_hscSCManager = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);

	// 初始化为空
	m_hscService = NULL;
}

CSystemServiceHelper::~CSystemServiceHelper()
{
	if ( m_hscSCManager)
	{
		::CloseServiceHandle(m_hscSCManager);
	}

	if ( m_hscService )
	{
		::CloseServiceHandle(m_hscService);
	}
	
}

SC_HANDLE CSystemServiceHelper::GetServiceHandle(LPCTSTR lpszServiceName)
{
	// 系统服务控制管理器句柄不能为空
	if ( !m_hscSCManager )
	{
		return NULL;
	}

	// 关闭上次打开的服务
	if ( m_hscService )
	{
		::CloseServiceHandle(m_hscService);
	}

	// 取得服务句柄并返回
	return ::OpenService(m_hscSCManager, lpszServiceName, SERVICE_ALL_ACCESS);
}

// 调用形式:oServiceHelper.StartService(oServiceHelper.GetServiceHandle(lpszServiceName))
BOOL CSystemServiceHelper::StartService(SC_HANDLE hscService)
{
	// 传入句柄为空,直接返回
	if ( !hscService)
	{
		return FALSE;
	}

	// 查询服务的状态
	SERVICE_STATUS ssStatus;
	memset(&ssStatus, 0, sizeof(SERVICE_STATUS));
	if ( !::QueryServiceStatus(hscService, &ssStatus) )
	{
		return FALSE;
	}

	// 开启服务
	if ( SERVICE_RUNNING != ssStatus.dwCurrentState )
	{
		if ( !::StartService(hscService, 0, NULL) )
		{
			return FALSE;
		}
	}

	return TRUE;
}

// 停止服务
BOOL CSystemServiceHelper::StopService(SC_HANDLE hscService)
{
	if ( !hscService )
	{
		return FALSE;
	}

	// 获取当前服务的状态信息
	SERVICE_STATUS ssStatus;
	memset(&ssStatus, 0, sizeof(SERVICE_STATUS));
	if ( !::QueryServiceStatus(hscService, &ssStatus) )
	{
		return FALSE;
	}

	// 如果服务已停止,则直接返回
	if ( SERVICE_STOPPED == ssStatus.dwCurrentState )
	{
		return TRUE;
	}

	// 如果服务正在被停止,则等待服务停止结束并返回
	while ( SERVICE_STOP_PENDING == ssStatus.dwCurrentState )
	{
		::Sleep(ssStatus.dwWaitHint);

		// 循环检测停止状态
		if ( !::QueryServiceStatus(hscService, &ssStatus) )
		{
			return FALSE;
		}

		if ( SERVICE_STOPPED == ssStatus.dwCurrentState )
		{
			// 主服务已停止,直接返回
			return TRUE;
		}
	}

	// 先停止所有依赖服务项
	std::vector<ENUM_SERVICE_INFO> vecServiceStatus;
	if ( !GetDependentServices(hscService, vecServiceStatus) )
	{
		return FALSE;
	}

	if ( !vecServiceStatus.empty() )
	{
		SERVICE_STATUS ssDepServiceStatus;
		memset(&ssDepServiceStatus, 0, sizeof(SERVICE_STATUS));
		
		std::vector<ENUM_SERVICE_INFO>::const_iterator iter = vecServiceStatus.begin();
		for (SC_HANDLE hDepService = NULL; vecServiceStatus.end() != iter; ++iter)
		{
			// 停止依赖服务项
			if ( !StopService(GetServiceHandle(iter->strServiceName.c_str())) )
			{
				return FALSE;
			}
		}
	}

	// 停止服务自身
	if ( !::ControlService(hscService, SERVICE_CONTROL_STOP, &ssStatus) )
	{
		return FALSE;
	}

	// 循环检测服务的停止状态
	while ( SERVICE_STOPPED != ssStatus.dwCurrentState )
	{
		::Sleep(ssStatus.dwWaitHint);
		
		// 循环检测停止状态
		if ( !::QueryServiceStatus(hscService, &ssStatus) )
		{
			return FALSE;
		}
		
		if ( SERVICE_STOPPED == ssStatus.dwCurrentState )
		{
			break;
		}
	}

	return TRUE;
}

// 重启系统服务程序
BOOL CSystemServiceHelper::RestartService(SC_HANDLE hscService)
{
	// 停止服务项
	if ( !StopService(hscService) )
	{
		return FALSE;
	}

	// 启动服务项
	if ( !StartService(hscService) )
	{
		return FALSE;
	}

	return TRUE;
}

BOOL CSystemServiceHelper::GetDependentServices(SC_HANDLE hscService, std::vector<ENUM_SERVICE_INFO>& vecServiceStatus)
{
	if ( !hscService )
	{
		return FALSE;
	}

	// 清空依赖项列表
	vecServiceStatus.clear();

	// 依赖的服务列表
	LPENUM_SERVICE_STATUS lpDependencies = NULL;
	DWORD dwBytesNeeded = 0;
	DWORD dwCount = 0;
	
	// 首先传入错误参数,获取依赖项的数组长度,如果返回为真,说明依赖项为空
	if ( ::EnumDependentServices(hscService, SERVICE_ACTIVE, NULL, 0, &dwBytesNeeded, &dwCount) )
	{
		// the Enum call succeed, there are no dependent services so do nothing
	}
	else
	{
		ASSERT(dwBytesNeeded > 0);

		// 开辟内存缓冲区,用来保存所有依赖项数据
		char* buffer = new char [dwBytesNeeded];
		memset(buffer, 0, dwBytesNeeded);
		lpDependencies = (LPENUM_SERVICE_STATUS)buffer;
		
		// 枚举所有依赖项
		if ( !EnumDependentServices(hscService, SERVICE_ACTIVE, lpDependencies, 
			dwBytesNeeded, &dwBytesNeeded, &dwCount ) )
		{
			delete[] buffer;
			return FALSE;
		}
		
		// 将所有依赖项存入对象vecServiceStatus中
		for (int i = 0; i < dwCount; ++i)
		{
			ENUM_SERVICE_INFO stServiceStatus;
			stServiceStatus.strServiceName = lpDependencies[i].lpServiceName;
			stServiceStatus.strDisplayName = lpDependencies[i].lpDisplayName;
			stServiceStatus.ServiceStatus = lpDependencies[i].ServiceStatus;

			vecServiceStatus.push_back(stServiceStatus);
		}

		// 释放缓冲区
		delete[] buffer;
	}

	return TRUE;
}


int main(int argc, char* argv[])
{
	// 定义系统服务帮助对象
	CSystemServiceHelper oServiceHelper;

	// 获取服务的依赖项
	//std::vector<ENUM_SERVICE_INFO> vecServiceStatus;
	//BOOL bResult = oServiceHelper.GetDependentServices(oServiceHelper.GetServiceHandle(_T("Themes")), vecServiceStatus);

	// 重启服务项
	BOOL bResult = oServiceHelper.RestartService(oServiceHelper.GetServiceHandle(_T("Themes")));

	return 0;
}
posted on 2011-05-15 23:37  潇湘雨歇  阅读(257)  评论(0编辑  收藏  举报