一个类,用于全方位控制系统服务。
hServer.h
#ifndef __H_SERVER_H__ #define __H_SERVER_H__ #pragma once #include <Windows.h> class hServer { public: hServer (LPCTSTR servName); ~hServer (); BOOL IsAutoRun (); BOOL IsDemandRun (); BOOL IsDisabled (); BOOL SetAutoRun (); BOOL SetDemandRun (); BOOL SetDisabled (); BOOL IsRunning (); BOOL Start (); BOOL Stop (); private: BOOL _ControlServices (BOOL (*callback)(SC_HANDLE)); BOOL _ControlServices (BOOL (*callback)(SC_HANDLE, SC_HANDLE)); LPTSTR h_servName; }; #endif //__H_SERVER_H__
#include "hServer.h" //#define _NO_COPY_ hServer::hServer (LPCTSTR servName) { #ifdef _NO_COPY_ h_servName = const_cast<LPTSTR>(servName); #else //_NO_COPY_ h_servName = new TCHAR [lstrlen (servName) + 1]; lstrcpy (h_servName, servName); #endif //_NO_COPY_ } hServer::~hServer () { #ifndef _NO_COPY_ delete h_servName; #endif //_NO_COPY_ } BOOL hServer::IsAutoRun () { return _ControlServices ([] (SC_HANDLE service) { QUERY_SERVICE_CONFIG *qsc = (LPQUERY_SERVICE_CONFIG)new BYTE [8 * 1024];//查询服务配置 DWORD d; BOOL bRet = FALSE; if (QueryServiceConfig (service, qsc, 8 * 1024, &d)) if (qsc->dwStartType <= 2)/*SERVICE_BOOT_START 0 SERVICE_SYSTEM_START 1 SERVICE_AUTO_START 2*/ bRet = TRUE; delete qsc; return bRet; }); } BOOL hServer::IsDemandRun () { return _ControlServices ([] (SC_HANDLE service) { QUERY_SERVICE_CONFIG *qsc = (LPQUERY_SERVICE_CONFIG)new BYTE [8 * 1024];//查询服务配置 DWORD d; BOOL bRet = FALSE; if (QueryServiceConfig (service, qsc, 8 * 1024, &d)) if (qsc->dwStartType == SERVICE_DEMAND_START) bRet = TRUE; delete qsc; return bRet; }); } BOOL hServer::IsDisabled () { return _ControlServices ([] (SC_HANDLE service) { QUERY_SERVICE_CONFIG *qsc = (LPQUERY_SERVICE_CONFIG)new BYTE [8 * 1024];//查询服务配置 DWORD d; BOOL bRet = FALSE; if (QueryServiceConfig (service, qsc, 8 * 1024, &d)) if (qsc->dwStartType == SERVICE_DISABLED) bRet = TRUE; delete qsc; return bRet; }); } BOOL hServer::SetAutoRun () { return _ControlServices ([] (SC_HANDLE scm, SC_HANDLE service) { SC_LOCK sclLock = LockServiceDatabase (scm); BOOL bRet = ChangeServiceConfig (service, SERVICE_NO_CHANGE, SERVICE_AUTO_START, SERVICE_NO_CHANGE, NULL, NULL, NULL, NULL, NULL, NULL, NULL); if (sclLock) UnlockServiceDatabase (sclLock); return bRet; }); } BOOL hServer::SetDemandRun () { return _ControlServices ([] (SC_HANDLE scm, SC_HANDLE service) { SC_LOCK sclLock = LockServiceDatabase (scm); BOOL bRet = ChangeServiceConfig (service, SERVICE_NO_CHANGE, SERVICE_DEMAND_START, SERVICE_NO_CHANGE, NULL, NULL, NULL, NULL, NULL, NULL, NULL); if (sclLock) UnlockServiceDatabase (sclLock); return bRet; }); } BOOL hServer::SetDisabled () { return _ControlServices ([] (SC_HANDLE scm, SC_HANDLE service) { SC_LOCK sclLock = LockServiceDatabase (scm); BOOL bRet = ChangeServiceConfig (service, SERVICE_NO_CHANGE, SERVICE_DISABLED, SERVICE_NO_CHANGE, NULL, NULL, NULL, NULL, NULL, NULL, NULL); if (sclLock) UnlockServiceDatabase (sclLock); return bRet; }); } BOOL hServer::IsRunning () { return _ControlServices ([] (SC_HANDLE service) { SERVICE_STATUS status;//查询服务状态 if (QueryServiceStatus (service, &status)) return (BOOL) (status.dwCurrentState == SERVICE_RUNNING || status.dwCurrentState == SERVICE_START_PENDING); return FALSE; }); } BOOL hServer::Start () { return _ControlServices ([] (SC_HANDLE service) { SERVICE_STATUS status;//查询服务状态 if (QueryServiceStatus (service, &status)) { if (status.dwCurrentState == SERVICE_RUNNING) return TRUE; } return StartService (service, NULL, NULL); }); } BOOL hServer::Stop () { return _ControlServices ([] (SC_HANDLE service) { SERVICE_STATUS status;//查询服务状态 if (QueryServiceStatus (service, &status)) { if (status.dwCurrentState == SERVICE_STOPPED) return TRUE; } return ControlService (service, SERVICE_CONTROL_STOP, &status); }); } BOOL hServer::_ControlServices (BOOL (*callback)(SC_HANDLE)) { SC_HANDLE scm = OpenSCManager (NULL, NULL, SC_MANAGER_ALL_ACCESS); if (!scm) return FALSE; SC_HANDLE service = OpenService (scm, h_servName, SERVICE_ALL_ACCESS); if (!service) { CloseServiceHandle (scm); return FALSE; } BOOL bRet = callback (service); CloseServiceHandle (service); CloseServiceHandle (scm); return bRet; } BOOL hServer::_ControlServices (BOOL (*callback)(SC_HANDLE, SC_HANDLE)) { SC_HANDLE scm = OpenSCManager (NULL, NULL, SC_MANAGER_ALL_ACCESS); if (!scm) return FALSE; SC_HANDLE service = OpenService (scm, h_servName, SERVICE_ALL_ACCESS); if (!service) { CloseServiceHandle (scm); return FALSE; } BOOL bRet = callback (scm, service); CloseServiceHandle (service); CloseServiceHandle (scm); return bRet; }
另外,hServer.cpp文件里面有一个宏,_NO_COPY_,如果定义了这个宏,类将不会单独分配一串内存用于存储服务名称。如果服务名称在堆中为常量,完全可以将这行代码取消注释。由于不涉及服务名称更改(需要控制多个服务请自行实例化多个对象),所以直接使用最简洁的C STRING。
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/fawdlstty/article/details/47267637