我知道這個問題是舊的,但如何保持解答和問題仍然存在,直到今天,我認爲是適當的給出一個可能的答案。
您可以通過重新實現void QtServiceBase::createApplication (int & argc, char ** argv)
得到一個Qt服務的啓動參數按照docs:
This function is only called when no service specific arguments were passed to the service constructor, and is called by exec() before it calls the executeApplication() and start() functions.
所以,當你的服務調用start功能ARGS將可用,因爲在start
函數之前調用createApplication
。
這裏,例如:
#include <QtCore>
#include "qtservice.h"
class Service : public QtService<QCoreApplication>
{
public:
explicit Service(int argc, char *argv[], const QString &name) : QtService<QCoreApplication>(argc, argv, name)
{
setServiceDescription("Service");
setServiceFlags(QtServiceBase::CanBeSuspended);
setStartupType(QtServiceController::ManualStartup);
}
protected:
void start()
{
// use args;
}
void stop()
{
}
void pause()
{
}
void resume()
{
}
void processCommand(int code)
{
}
void createApplication(int &argc, char **argv)
{
for (int i = 0; i < argc; i++)
args.append(QString(argv[i]));
QtService::createApplication(argc, argv);
}
private:
QStringList args;
};
int main(int argc, char *argv[])
{
Service s(argc, argv, "Service");
return s.exec();
}