简体   繁体   English

如何在Windows上通过QProcess启动提升的子流程?

[英]How to start an elevated subprocess through QProcess on Windows?

I'm using the QProcess class to start a subprocess that performs a task. 我正在使用QProcess类来启动执行任务的子流程。 Sometimes the process needs administrator privileges. 有时,该过程需要管理员权限。 On Linux I simply run it through pkexec and everything just works. 在Linux上,我只是通过pkexec运行它,一切都正常。 How can I accomplish the same effect on Windows? 如何在Windows上实现相同的效果?

To be clear: I want to be able to decide at runtime whether to run it as Administrator or not. 需要说明的是:我希望能够在运行时决定是否以管理员身份运行它。 I also need to be able to communicate with the process via stdin/stdout. 我还需要能够通过stdin / stdout与该进程进行通信。

Or, to put it in code: 或者,将其放入代码中:

void modifyArgsForRoot(QString &program, QStringList &args)
{
#if defined(Q_OS_LINUX)
    args.prepend(program);
    program = "pkexec";
#elif defined(Q_OS_WIN32)
    // what do I put here? //////////////////////////////
#endif
}

void foo()
{
    QProcess p;
    QString program;
    QStringList arguments;
    // ......
    if (!hasWriteAccessToCertainDir())
        modifyArgsForRoot(program, arguments);

    p.start(program, arguments);
}

You might want to have a look at the runas command , which is somewhat of an alternative to pkexec for Windows. 您可能需要看一下runas命令 ,它对于Windows的pkexec来说有点替代。

For instance, you could try 例如,您可以尝试

#elif defined(Q_OS_WIN32)
    args.prepend(program);
    args.prepend("/user:mymachine\administrator"); // Change accordingly
    args.prepend("/noprofile"); // See link above
    program = "runas";
#endif

Edit: You could also try to use a manifest, as described here . 编辑:您还可以尝试使用清单,作为描述在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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