From 4b26c859173428d9f6dc7b34ec5796b14c1ad03a Mon Sep 17 00:00:00 2001 From: Naim A Date: Sat, 20 Feb 2016 21:13:09 +0200 Subject: [PATCH] Start + Stop service --- src/main.cpp | 5 +++++ src/service.cpp | 14 +++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index cdebc42..2aa342e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -182,6 +182,11 @@ int main(int argc, char *argv[]) { svc.stop(); } + else + { + std::cerr << "No such service command." << std::endl; + return -1; + } } catch (const UDPT::OSError& ex) { diff --git a/src/service.cpp b/src/service.cpp index 9c146c2..d61aacf 100644 --- a/src/service.cpp +++ b/src/service.cpp @@ -77,12 +77,24 @@ namespace UDPT void Service::start() { - + std::shared_ptr hSvc = getService(SERVICE_START); + BOOL bRes = ::StartService(reinterpret_cast(hSvc.get()), 0, NULL); + if (FALSE == bRes) + { + throw OSError(); + } } void Service::stop() { + SERVICE_STATUS status = { 0 }; + std::shared_ptr hSvc = getService(SERVICE_STOP); + BOOL bRes = ::ControlService(reinterpret_cast(hSvc.get()), SERVICE_CONTROL_STOP, &status); + if (FALSE == bRes) + { + throw OSError(); + } } void Service::setup()