92 lines
2.4 KiB
C++
92 lines
2.4 KiB
C++
#include <service/ServiceManager.hpp>
|
|
#include <common/regkey.hpp>
|
|
|
|
ServiceManager::ServiceManager()
|
|
: mhServiceControl(0)
|
|
{
|
|
open();
|
|
}
|
|
|
|
ServiceManager::~ServiceManager()
|
|
{
|
|
close();
|
|
}
|
|
|
|
bool ServiceManager::open(void)
|
|
{
|
|
close();
|
|
mhServiceControl=::OpenSCManager(0,0,SC_MANAGER_CONNECT|SC_MANAGER_CREATE_SERVICE|SC_MANAGER_ENUMERATE_SERVICE|GENERIC_READ|GENERIC_WRITE);
|
|
return isOkay();
|
|
}
|
|
|
|
void ServiceManager::close(void)
|
|
{
|
|
if(!isOkay())return;
|
|
::CloseServiceHandle(mhServiceControl);
|
|
mhServiceControl=0;
|
|
}
|
|
|
|
bool ServiceManager::createService(const String &strServiceName,const String &strDisplayName,const String &strBinPath,const String &strAccountName,const String &strPassword)
|
|
{
|
|
SC_HANDLE hService;
|
|
|
|
if(!isOkay())return false;
|
|
hService=::CreateService(mhServiceControl,strServiceName,strDisplayName,
|
|
SERVICE_ALL_ACCESS|GENERIC_READ|GENERIC_WRITE,
|
|
SERVICE_WIN32_OWN_PROCESS,SERVICE_AUTO_START,SERVICE_ERROR_NORMAL,strBinPath,
|
|
0,0,0,0,0);
|
|
if(!hService)return false;
|
|
::CloseServiceHandle(hService);
|
|
createEventLogEntry(strServiceName,strBinPath);
|
|
return true;
|
|
}
|
|
|
|
bool ServiceManager::deleteService(const String &strServiceName)
|
|
{
|
|
SC_HANDLE hService;
|
|
bool returnCode;
|
|
|
|
if(!isOkay())return false;
|
|
hService=::OpenService(mhServiceControl,strServiceName,SERVICE_ALL_ACCESS);
|
|
if(!hService)return false;
|
|
returnCode=::DeleteService(hService)?true:false;
|
|
::CloseServiceHandle(hService);
|
|
removeEventLogEntry(strServiceName);
|
|
return returnCode;
|
|
}
|
|
|
|
void ServiceManager::createEventLogEntry(const String &strServiceName,const String &strBinPath)
|
|
{
|
|
RegKey regKey(RegKey::LocalMachine);
|
|
String strTopLevelKey("SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application");
|
|
String strKeyName;
|
|
|
|
if(strServiceName.isNull())return;
|
|
strKeyName=strTopLevelKey+String("\\")+strServiceName;
|
|
if(!regKey.openKey(strKeyName))
|
|
{
|
|
if(!regKey.createKey(strKeyName,""))return;
|
|
if(!regKey.openKey(strKeyName))return;
|
|
}
|
|
regKey.setValue("EventMessageFile",strBinPath);
|
|
regKey.setValue("TypesSupported",7);
|
|
regKey.closeKey();
|
|
}
|
|
|
|
void ServiceManager::removeEventLogEntry(const String &strServiceName)
|
|
{
|
|
RegKey regKey(RegKey::LocalMachine);
|
|
String strTopLevelKey("SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application");
|
|
|
|
if(strServiceName.isNull())return;
|
|
if(!regKey.openKey(strTopLevelKey))return;
|
|
regKey.deleteKey(strServiceName);
|
|
}
|
|
|
|
bool ServiceManager::isOkay(void)const
|
|
{
|
|
return mhServiceControl?true:false;
|
|
}
|
|
|
|
|