如何使用 QProcess::execute() 终止进程?

How to kill a process with QProcess::execute()?

我在使用 taskkill 终止进程时遇到了一些问题。

我的代码:

QStringList args;
args << "/F";
args << "/IM testApp.exe";
QProcess::execute("taskkill", args); //Should be 'taskkill /IM testApp.exe /F'

输出(翻译自德语):

ERROR: Invalid argument - "/IM testApp.exe".
Type "TASKKILL /?" to show the syntax.

"/IM testApp.exe" 生成一个参数,但应该是两个参数。你得到命令 taskkill /F "/IM testApp.exe"。正确的调用是

QStringList args;
args << "/F";
args << "/IM";
args << "testApp.exe";
QProcess::execute("taskkill", args);