如何使用QProcess执行cmd命令? [英] How to execute a cmd command using QProcess?

查看:2324
本文介绍了如何使用QProcess执行cmd命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用

QProcess::startDetached("cmd /c net stop \"MyService\"");

这似乎没有停止服务。但是,如果我从开始>>运行它,它工作。

This does not seem to stop the service. However, if I run it from start >> run, it works.

推荐答案

QProcess :: startDetached将第一个参数执行的命令和由空格分隔的以下参数将被解释为命令的单独参数。

QProcess::startDetached will take the first parameter as the command to execute and the following parameters, delimited by a space, will be interpreted as separate arguments to the command.

因此,在这种情况下: -

Therefore, in this case: -

QProcess::startDetached("cmd /c net stop \"MyService\"");

该函数将 cmd 视为命令,并通过/ c,net,停止和MyService作为cmd的参数。

The function sees cmd as the command and passes /c, net, stop and "MyService" as arguments to cmd. However, other than /c, the others are parsed separately and are not valid arguments.

你需要做的是使用引号围绕net stop \MyService \将其作为单个参数传递,这样会给你: -

What you need to do is use quotes around the "net stop \"MyService\" to pass it as a single argument, so that would give you: -

QProcess::startDetached("cmd /c \"net stop \"MyService\"\"");

使用字符串列表你可以使用: -

Alternatively, using the string list you could use: -

QProcess::startDetached("cmd", QStringList() << "/c" << "net stop \"MyService\"");

这篇关于如何使用QProcess执行cmd命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆