简体   繁体   中英

QtService does not pass the start parameters from Service Manager window

How to get the Start parameters that are passed from the Windows Service Manager dialog. I was hoping I would get them as command line args passed to the main function.

在此处输入图片说明

If I pass the arguments to binPath when creating the service then I get the arguments passed into main function.

sc create "Myservice" binPath= "Path_to_exe\Myservice.exe -port 18082"

But this way we need to uninstall and install the service everytime to change any arguments. Is there any way to get the start parameters in Qt?

If I create the service using .NET, I can use the following function to get these Start parameters.

 System::Environment::GetCommandLineArgs();

I know that this question is old, but how it keeps unanswered and the problem persist until nowadays, I believe that is appropriate give a possible answer.

You will be able to get the start parameters of a Qt Service by reimplementing void QtServiceBase::createApplication ( int & argc, char ** argv )

According to the 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.

So when your service call the start function the args will be available, because the createApplication is called before the start function.

Here a example:

#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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM