Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Create Windows Service by Qt



  • Hi, I'm trying to create windows service using Qt, but I can’t. I have a code:
    main.h

    #include "mainwindow.h"
    #include <QApplication>
    #include "ckeyboardtest.h"
    //#include <iostream>
    
    int main(int argc, char *argv[])
    {
        CKeyboardTest a(argc, argv);
        return             a.exec();
    }
    

    ckeyboardtest.h

    #ifndef CKEYBOARDTEST_H
    #define CKEYBOARDTEST_H
    
    #include <QObject>
    #include <QtCore/QCoreApplication>
    #include "qtservice.h"
    
    class CKeyboardTest : public QObject, public QtService<QCoreApplication>
    {
        Q_OBJECT
    public:
         CKeyboardTest(int argc, char **argv);
         ~CKeyboardTest();
    protected:
         void start();
         void stop();
         void pause();
         void resume();
         void processCommand(int nCode);
    
    signals:
    
    public slots:
    };
    
    #endif // CKEYBOARDTEST_H
    

    ckeyboardtest.cpp

    #include "ckeyboardtest.h"
    
    CKeyboardTest::CKeyboardTest(int argc, char **argv) : QtService<QCoreApplication>(argc, argv, "TestService9")
    {
        try
        {
            setServiceDescription("This is my service");
            setServiceFlags(QtServiceBase::CanBeSuspended);
        }
        catch(...)
        {
            //qCritical() << "An unknow error in the constructor";
        }
    }
    
    CKeyboardTest::~CKeyboardTest()
    {
    
    }
    
    void CKeyboardTest::pause()
    {
    
    }
    
    void CKeyboardTest::processCommand(int nCode)
    {
    
    }
    
    void CKeyboardTest::resume()
    {
    
    }
    
    void CKeyboardTest::start()
    {
        try
        {
            QCoreApplication *app = application();
        }
        catch(...)
        {
           // qCritical() << "An unknow error in start";
        }
    
    }
    
    void CKeyboardTest::stop()
    {
    
    }
    
    

    I also have files taken from an example(qt-solutions-master): qtservice.h, qtservice.cpp, qtservice_p.h, qtservice_win.cpp
    I can create service from my lindcommand: sc create "SimpleService" binpath= C:..\My.exe
    But I can't start it![alt text]![alt text](Start Service.JPG image url)
    What i do wrong???


Log in to reply