|  | Home

# QtServiceBase Class Reference

The QtServiceBase class provides an API for implementing Windows services and Unix daemons. More...

     #include <QtServiceBase>

Inherited by QtService.

- List of all members, including inherited members

## Public Types

enum | MessageType { Success, Error, Warning, Information }
enum | ServiceFlag { Default, CanBeSuspended, CannotBeStopped, NeedsStopOnShutdown }
flags | ServiceFlags

## Public Functions

| QtServiceBase ( int argc, char ** argv, const QString & name )
virtual | ~QtServiceBase ()
int | exec ()
void | logMessage ( const QString & message, MessageType type = Success, int id = 0, uint category = 0, const QByteArray & data = QByteArray() )
QString | serviceDescription () const
ServiceFlags | serviceFlags () const
QString | serviceName () const
void | setServiceDescription ( const QString & description )
void | setServiceFlags ( ServiceFlags flags )
void | setStartupType ( QtServiceController::StartupType type )
QtServiceController::StartupType | startupType () const

## Static Public Members

QtServiceBase * | instance ()

## Protected Functions

virtual void | createApplication ( int & argc, char ** argv ) = 0
virtual int | executeApplication () = 0
virtual void | pause ()
virtual void | processCommand ( int code )
virtual void | resume ()
virtual void | start () = 0
virtual void | stop ()

## Detailed Description

The QtServiceBase class provides an API for implementing Windows services and Unix daemons.

A Windows service or Unix daemon (a "service"), is a program that runs "in the background" independently of whether a user is logged in or not. A service is often set up to start when the machine boots up, and will typically run continuously as long as the machine is on.

Services are usually non-interactive console applications. User interaction, if required, is usually implemented in a separate, normal GUI application that communicates with the service through an IPC channel. For simple communication, QtServiceController::sendCommand() and QtService::processCommand() may be used, possibly in combination with a shared settings file. For more complex, interactive communication, a custom IPC channel should be used, e.g. based on Qt's networking classes. (In certain circumstances, a service may provide a GUI itself, ref. the "interactive" example documentation).

Typically, you will create a service by subclassing the QtService template class which inherits QtServiceBase and allows you to create a service for a particular application type.

The Windows implementation uses the NT Service Control Manager, and the application can be controlled through the system administration tools. Services are usually launched using the system account, which requires that all DLLs that the service executable depends on (i.e. Qt), are located in the same directory as the service, or in a system path.

On Unix a service is implemented as a daemon.

You can retrieve the service's description, state, and startup type using the serviceDescription(), serviceFlags() and startupType() functions respectively. The service's state is decribed by the ServiceFlag enum. The mentioned properites can also be set using the corresponding set functions. In addition you can retrieve the service's name using the serviceName() function.

Several of QtServiceBase's protected functions are called on requests from the QtServiceController class:

- start()
- pause()
- processCommand()
- resume()
- stop()

You can control any given service using an instance of the QtServiceController class which also allows you to control services from separate applications. The mentioned functions are all virtual and won't do anything unless they are reimplemented. You can reimplement these functions to pause and resume the service's execution, as well as process user commands and perform additional clean-ups before shutting down.

QtServiceBase also provides the static instance() function which returns a pointer to an application's QtServiceBase instance. In addition, a service can report events to the system's event log using the logMessage() function. The MessageType enum describes the different types of messages a service reports.

The implementation of a service application's main function typically creates an service object derived by subclassing the QtService template class. Then the main function will call this service's exec() function, and return the result of that call. For example:

     int main(int argc, char **argv)
     {
         MyService service(argc, argv);
         return service.exec();
     }

When the exec() function is called, it will parse the service specific arguments passed in `argv`, perform the required actions, and return.

The following arguments are recognized as service specific:

Short | Long | Explanation
--- | --- | ---
-i | -install | Install the service.
-u | -uninstall | Uninstall the service.
-e | -exec | Execute the service as a standalone application (useful for debug purposes). This is a blocking call, the service will be executed like a normal application. In this mode you will not be able to communicate with the service from the contoller.
-t | -terminate | Stop the service.
-p | -pause | Pause the service.
-r | -resume | Resume a paused service.
-c cmd | -command cmd | Send the user defined command code cmd to the service application.
-v | -version | Display version and status information.

If none of the arguments is recognized as service specific, exec() will first call the createApplication() function, then executeApplication() and finally the start() function. In the end, exec() returns while the service continues in its own process waiting for commands from the service controller.

See also QtService and QtServiceController.

## Member Type Documentation

### enum QtServiceBase::MessageType

This enum describes the different types of messages a service reports to the system log.

Constant | Value | Description
--- | --- | ---
`QtServiceBase::Success` | `0` | An operation has succeeded, e.g. the service is started.
`QtServiceBase::Error` | `1` | An operation failed, e.g. the service failed to start.
`QtServiceBase::Warning` | `2` | An operation caused a warning that might require user interaction.
`QtServiceBase::Information` | `3` | Any type of usually non-critical information.

### enum QtServiceBase::ServiceFlag
flags QtServiceBase::ServiceFlags

This enum describes the different capabilities of a service.

Constant | Value | Description
--- | --- | ---
`QtServiceBase::Default` | `0x00` | The service can be stopped, but not suspended.
`QtServiceBase::CanBeSuspended` | `0x01` | The service can be suspended.
`QtServiceBase::CannotBeStopped` | `0x02` | The service cannot be stopped.
`QtServiceBase::NeedsStopOnShutdown` | `0x04` | (Windows only) The service will be stopped before the system shuts down. Note that Microsoft recommends this only for services that must absolutely clean up during shutdown, because there is a limited time available for shutdown of services.

The ServiceFlags type is a typedef for QFlags<ServiceFlag>. It stores an OR combination of ServiceFlag values.

## Member Function Documentation

### QtServiceBase::QtServiceBase ( int argc, char ** argv, const QString & name )

Creates a service instance called name. The argc and argv parameters are parsed after the exec() function has been called. Then they are passed to the application's constructor. The application type is determined by the QtService subclass.

The service is neither installed nor started. The name must not contain any backslashes or be longer than 255 characters. In addition, the name must be unique in the system's service database.

See also exec(), start(), and QtServiceController::install().

### QtServiceBase::~QtServiceBase () `[virtual]`

Destroys the service object. This neither stops nor uninstalls the service.

To stop a service the stop() function must be called explicitly. To uninstall a service, you can use the QtServiceController::uninstall() function.

See also stop() and QtServiceController::uninstall().

### void QtServiceBase::createApplication ( int & argc, char ** argv ) `[pure virtual protected]`

Creates the application object using the argc and argv parameters.

This function is only called when no service specific arguments were passed to the service constructor, and is called by exec() before it calls the executeApplication() and start() functions.

The createApplication() function is implemented in QtService, but you might want to reimplement it, for example, if the chosen application type's constructor needs additional arguments.

See also exec() and QtService.

### int QtServiceBase::exec ()

Executes the service.

When the exec() function is called, it will parse the service specific arguments passed in `argv`, perform the required actions, and exit.

If none of the arguments is recognized as service specific, exec() will first call the createApplication() function, then executeApplication() and finally the start() function. In the end, exec() returns while the service continues in its own process waiting for commands from the service controller.

See also QtServiceController.

### int QtServiceBase::executeApplication () `[pure virtual protected]`

Executes the application previously created with the createApplication() function.

This function is only called when no service specific arguments were passed to the service constructor, and is called by exec() after it has called the createApplication() function and before start() function.

This function is implemented in QtService.

See also exec() and createApplication().

### QtServiceBase * QtServiceBase::instance () `[static]`

Returns a pointer to the current application's QtServiceBase instance.

### void QtServiceBase::logMessage ( const QString & message, MessageType type = Success, int id = 0, uint category = 0, const QByteArray & data = QByteArray() )

Reports a message of the given type with the given message to the local system event log. The message identifier id and the message category are user defined values. The data parameter can contain arbitrary binary data.

Message strings for id and category must be provided by a message file, which must be registered in the system registry. Refer to the MSDN for more information about how to do this on Windows.

See also MessageType.

### void QtServiceBase::pause () `[virtual protected]`

Reimplement this function to pause the service's execution (for example to stop a polling timer, or to ignore socket notifiers).

This function is called in reply to controller requests. The default implementation does nothing.

See also resume() and QtServiceController::pause().

### void QtServiceBase::processCommand ( int code ) `[virtual protected]`

Reimplement this function to process the user command code.

This function is called in reply to controller requests. The default implementation does nothing.

See also QtServiceController::sendCommand().

### void QtServiceBase::resume () `[virtual protected]`

Reimplement this function to continue the service after a call to pause().

This function is called in reply to controller requests. The default implementation does nothing.

See also pause() and QtServiceController::resume().

### QString QtServiceBase::serviceDescription () const

Returns the description of the service.

See also setServiceDescription() and serviceName().

### ServiceFlags QtServiceBase::serviceFlags () const

Returns the service's state which is decribed using the ServiceFlag enum.

See also ServiceFlags and setServiceFlags().

### QString QtServiceBase::serviceName () const

Returns the name of the service.

See also QtServiceBase() and serviceDescription().

### void QtServiceBase::setServiceDescription ( const QString & description )

Sets the description of the service to the given description.

See also serviceDescription().

### void QtServiceBase::setServiceFlags ( ServiceFlags flags )

Sets the service's state to the state described by the given flags.

See also ServiceFlags and serviceFlags().

### void QtServiceBase::setStartupType ( QtServiceController::StartupType type )

Sets the service's startup type to the given type.

See also QtServiceController::StartupType and startupType().

### void QtServiceBase::start () `[pure virtual protected]`

This function must be implemented in QtServiceBase subclasses in order to perform the service's work. Usually you create some main object on the heap which is the heart of your service.

The function is only called when no service specific arguments were passed to the service constructor, and is called by exec() after it has called the executeApplication() function.

Note that you don't need to create an application object or call its exec() function explicitly.

See also exec(), stop(), and QtServiceController::start().

### QtServiceController::StartupType QtServiceBase::startupType () const

Returns the service's startup type.

See also QtServiceController::StartupType and setStartupType().

### void QtServiceBase::stop () `[virtual protected]`

Reimplement this function to perform additional cleanups before shutting down (for example deleting a main object if it was created in the start() function).

This function is called in reply to controller requests. The default implementation does nothing.

See also start() and QtServiceController::stop().