Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QtService could not start

Tags:

c++

service

qt

I have a problem with QtService class. I am trying to to build a Windows service. I downloaded the files and included them into my project.

Now, when I run the project (in QtCreator) I get message

The service MyService could not start.

The method start() is not executed.

I've found upper message in file qtservice.cpp at the end of QtServiceBase::exec() implementation.

Do you have any idea why I get this message?

myservice.h:

#ifndef MYSERVICE_H
#define MYSERVICE_H

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

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

private:

};

#endif // MYSERVICE_H

myservice.cpp:

#include "myservice.h"

MyService::MyService(int argc, char **argv) : QtService<QCoreApplication>(argc, argv, "MyService")
{
    qDebug() << "CONSTRUCTOR";
    setServiceDescription("This is my service. ");
    setServiceFlags(QtServiceBase::CanBeSuspended);
    qDebug() << "CONSTRUCTOR 1";

}

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

    } catch (...) {
        qCritical() << "An unknown error occured in deconstructor";
    }
}

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

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

        qDebug() << "Service paused";
    } catch (...) {
        qCritical() << "An unknown error occured in pause";
    }
}

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

        qDebug() << "Service resumed";
    } catch (...) {
        qCritical() << "An unknown error occured in resume";
    }
}

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

        qDebug() << "Service stopped";
    } catch (...) {
        qCritical() << "An unknown error occured in stop";
    }
}

main.cpp:

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

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

and .pro file:

QT       += core gui network

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = ServiceTest
TEMPLATE = app


SOURCES += main.cpp\
        myservice.cpp

HEADERS  += myservice.h

include(QtService/qtservice.pri)
like image 805
janekpel Avatar asked Dec 18 '22 21:12

janekpel


1 Answers

Have you passed the argument -exec to the service? (click on "Project", then "Run Setting" then fill the Argument field).

The service wants to run as a service unless you pass the -exec parameter that tells it to run as an application (so you can debug it).

Other command line parameters are -install, -uninstall, -pause, etc. After you install the service with -install then you can run it using the Windows Administrative tools.

like image 99
Paolo Brandoli Avatar answered Dec 24 '22 02:12

Paolo Brandoli