[英]How to execute a cmd command using QProcess?
I am attempting to execute a cmd command using 我正在尝试使用执行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.
但是,如果我从start >> run运行它,它的工作原理。
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. QProcess :: startDetached将第一个参数作为要执行的命令,并且由空格分隔的以下参数将被解释为命令的单独参数。
Therefore, in this case: - 因此,在这种情况下: -
QProcess::startDetached("cmd /c net stop \"MyService\"");
The function sees cmd as the command and passes /c, net, stop and "MyService" as arguments to cmd. 该函数将cmd视为命令,并将/ c,net,stop和“MyService”作为cmd的参数传递。 However, other than /c, the others are parsed separately and are not valid arguments.
但是,除了/ c之外,其他的都是单独解析的,并且不是有效的参数。
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: - 你需要做的是使用“net stop \\”MyService \\“附近的引号将它作为一个参数传递给你,这样你就可以:
QProcess::startDetached("cmd /c \"net stop \"MyService\"\"");
Alternatively, using the string list you could use: - 或者,使用字符串列表,您可以使用: -
QProcess::startDetached("cmd", QStringList() << "/c" << "net stop \"MyService\"");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.