add double qoutes to qstring or qprocess
-
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
-
@SGaist
@JonB
@Christian-Ehrlicher
thanks all
i found the solutionbool 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 ; }
-
Hi,
qDebug is a debugging tool and not a "pure string printer". In that sense, the backslashes there are correct, they tells you that you have quotes in your string because that would be how you write it in your code.
-
@SGaist said in add double qoutes to qstring or qprocess:
qDebug is a debugging tool and not a "pure string printer". In that sense, the backslashes there are correct, they tells you that you have quotes in your string because that would be how you write it in your code.
@SGaist also in qprocess receive the code with \" and code will not working
QString a = p.readAllStandardOutput();
qInfo() << a;a return empty
-
@SGaist said in add double qoutes to qstring or qprocess:
qDebug is a debugging tool and not a "pure string printer". In that sense, the backslashes there are correct, they tells you that you have quotes in your string because that would be how you write it in your code.
@SGaist also in qprocess receive the code with \" and code will not working
QString a = p.readAllStandardOutput();
qInfo() << a;a return empty
@fadu
You should alsop.readAllStandardError();
for any error message. And you cannot do this immediately afterp.start()
, you needp.waitForFinished()
.Your command line contains a
|
redirection symbol. So you need to send the whole command tocmd
(unless you do the direction yourself and twoQProcess
es). Like:p.start("cmd", { "/c", "tasklist /FI \"IMAGENAME x32dbg.exe\" /FO LIST | findstr \"PID:\"" } );
For the case of this is simply to do a
findstr "PID:"
you could do that in the calling Qt code by searchingreadAllStandardOutput()
for the string yourself, if you chose. -
@JonB
p.wait it's added and error replayed with "ERROR: Invalid argument/option - 'x32dbg.exe"'.\r\nType "TASKLIST /?" for usage.\r\n
qprocess didn't detect \" and remove the \@fadu said in add double qoutes to qstring or qprocess:
qprocess didn't detect " and remove the \
No -
\"
has nothing to do with QProcess but plain c. You (properly) escape a"
here.
Don't use cmd.exe but start tasklist directly, properly pass the paramaters and then parse the output with Qt. -
@JonB
p.wait it's added and error replayed with "ERROR: Invalid argument/option - 'x32dbg.exe"'.\r\nType "TASKLIST /?" for usage.\r\n
qprocess didn't detect \" and remove the \@fadu said in add double qoutes to qstring or qprocess:
"ERROR: Invalid argument/option - 'x32dbg.exe"'.\r\nType "TASKLIST /?" for usage.\r\n
I do not know what your problem is. First try the command outside of Qt or C++ from a Command Prompt:
tasklist /FI "IMAGENAME x32dbg.exe" /FO LIST | findstr "PID:"
Note the
"
s you use. When you put those into a C++ literal string, enclosed in"
s, you need to put a\"
for each"
, hence:"tasklist /FI \"IMAGENAME x32dbg.exe\" /FO LIST | findstr \"PID:\""
Meanwhile however testing in a Command Prompt I get:
C:\Users\Jon>tasklist /FI "IMAGENAME x32dbg.exe" /FO LIST | findstr "PID:" ERROR: The search filter cannot be recognized.
For me I cannot get anything to be accepted as the
/FI
argument, so I don't what is going on!Start by getting a simple example working (which I can't), like just
tasklist /FI System /FO LIST
and build up from there.
UPDATE
I looked attasklist /?
and you are not using/FI
correctly. It needs to betasklist /FI "IMAGENAME eq x32dbg.exe"
Alter your command line in C++ literal correctly for this.
So long as you use the
|
you will have to go viacmd /c ...
as I have shown. If you want to avoid that run the command and search its output forPID:
from your Qt program yourself, as I said earlier and @Christian-Ehrlicher suggests you do. In fact looking at the output of just a plaintasklist
with no arguments at all you can just as simply look through each of its lines and pick out the one which isx32dbg.exe <pid-number>
. -
@SGaist
@JonB
@Christian-Ehrlicher
thanks all
i found the solutionbool 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 ; }