xentara-utils v2.1
The Xentara Utility Library
Loading...
Searching...
No Matches
xentara::utils::windows::service::ServiceDefinition< kServiceName, kServiceMain, kEventProvider, kFatalErrorEvent, kOptions > Class Template Referencefinal

A definition of a Windows Service. More...

#include <xentara/utils/windows/service/ServiceDefinition.hpp>

Related Symbols

(Note that these are not member symbols.)

using ServiceMainFunction = auto(*)(int, char *[]) -> int
 A callback suitable for use as a service main function.
 

Detailed Description

template<tparam::String kServiceName, ServiceMainFunction kServiceMain, const GUID & kEventProvider, const EVENT_DESCRIPTOR & kFatalErrorEvent, ServiceOptions kOptions = ServiceOptions {}>
class xentara::utils::windows::service::ServiceDefinition< kServiceName, kServiceMain, kEventProvider, kFatalErrorEvent, kOptions >

A definition of a Windows Service.

This class is used to configure the parameters for a Windows service. The service can then be using either run() or tryRunAsWindowsService().

Each service has a service main function. The service main function has the same signature as the global main function, and executes the application logic of the service.

The service main function is not a full Windows ServiceMain, as described under Writing a ServiceMain Function function in the Windows System Services documentation. It only executes the application logic of the service. Maintenance tasks like registering the service control hander and reporting the service status are done automatically by the framework.

The framework will call application::TerminateHandler::terminate() when the service needs to shut down. The service main must watch the state of the terminate handler and return with an appropriate exit code once the Terminate state had been triggered.

The Windows service manager will expect the service to terminate within a specific amount of time once the Terminate state has been triggered. By default, this timeout is 10 seconds, but it can be increased or decreased by specifying a kOptions template parameter with shutdownTimeout set to an appropriate value.

Template Parameters
kServiceNameThe name of the service
kServiceMainThe service main function. This function will be called after service initialization, passing all the service parameters to it. The return value of the function will be used as the exit code of the service.
kEventProviderThe GUID of the event provider to use for logging. This must be the UUID of a registered manifest-based ETW event provider.
kFatalErrorEvent

The event that should be used to log fatal errors running the service. This event must belong to the provider identified by kEventProvider, and it must have a single user data of type win:UnicodeString, that will be set to the error message. The event message should usually have the form "XYZ has terminated with an error: %1", where XYZ is the name of the service.

Note that the kServiceMain function must log its own errors. This event is only used for errors initializing and starting the service.

optionsOptions for the service
See also
run()
tryRunAsWindowsService()

Friends And Related Symbol Documentation

◆ ServiceMainFunction

template<tparam::String kServiceName, ServiceMainFunction kServiceMain, const GUID & kEventProvider, const EVENT_DESCRIPTOR & kFatalErrorEvent, ServiceOptions kOptions = ServiceOptions {}>
using ServiceMainFunction = auto (*)(int, char *[]) -> int
related

A callback suitable for use as a service main function.

This is simply a function pointer to a function with the same signature as the normal main() function.