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 :
#ifndef SERVICE_TEMPLATE_H
#define SERVICE_TEMPLATE_H
#include <QtServiceBase>
// definition of the service class
class CService : public QtService<QCoreApplication>
{
public:
CService(int argc, char **argv);
~CService(){};
protected:
void start();
void stop(){};
void pause(){};
void resume(){};
void processCommand(int code){};
private:
};
#endif
#ifndef SERVICE_TEMPLATE_H
#define SERVICE_TEMPLATE_H
#include <QtServiceBase>
// definition of the service class
class CService : public QtService<QCoreApplication>
{
public:
CService(int argc, char **argv);
~CService(){};
protected:
void start();
void stop(){};
void pause(){};
void resume(){};
void processCommand(int code){};
private:
};
#endif
To copy to clipboard, switch view to plain text mode
serviceTemplate.cpp :
#include "serviceTemplate.h"
CService::CService(int argc, char **argv)
: QtService<QCoreApplication>(argc, argv, "Qt Service")
{
setServiceDescription("A dummy Qt Service");
setServiceFlags(QtServiceBase::CanBeSuspended);
}
// this service does nothing
void CService::start()
{
while(1){ // Do nothing }
}
#include "serviceTemplate.h"
CService::CService(int argc, char **argv)
: QtService<QCoreApplication>(argc, argv, "Qt Service")
{
setServiceDescription("A dummy Qt Service");
setServiceFlags(QtServiceBase::CanBeSuspended);
}
// this service does nothing
void CService::start()
{
QCoreApplication *app = application();
while(1){ // Do nothing }
}
To copy to clipboard, switch view to plain text mode
relevant code from the GUI app :
// install service
bool ires = false;
QString servicePath
= installPath
+ "\\service\\backupservice.exe";
// serviceTemplate.exe serviceController = new QtServiceController(servicePath);
if(!serviceController->isInstalled())
{
log
->append
("\n" + QDateTime::currentDateTime().
toString() + "\n" + "Service not installed, installing...");
if((ires = serviceController->install(servicePath)) == false)
{
log
->append
("\n" + QDateTime::currentDateTime().
toString() + "\n" + "Error installing service, try running application as Administrator ");
MenuTab->setCurrentWidget(log);
}
}
bool startOK = serviceController->start();
bool isRunning = serviceController->isRunning();
if(isRunning)
{
log
->append
("\n" + QDateTime::currentDateTime().
toString() + "\n" + "Service is running");
}
else
{
// ends up here always.. why???
log
->append
("\n" + QDateTime::currentDateTime().
toString() + "\n" + "Could not start service, try running application as Administrator");
MenuTab->setCurrentWidget(log);
}
// install service
bool ires = false;
QString servicePath = installPath + "\\service\\backupservice.exe"; // serviceTemplate.exe
serviceController = new QtServiceController(servicePath);
if(!serviceController->isInstalled())
{
log->append("\n" + QDateTime::currentDateTime().toString() +
"\n" + "Service not installed, installing...");
if((ires = serviceController->install(servicePath)) == false)
{
log->append("\n" + QDateTime::currentDateTime().toString() +
"\n" + "Error installing service, try running application as Administrator ");
MenuTab->setCurrentWidget(log);
}
}
bool startOK = serviceController->start();
bool isRunning = serviceController->isRunning();
if(isRunning)
{
log->append("\n" + QDateTime::currentDateTime().toString() +
"\n" + "Service is running");
}
else
{
// ends up here always.. why???
log->append("\n" + QDateTime::currentDateTime().toString() +
"\n" + "Could not start service, try running application as Administrator");
MenuTab->setCurrentWidget(log);
}
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
Bookmarks