From 1feafd9d186a825bbd0247d88e24e3d25934d61f Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Mon, 27 Sep 2021 20:09:47 +0200 Subject: zenserver: added better detection of whether we are running as a service --- zenserver/windows/service.cpp | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) (limited to 'zenserver/windows/service.cpp') 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 +#include #include #include @@ -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; } -- cgit v1.2.3