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