Results 1 to 4 of 4

Thread: Problems starting a QtService using QtServiceController

  1. #1
    Join Date
    Aug 2010
    Location
    Horten, Norway
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Problems starting a QtService using QtServiceController

    Hi,

    I have a QtServiceController related problem. I have made a service derived from QtService. The service functions as it should, and will install and run from a commandline (as Administrator) using the -i and -s arguments. The problem arises when I try to control the service from a separate Qt GUI application using the QtServiceController. I can successfully install the service, but it will not start. I have tried to run a release compiled executable of the GUI app as administrator, that didn't work either. This code reproduces the problem on my computer:

    serviceTemplate.h :

    Qt Code:
    1. #ifndef SERVICE_TEMPLATE_H
    2. #define SERVICE_TEMPLATE_H
    3. #include <QtServiceBase>
    4.  
    5. // definition of the service class
    6. class CService : public QtService<QCoreApplication>
    7. {
    8. public:
    9. CService(int argc, char **argv);
    10. ~CService(){};
    11.  
    12. protected:
    13. void start();
    14. void stop(){};
    15. void pause(){};
    16. void resume(){};
    17. void processCommand(int code){};
    18.  
    19. private:
    20.  
    21. };
    22. #endif
    To copy to clipboard, switch view to plain text mode 

    serviceTemplate.cpp :

    Qt Code:
    1. #include "serviceTemplate.h"
    2.  
    3. CService::CService(int argc, char **argv)
    4. : QtService<QCoreApplication>(argc, argv, "Qt Service")
    5. {
    6. setServiceDescription("A dummy Qt Service");
    7. setServiceFlags(QtServiceBase::CanBeSuspended);
    8. }
    9.  
    10. // this service does nothing
    11. void CService::start()
    12. {
    13. QCoreApplication *app = application();
    14. while(1){ // Do nothing }
    15. }
    To copy to clipboard, switch view to plain text mode 

    relevant code from the GUI app :

    Qt Code:
    1. // install service
    2. bool ires = false;
    3. QString servicePath = installPath + "\\service\\backupservice.exe"; // serviceTemplate.exe
    4. serviceController = new QtServiceController(servicePath);
    5. if(!serviceController->isInstalled())
    6. {
    7. log->append("\n" + QDateTime::currentDateTime().toString() +
    8. "\n" + "Service not installed, installing...");
    9. if((ires = serviceController->install(servicePath)) == false)
    10. {
    11. log->append("\n" + QDateTime::currentDateTime().toString() +
    12. "\n" + "Error installing service, try running application as Administrator ");
    13. MenuTab->setCurrentWidget(log);
    14. }
    15. }
    16. bool startOK = serviceController->start();
    17. bool isRunning = serviceController->isRunning();
    18. if(isRunning)
    19. {
    20. log->append("\n" + QDateTime::currentDateTime().toString() +
    21. "\n" + "Service is running");
    22. }
    23. else
    24. {
    25. // ends up here always.. why???
    26. log->append("\n" + QDateTime::currentDateTime().toString() +
    27. "\n" + "Could not start service, try running application as Administrator");
    28. MenuTab->setCurrentWidget(log);
    29. }
    To copy to clipboard, switch view to plain text mode 

    I migth also add that the service controller does not report the correct install-status of the service, it will always report "not installed" e.g. serviceController->isInstalled() = false, even though it is installed (confirmed with task manager). So the only thing that I can make it to do correctly, is installing the service.

    Are there any known circumstances in which the service controller can't start a service, when the service can be started manually? Any help would be greatly appreciated, I've been struggling with this for some days now.

    OS : Windows 7, 64
    Qt : 4.5.2

  2. #2
    Join Date
    Aug 2010
    Location
    Horten, Norway
    Posts
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problems starting a QtService using QtServiceController

    Problem remains unsolved. Noone seems to want to touch it. Please reply here if one or more of the following is valid:
    * you have the same problem, and you cannot solve it either
    * you had the same problem, but you solved it
    * you see whats wrong with the code
    * you think the code looks ok, "there must be something else"
    * you could perhaps see what's wrong, but you need more information

    have a good weekend everyone!

  3. #3
    Join Date
    Sep 2009
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problems starting a QtService using QtServiceController

    I have similar problem as You Stiander. I use Qt 4.7 and it doesn't see QtServiceController class. I have not found in doc on qt.nokia.com informations about this QtService module in Qt 4.7. It means that it is not support now? So how create program as service and control its cycle from Qt level?

  4. #4
    Join Date
    May 2011
    Location
    near munich
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problems starting a QtService using QtServiceController

    Your CService::start() function shouldn´t be blocking. It is not supposed to be the service´s main function (it took me some time to figure that out...). Using these classes there is no main function. You just get an event loop which you start in your programs main() function using myservice.exec().

    To implement your functionality you have to use an event driven approach (signals and slots). Look at the following example. It writes once a second a string to your applications event log.

    Qt Code:
    1. #ifndef SVCMAIN_H
    2. #define SVCMAIN_H
    3.  
    4. #include <QObject>
    5.  
    6. class SvcMain : public QObject
    7. {
    8. Q_OBJECT
    9. public:
    10. explicit SvcMain(QObject *parent = 0);
    11.  
    12. signals:
    13.  
    14. public slots:
    15. void handleTimerEvent();
    16. };
    17.  
    18. #endif // SVCMAIN_H
    To copy to clipboard, switch view to plain text mode 

    The constructor creates a periodic timer:

    Qt Code:
    1. #include "svcmain.h"
    2. #include <QTimer>
    3. #include "qtservice/qtservice.h"
    4.  
    5. SvcMain::SvcMain(QObject *parent) :
    6. QObject(parent)
    7. {
    8. QTimer *timer = new QTimer(this);
    9. connect(timer, SIGNAL(timeout()), this, SLOT(handleTimerEvent()));
    10. timer->start(1000);
    11. }
    12.  
    13. void SvcMain::handleTimerEvent()
    14. {
    15. QtServiceBase::instance()->logMessage(QString("handleTimerEvent"), QtServiceBase::Information );
    16. }
    To copy to clipboard, switch view to plain text mode 

    Your start() function should look like this:

    Qt Code:
    1. void CService::start()
    2. {
    3. svcmain = new SvcMain();
    4. }
    To copy to clipboard, switch view to plain text mode 

    svcmain is a private member variable:

    Qt Code:
    1. private:
    2. SvcMain* svcmain;
    To copy to clipboard, switch view to plain text mode 

    Look at the examples on how to implement the remaining functions (stop/resume/...).
    Hope this helps.

    Sebastian

Similar Threads

  1. Replies: 0
    Last Post: 1st May 2010, 12:41
  2. QtService on Windows as Non-Interactive
    By stefanadelbert in forum Qt Programming
    Replies: 2
    Last Post: 26th April 2010, 03:27
  3. problems starting process
    By parsito in forum Qt Programming
    Replies: 8
    Last Post: 11th May 2007, 21:32
  4. QtService fails to start
    By a550ee in forum Qt Programming
    Replies: 1
    Last Post: 9th November 2006, 13:34
  5. QtService and GAC loadable dll
    By stevey in forum Qt Programming
    Replies: 3
    Last Post: 17th July 2006, 03:39

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.