我正在尝试使用
执行cmd命令QProcess::startDetached("cmd /c net stop \"MyService\"");
这似乎并没有停止服务。但是,如果我从开始>>运行它跑,它有效。
答案 0 :(得分:4)
QProcess :: startDetached将第一个参数作为要执行的命令,并且由空格分隔的以下参数将被解释为命令的单独参数。
因此,在这种情况下: -
QProcess::startDetached("cmd /c net stop \"MyService\"");
该函数将 cmd 视为命令,并将/ c,net,stop和“MyService”作为cmd的参数传递。但是,除了/ c之外,其他的都是单独解析的,并且不是有效的参数。
您需要做的是使用“net stop \”MyService \“附近的引号将其作为单个参数传递,这样就可以了: -
QProcess::startDetached("cmd /c \"net stop \"MyService\"\"");
或者,使用字符串列表,您可以使用: -
QProcess::startDetached("cmd", QStringList() << "/c" << "net stop \"MyService\"");