diff options
| author | Stefan Boberg <[email protected]> | 2021-09-27 20:09:47 +0200 |
|---|---|---|
| committer | Stefan Boberg <[email protected]> | 2021-09-27 20:09:47 +0200 |
| commit | 1feafd9d186a825bbd0247d88e24e3d25934d61f (patch) | |
| tree | 8b1944893d37e5d7fd25e6f074646102b6b14025 /zenserver/windows/service.cpp | |
| parent | zencore: Added ability to forcefully set the IsInteractiveSession state (diff) | |
| download | zen-1feafd9d186a825bbd0247d88e24e3d25934d61f.tar.xz zen-1feafd9d186a825bbd0247d88e24e3d25934d61f.zip | |
zenserver: added better detection of whether we are running as a service
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; } |