Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. add double qoutes to qstring or qprocess
Forum Updated to NodeBB v4.3 + New Features

add double qoutes to qstring or qprocess

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 765 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fadu
    wrote on 29 May 2022, 20:22 last edited by fadu
    #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

    1 Reply Last reply
    0
    • F Offline
      F Offline
      fadu
      wrote on 30 May 2022, 15:13 last edited by
      #8

      @SGaist
      @JonB
      @Christian-Ehrlicher
      thanks all
      i found the solution

      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 ;
      }
      
      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 29 May 2022, 20:49 last edited by
        #2

        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.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • F Offline
          F Offline
          fadu
          wrote on 29 May 2022, 21:04 last edited by fadu
          #3

          @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

          J 1 Reply Last reply 29 May 2022, 22:29
          0
          • F fadu
            29 May 2022, 21:04

            @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

            J Offline
            J Offline
            JonB
            wrote on 29 May 2022, 22:29 last edited by JonB
            #4

            @fadu
            You should also p.readAllStandardError(); for any error message. And you cannot do this immediately after p.start(), you need p.waitForFinished().

            Your command line contains a | redirection symbol. So you need to send the whole command to cmd (unless you do the direction yourself and two QProcesses). 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 searching readAllStandardOutput() for the string yourself, if you chose.

            1 Reply Last reply
            0
            • F Offline
              F Offline
              fadu
              wrote on 29 May 2022, 22:48 last edited by fadu
              #5

              @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 \

              C J 2 Replies Last reply 30 May 2022, 04:53
              0
              • F fadu
                29 May 2022, 22:48

                @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 \

                C Offline
                C Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 30 May 2022, 04:53 last edited by
                #6

                @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.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                1 Reply Last reply
                0
                • F fadu
                  29 May 2022, 22:48

                  @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 \

                  J Offline
                  J Offline
                  JonB
                  wrote on 30 May 2022, 07:18 last edited by JonB
                  #7

                  @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 at tasklist /? and you are not using /FI correctly. It needs to be

                  tasklist /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 via cmd /c ... as I have shown. If you want to avoid that run the command and search its output for PID: from your Qt program yourself, as I said earlier and @Christian-Ehrlicher suggests you do. In fact looking at the output of just a plain tasklist with no arguments at all you can just as simply look through each of its lines and pick out the one which is x32dbg.exe <pid-number>.

                  1 Reply Last reply
                  1
                  • F Offline
                    F Offline
                    fadu
                    wrote on 30 May 2022, 15:13 last edited by
                    #8

                    @SGaist
                    @JonB
                    @Christian-Ehrlicher
                    thanks all
                    i found the solution

                    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 ;
                    }
                    
                    1 Reply Last reply
                    0

                    3/8

                    29 May 2022, 21:04

                    topic:navigator.unread, 5
                    • Login

                    • Login or register to search.
                    3 out of 8
                    • First post
                      3/8
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved