Home

QtService Class Reference

The QtService class provides an API from implementing Windows services and Unix daemons. More...

#include <qtservice.h>

List of all member functions.

Public Members

Protected Members


Detailed Description

The QtService class provides an API from implementing Windows services and Unix daemons.

A Windows service or Unix daemon (a "service"), is a program that runs regardless of whether a user is logged in or not. Services are usually non-interactive console applications, but can also provide a user interface.

A service is installed in a system's service database with install(). The system will start the service on startup, depending on the StartupType specified. A service can also be started explicitly with exec().

On Windows 95, 98 and ME the service is registered in the Windows Registry's RunServices entry.

On Windows NT based systems, such as Windows NT 4, 2000, and XP, the 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 the service is implemented as a daemon.

When a service is started, it calls initialize(), and then the run() implementation which usually creates a QApplication object and enters the event loop. The stop() implementation must make the application leave the event loop, so that the run() implementation can return; usually this is done by calling qApp->quit().

On Windows NT based systems the service control manager can send commands to the service to pause(), resume(), or stop() the service, as well as service specific user() commands. This can be achieved on other systems by running the executable itself with suitable command line arguments.

A service can report events to the system's event log with reportEvent(). The status of the service can be queried with isInstalled() and isRunning().

A running service can be stopped by the service control manager, or by calling terminate(). If the service is no longer needed it can be uninstalled from the system's service database with uninstall().

The implementation of a service application's main() entry point function usually creates a QtService object, calls parseArguments(), and returns the result of that call.

When a service object is destroyed the service is not stopped and is not uninstalled.

Warning: On Windows, different versions have different limitations as to what a service is allowed to do. For example, on Windows XP Home Edition the Local System Account does not have the privilege to "Logon as a service". If you have problems with a service consult the documentation provided by Microsoft, i.e. in MSDN.


Member Type Documentation

QtService::EventType

This enum describes the different types of events reported by a service to the system log.

QtService::StartupType

This enum describes when a service should be started.


Member Function Documentation

QtService::QtService ( const QString & name, const QString & desc = QString::null, StartupType startup = Auto )

Constructs a QtService object called name and with the optional description desc. startup specifies when the service should be started. The default is Auto.

There can only be one QtService object in a process.

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

See also install() and exec().

QtService::~QtService () [virtual]

Destroys this service object. This does not stop or uninstall the service.

See also uninstall().

int QtService::exec ( int argc, char ** argv ) [virtual]

Tries to start the service, passing argc and argv to the run() implementation.

If the service is installed it is started as a separate process, and exec() returns immediately with the return value 0 if the service could be started. Otherwise a non-zero error code is returned and an error event is reported to the system's event log.

If the service is not installed the run() implementation is called directly, and the result is returned.

The default implementation of parseArguments() calls this function when the command line option -e (or -exec) is used.

See also install(), start(), and run().

bool QtService::initialize () [virtual protected]

Reimplement this function to perform service dependent initialization routines, and return true if successful; otherwise return false.

The default implementation does nothing and returns true.

See also run().

bool QtService::install () [virtual]

Installs the service in the system's service control manager and returns true if successful; otherwise returns false.

The service reports the result of the installation to the system's event log.

The default implementation of parseArguments() calls this function when the command line option -i (or -install) is used.

Warning: Due to the different implementations of how services (daemons) are installed on various UNIX-like systems, this function is not implemented on such systems.

See also uninstall().

bool QtService::isInstalled () const [virtual]

Returns true if the service is installed in the system's default service control manager; otherwise returns false.

Warning: This function always returns false on UNIX-like systems.

See also install().

bool QtService::isRunning () const [virtual]

Returns true if the service is running; otherwise returns false.

A service must be installed before it can be run, except on UNIX-like systems; see install().

Note that isRunning() returns false if the program runs stand-alone, so you can modify your application's behavior depending on the context it is running in:

    int MyService::run( int argc, char **argv )
    {
        QApplication app( argc, argv );

        QWidget *gui = new ServiceGui( ... );
        if ( !isRunning() ) // running stand alone -> quit when GUI is closed
            app.setMainWidget( gui );

        gui->show();
        return app.exec();
    }
    

See also isInstalled(), exec(), start(), and stop().

int QtService::parseArguments ( int argc, char ** argv )

Parses argc command line arguments in argv and returns an error code, or zero if no error occurred.

The following arguments are recognized:

Short Long Explanation
-i -install Calls install() to install the service
-u -uninstall Calls uninstall() to uninstall the service
-e -exec Calls exec() to execute the service application
-t -terminate Calls terminate() to stop the service application
-p -pause Call requestPause() to pause the service application
-r -resume Call requestResume() to resume a paused service application
-c cmd -command cmd Send the user defined command code cmd to the service application
-v -version Displays version and status information

If no argument is provided the service calls start() to run the service and listen to commands from the service control manager.

Examples: interactive/main.cpp and server/main.cpp.

void QtService::pause () [virtual protected]

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

The default implementation does nothing.

See also resume() and requestPause().

Example: interactive/main.cpp.

void QtService::reportEvent ( const QString & message, EventType type = Success, int ID = 0, uint category = 0, const QByteArray & data = QByteArray ( ) ) [virtual]

Report an event of type type with text message to the local system event log. The event identifier ID and the event category category are user defined values. data 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.

void QtService::requestPause () [virtual]

Requests the running service to pause. The service will call the pause() implementation.

This function does nothing if the service is not running.

See also requestResume().

void QtService::requestResume () [virtual]

Requests a paused service to continue. The service will call the resume() implementation.

This function does nothing if the service is not running.

See also requestPause().

void QtService::resume () [virtual protected]

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

The default implementation does nothing.

See also pause() and requestResume().

Example: interactive/main.cpp.

int QtService::run ( int argc, char ** argv ) [pure virtual protected]

This pure virtual function must be implemented in derived classes in order to perform the service's work. Usually you create the QtService subclass instance in main() and return the value of a call to parseArguments(). In the subclass's run() function you create the QApplication object passing argc and argv, initialize the application, and enter the event loop (e.g. by calling return app.event(); in this function.

See also start(), exec(), terminate(), and stop().

Example: interactive/main.cpp.

void QtService::sendCommand ( int code ) [virtual]

Sends the user command code to the service. The service will call the user() implementation.

This function does nothing if the service is not running.

QString QtService::serviceDescription () const

Returns the description of the service.

See also serviceName().

QString QtService::serviceName () const

Returns the name of the service.

See also serviceDescription().

bool QtService::start () [virtual protected]

Starts the service, and returns true if successful; otherwise returns false.

A service must be installed before it can be started, except on UNIX-like systems; see install().

The service reports an error EventType to the system event log if starting the service fails.

The default implementation of parseArguments() calls this function if no command line options are used.

See also install(), exec(), stop(), and isRunning().

StartupType QtService::startupType () const

Returns the service's startup type.

See also StartupType.

void QtService::stop () [virtual protected]

Stops the service. The default implementations calls quit() if there is a QApplication object, otherwise it does nothing. Reimplement this function if you want to perform additional cleanups before shutting down.

See also run() and terminate().

bool QtService::terminate () [virtual]

Asks the service control manager to stop the service if the service is running, otherwise does nothing. Returns true if the service could be stopped (or was not running); otherwise returns false.

The default implementation of parseArguments() calls this function when the command line option -t (or -terminate) is used.

See also exec() and isRunning().

bool QtService::uninstall () [virtual]

Uninstalls the service from the system's default service control manager and returns true if successful; otherwise returns false.

The service reports the result of the uninstallation to the system's event log.

The default implementation of parseArguments() calls this function when the command line option -u (or -uninstall) is used.

Warning: Due to the different implementations of how services (daemons) are installed on various UNIX-like systems, this function is not implemented on such systems.

See also install() and isInstalled().

void QtService::user ( int code ) [virtual protected]

Reimplement this function to process the user command code.

The default implementation does nothing.

See also sendCommand().

Example: interactive/main.cpp.


This file is part of the Qt Solutions. Copyright © 2003-2005 Trolltech. All Rights Reserved.

Copyright © 2003-2005 TrolltechTrademarks
Qt Solutions