diff options
Diffstat (limited to 'zenserver/windows/service.cpp')
| -rw-r--r-- | zenserver/windows/service.cpp | 33 |
1 files changed, 21 insertions, 12 deletions
diff --git a/zenserver/windows/service.cpp b/zenserver/windows/service.cpp index 017b5f9a7..b7b3b9bc1 100644 --- a/zenserver/windows/service.cpp +++ b/zenserver/windows/service.cpp @@ -3,6 +3,7 @@ #include "service.h" #include <zencore/zencore.h> +#include <zencore/except.h> #include <stdio.h> #include <tchar.h> @@ -146,26 +147,34 @@ CallMain(DWORD, LPSTR*) int WindowsService::ServiceMain() { - if (zen::IsInteractiveSession()) - { - // Not actually running as a service - return Run(); - } - else + gSvc = this; + + SERVICE_TABLE_ENTRY DispatchTable[] = {{(LPWSTR)SVCNAME, (LPSERVICE_MAIN_FUNCTION)&CallMain}, {NULL, NULL}}; + + // This call returns when the service has stopped. + // The process should simply terminate when the call returns. + + if (!StartServiceCtrlDispatcher(DispatchTable)) { - gSvc = this; + const DWORD dwError = zen::GetLastError(); - SERVICE_TABLE_ENTRY DispatchTable[] = {{(LPWSTR)SVCNAME, (LPSERVICE_MAIN_FUNCTION)&CallMain}, {NULL, NULL}}; + if (dwError == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) + { + // Not actually running as a service + gSvc = nullptr; - // This call returns when the service has stopped. - // The process should simply terminate when the call returns. + zen::SetIsInteractiveSession(true); - if (!StartServiceCtrlDispatcher(DispatchTable)) + return Run(); + } + else { - SvcReportEvent((LPTSTR)L"StartServiceCtrlDispatcher"); + zen::ThrowSystemError(dwError, "StartServiceCtrlDispatcher failed"); } } + zen::SetIsInteractiveSession(false); + return 0; } |