Search code examples
c++qtwindows-services

QtService: Start installed Service on Windows10


I have created a simple service application for testing with QtService. This can be easily executed in the QtCreator using the command line arguments: -exec: This allows me to debug the service in QtCreator -install: This allows the service to be installed (Alternatively, I can also install the service via cmd with the sc command).

After I have installed the service, I try to start it in the Windows service management. However, the error message appears: The service "MyService" on "Local computer" could not be started. Error 1053: The service did not respond to the start or control request in time.

This error message appears immediately without trying to start the service for 30 seconds. I tried debug and release mode.

My guess is that Qt dlls must also be next to the EXE. But I don't know which.

simpleService.pro

QT -= gui

CONFIG += c++11 console
CONFIG -= app_bundle

DEFINES += QT_DEPRECATED_WARNINGS

SOURCES += \
        main.cpp \
        myservice.cpp

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

include(qtservice/src/qtservice.pri)

HEADERS += \
    myservice.h

myservice.h

#ifndef MYSERVICE_H
#define MYSERVICE_H

#include <qtservice.h>
#include <QCoreApplication>
#include <QDebug>
#include <QObject>

class MyService: public QtService<QCoreApplication>
{
public:
    MyService(int argc, char **argv);
    ~MyService();
protected:
    void start();
    void pause();
    void resume();
    void stop();
    void createApplication(int &argc, char **argv);

private:
    QStringList _args;
};

#endif // MYSERVICE_H

myservice.cpp

#include "myservice.h"

MyService::MyService(int argc, char **argv) : QtService<QCoreApplication>(argc, argv, "MyService7")
{
    try {
        qDebug() << "CONSTRUCTOR";

        setServiceDescription("This is my service. ");
        setServiceFlags(QtServiceBase::CanBeSuspended); // able to resume
        qDebug() << "CONSTRUCTOR 1";
    } catch (...) {
        qCritical() << "An unknown error occured in constructor";
    }
}

MyService::~MyService()
{
    qDebug() << "DECONSTRUCTOR";
}

void MyService::start()
{
    qDebug() << "START";
    try {
        QCoreApplication *app = application(); // nessesary for windows
        qDebug() << "Service started";
        qDebug() << app->applicationDirPath();
    } catch (...) {
        qCritical() << "An unknown error occured in start";
    }
}

void MyService::pause()
{
    qDebug() << "PAUSE";
}

void MyService::resume()
{
    qDebug() << "RESUME";
}

void MyService::stop()
{
    qDebug() << "STOP";
}

void MyService::createApplication(int &argc, char **argv)
{
    for (int i = 0; i < argc; i++)
    {
        _args.append(QString(argv[i]));
        qDebug() << "Arg: " << argv[i];
    }
    QtService::createApplication(argc, argv);
}

main.cpp

#include "myservice.h"
#include <QCoreApplication>

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

Solution

  • I found a solution:

    1. In Qt 5.13.1(MinGW 7.3.0 64-bit) Compiler (In my case) change the directory to exe file. Then enter following command: windeployqt.exe .

    2. Put the QtSolutions_service-head.dll next to exe file.

    Now I can start the service.