Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. International
  3. German
  4. QtService: Installierten Service in Windows10 starten
Forum Updated to NodeBB v4.3 + New Features

QtService: Installierten Service in Windows10 starten

Scheduled Pinned Locked Moved Solved German
2 Posts 1 Posters 590 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    henrik2016
    wrote on last edited by henrik2016
    #1

    Hallo,

    ich habe mit QtService eine einfache Dienst-Anwendung zum Testen erstellt. Diese lässt sich im QtCreator problemlos über die Kommandozeilenargumente ausführen:
    -exec: Damit kann ich den Dienst im QtCreator debuggen
    -install: Damit lässt sich der Dienst installieren (Alternativ kann ich auch über cmd mit dem Befehl sc den Dienst installieren).

    Nachdem ich den Dienst installiert habe, versuche ich diesen in der Windows Dienstverwaltung zu starten. Allerdings erscheint die Fehlermeldung:
    Der Dienst "MyService" auf "Lokaler Computer" konnte nicht gestartet werden.
    Fehler 1053: Der Dienst antwortete nicht rechtzeitig auf die Start- oder Steuerungsanforderung.

    Diese Fehlermeldung erscheint sofort, ohne 30 Sekunden zu versuchen den Dienst zu starten.
    Ich habe es im Debug- und im Release Mode versucht.

    EDIT: Meine Vermutung ist, dass Qt dlls noch zusätzlich neben der EXE liegen müssen, nur weiß ich nicht welche.

    simpleService.pro

    QT -= gui
    
    CONFIG += c++11 console
    CONFIG -= app_bundle
    
    # The following define makes your compiler emit warnings if you use
    # any Qt feature that has been marked deprecated (the exact warnings
    # depend on your compiler). Please consult the documentation of the
    # deprecated API in order to know how to port your code away from it.
    DEFINES += QT_DEPRECATED_WARNINGS
    
    # You can also make your code fail to compile if it uses deprecated APIs.
    # In order to do so, uncomment the following line.
    # You can also select to disable deprecated APIs only up to a certain version of Qt.
    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
    
    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"
    #include <QDateTime>
    #include <QFile>
    #include <QSettings>
    
    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";
        try {
    
        } catch (...) {
            qCritical() << "An unknown error occured in 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";
        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";
        }
    }
    
    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();
    }
    
    1 Reply Last reply
    0
    • H Offline
      H Offline
      henrik2016
      wrote on last edited by henrik2016
      #2

      Ich habe eine Lösung gefunden:

      1. In Qt 5.13.1(MinGW 7.3.0 64-bit) Compiler (In meinem Fall) zum Verzeichnis der EXE wechseln und folgenden Befehl eingeben:windeployqt.exe .
      2. Die QtSolutions_service-head.dll neben der EXE ablegen.

      So funktioniert es bei mir jetzt und ich kann den Dienst starten.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved