开发者

QtService does not pass the start parameters from Service Manager window

开发者 https://www.devze.com 2023-04-07 04:59 出处:网络
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.

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.

QtService does not pass the start parameters from Service Manager window

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号