Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Create Windows Service by Qt
Forum Updated to NodeBB v4.3 + New Features

Create Windows Service by Qt

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 1.6k 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.
  • A Offline
    A Offline
    architect23
    wrote on 25 Mar 2020, 13:34 last edited by
    #1

    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???

    1 Reply Last reply
    0
    • P Offline
      P Offline
      paulpv
      wrote on 21 Oct 2022, 23:27 last edited by
      #2

      FYI, sc.exe just installs an app that was coded to comply with the requirement of writing a Windows Service.

      At a minimum you need to implement code that calls StartServiceCtrlDispatcher with a SERVICE_TABLE_ENTRY that points to your LPSERVICE_MAIN_FUNCTION that starts a loop that listens for service control commands/exit.

      As I recall, there used to be some 3rd party tools, perhaps even 1st party Microsoft nowadays, that could wrap nearly any exe to launch as a service.

      Sample code for how to write your own complete service is at:
      https://learn.microsoft.com/en-us/windows/win32/services/the-complete-service-sample

      I am familiar with services (from 10+ years ago) and am just no re-visiting them to launch and control a Qt app that captures the screen.
      I may report back here if I make any progress on this.

      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