How to execute cmd command using QProcess?

I am trying to execute cmd command with

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

This is not like stopping a service. However, if I run it from start → run, it works.

+4
source share
1 answer

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

Therefore, in this case: -

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

The function sees cmd as a command and passes / c, net, stop and "MyService" as arguments to cmd. However, apart from / c, the rest are analyzed separately and are not valid arguments.

, "net stop" "MyService", , : -

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

, , : -

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

Source: https://habr.com/ru/post/1525558/


All Articles