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]
What i do wrong??? -
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 aSERVICE_TABLE_ENTRY
that points to yourLPSERVICE_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-sampleI 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.