Skip to content

Commit

Permalink
ServiceManager: Add Start and Stop methods
Browse files Browse the repository at this point in the history
Signed-off-by: Sameeh Jubran <[email protected]>
Signed-off-by: Dmitry Fleytman <[email protected]>
  • Loading branch information
Sameeh Jubran authored and Dmitry Fleytman committed May 29, 2017
1 parent 2214163 commit e7b4fd2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
44 changes: 31 additions & 13 deletions UsbDkHelper/ServiceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,31 @@ void ServiceManager::CreateServiceObject(const tstring &ServiceName, const tstri

void ServiceManager::DeleteServiceObject(const tstring &ServiceName)
{
assert(m_schSCManager);

SCMHandleHolder schService(OpenService(m_schSCManager, ServiceName.c_str(), SERVICE_ALL_ACCESS));
if (!schService)
{
auto err = GetLastError();
if (err != ERROR_SERVICE_DOES_NOT_EXIST)
{
throw UsbDkServiceManagerFailedException(TEXT("OpenService failed with error "), err);
}
return;
}

SCMHandleHolder schService(OpenServiceObject(ServiceName));
WaitForServiceStop(schService);
if (!DeleteService(schService))
{
throw UsbDkServiceManagerFailedException(TEXT("DeleteService failed"));
}
}

void ServiceManager::StartServiceObject(const tstring &ServiceName)
{
SCMHandleHolder schService(OpenServiceObject(ServiceName));

if (!StartService(SC_HANDLE(schService), 0, NULL))
{
throw UsbDkServiceManagerFailedException(TEXT("StartService failed"));
}
return;
}

void ServiceManager::StopServiceObject(const tstring & ServiceName)
{
SCMHandleHolder schService(OpenServiceObject(ServiceName));
WaitForServiceStop(schService);
}

void ServiceManager::WaitForServiceStop(const SCMHandleHolder &schService)
{
static const DWORD SERVICE_STOP_WAIT_QUANTUM = 50;
Expand All @@ -86,3 +91,16 @@ void ServiceManager::WaitForServiceStop(const SCMHandleHolder &schService)
}
} while ((ssp.dwCurrentState != SERVICE_STOPPED) && (iterationNumber++ < SERVICE_STOP_ITERATIONS));
}

SC_HANDLE ServiceManager::OpenServiceObject(const tstring & ServiceName)
{
assert(m_schSCManager);

SC_HANDLE sch = OpenService(m_schSCManager, ServiceName.c_str(), SERVICE_ALL_ACCESS);
if (!sch)
{
auto err = GetLastError();
throw UsbDkServiceManagerFailedException(TEXT("OpenService failed"), err);
}
return sch;
}
3 changes: 3 additions & 0 deletions UsbDkHelper/ServiceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,14 @@ class ServiceManager

void CreateServiceObject(const tstring &ServiceName, const tstring &ServicePath);
void DeleteServiceObject(const tstring &ServiceName);
void StartServiceObject(const tstring &ServiceName);
void StopServiceObject(const tstring &ServiceName);

private:
typedef UsbDkHandleHolder<SC_HANDLE> SCMHandleHolder;

static void WaitForServiceStop(const SCMHandleHolder &schService);
SC_HANDLE OpenServiceObject(const tstring & ServiceName);

SCMHandleHolder m_schSCManager;
};

0 comments on commit e7b4fd2

Please sign in to comment.