Search code examples
c++qtqt5qprocesstaskkill

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


I've some problems with killing a process using taskkill.

My code:

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

Output (translated from german):

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

Solution

  • "/IM testApp.exe" makes a single arg, but should be two args. You get the command taskkill /F "/IM testApp.exe". The proper invocation is

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