-1
QProcess p;
QString aa = "tasklist /FI 'IMAGENAME x32dbg.exe' /FO LIST | findstr 'PID:'";
aa.replace(0x27,0x22);
qInfo() << aa; 
p.start(aa.toStdString().c_str());
p.waitForFinished();
qInfo() << "Output:" << p.readAllStandardOutput() << "Error:" << p.readAllStandardError();
// returned error <ERROR: Invalid argument/option - 'x32dbg.exe\"'.\r\nType \"TASKLIST /?\" for usage.\r\n">

qebug return

{tasklist /FI \"IMAGENAME x32dbg.exe\" /FO LIST | findstr \"PID:\"}

the correct text must be

{tasklist /FI "IMAGENAME eq x32dbg.exe" /FO LIST | findstr "PID:"}

i tried with \" and add the command line in const char * all return same result

  • There is no `eq` in the original string. There is no problems in QString, you see the string as you would see it in a C++ code. You should use escaped " in the code instead of `aa.replace(0x27,0x22)`, QString is unnecessary. – 273K May 29 '22 at 20:55
  • i tried it with \" but qprocess stay receive the code with \" and code will not working – Fadi Almhimeed May 29 '22 at 21:07
  • You made an error somewhere else. The string looks well. – 273K May 29 '22 at 22:19
  • i tried a lot of solutions but qprocess get the string with \" – Fadi Almhimeed May 29 '22 at 22:23
  • I would not say you tried a lot. I would say you tried to guess. Better is read the manual. I have read and posted the answer. – 273K May 29 '22 at 22:29
  • Instead playing with command line which is platform specific it is better to use platform specific API to read list of processes. Just use [EnumProcesses](https://learn.microsoft.com/pl-pl/windows/win32/api/psapi/nf-psapi-enumprocesses?redirectedfrom=MSDN), [OpenProcess](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-openprocess) and GetModuleBaseName. See [example](https://learn.microsoft.com/en-us/windows/win32/psapi/enumerating-all-processes) – Marek R May 29 '22 at 22:46

3 Answers3

1

The problem is you cannot run pipes with QProcess, but only a single process. The workaround would be to pass your command as an argument to cmd.exe:

QProcess p;
p.start("cmd.exe", QStringList() << "/C" << "tasklist /FI \"IMAGENAME x32dbg.exe\" /FO LIST | findstr \"PID:\"");
273K
  • 29,503
  • 10
  • 41
  • 64
  • stay the same error "ERROR: Invalid argument/option - 'x32dbg.exe\"'.\r\nType \"TASKLIST /?\" for usage.\r\n" – – Fadi Almhimeed May 29 '22 at 22:50
  • @FadiAlmhimeed Fix the command. I don't know your code and command. SO is not a site where you will be directed in typing each character. – 273K May 29 '22 at 22:52
0

QDebug's quoting option enabled by default, so use noquote().

snan
  • 24
  • 3
  • the main problem not with qdebug it's with qprocess command bcz qprocess recieve the text with \" and code will not working – Fadi Almhimeed May 29 '22 at 21:12
0
bool isRunning(const QString &process) {
  QProcess tasklist;
  tasklist.start(
        "tasklist",
        QStringList() << "/NH"
                      << "/FO" << "CSV"
                      << "/FI" << QString("IMAGENAME eq %1").arg(process));
  tasklist.waitForFinished();
  QString output = tasklist.readAllStandardOutput();
  qInfo() << output ;