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. result of QProcess and Windows cmd is not the same - why?
Qt 6.11 is out! See what's new in the release blog

result of QProcess and Windows cmd is not the same - why?

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 5 Posters 1.2k Views 2 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.
  • LeoCL Offline
    LeoCL Offline
    LeoC
    wrote on last edited by
    #1

    Hey,

    I need a hint where to look for my current issue. Basically I want to use an external cmd tool within my program. The purpose of this external tool is to convert measurement files into another format. Therefore it takes three input parameters like inputfile, signalfile and outputfile.

    Therefore I did something like:

    QFile fMeasurementFile("measurementfile.zip");
    QFile fSignalFile("signalfile");
    QFile fOutput("output.csv");
    QFile fMyTool("mytool.exe");
    
    QStringList arguments;
    arguments << "inputfile=" + fMeasurementFile.fileName() + " signalfile" + fSignalFile.fileName() + " outputfile=" + fOutput.fileName());
    
    pProcess->start(fMyTool.fileName(), arguments);
    
    qDebug() << endl <<  "=====";
    qDebug() << pProcess->program() << pProcess->arguments();
    qDebug() << "=====" << endl;
    

    And I can also read the output:

    void MainWindow::readOutput() {
        QByteArray bArray = pProcess->readAllStandardOutput().simplified().trimmed();
        qDebug() << bArray;
    }
    

    My program itself works. Also the external program gets executed and starts working but terminates then. I first thought it might be a typing mistake somewhere but when I copy the result of pProcess->arguments() to a cmd window the execution works absolutely fine. So there is no issue with the filepaths or so.
    Then I tried executing the external programm without QProcess:

    
    std::array<char, 128> buffer;
    std::string result;
    std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(fMyTool.fileName().toUtf8().constData() + arguments.at(0).toUtf8(), "r"), pclose);
    if (!pipe) {
       throw std::runtime_error("popen() failed!");
    }
    while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
        result += buffer.data();
    }
    qDebug() << QString::fromStdString(result);
    

    ... and it works absolutely fine. The external program works as expected.

    So I wonder what is different when using QProcess.

    Using QProcess the external program terminates with "illegal parameter" and I haven't noticed it first but the output of readOutput(); is:

    blablabla ... ILLEGAL PARAMETER inputfile=measurementfile.zip signalfile=signalfile outputfile=output.csv\x07
    

    ... what is the meaning of "\x07" here? That is the only difference I can figure out in all my test runs. And most important: how to remove it? For me it seems that this is the illegal parameter?
    I hope you can give me a hint here despite I cannot provide the full code or even the external program.

    Thanks!

    KroMignonK JonBJ 2 Replies Last reply
    0
    • LeoCL LeoC

      Hey,

      I need a hint where to look for my current issue. Basically I want to use an external cmd tool within my program. The purpose of this external tool is to convert measurement files into another format. Therefore it takes three input parameters like inputfile, signalfile and outputfile.

      Therefore I did something like:

      QFile fMeasurementFile("measurementfile.zip");
      QFile fSignalFile("signalfile");
      QFile fOutput("output.csv");
      QFile fMyTool("mytool.exe");
      
      QStringList arguments;
      arguments << "inputfile=" + fMeasurementFile.fileName() + " signalfile" + fSignalFile.fileName() + " outputfile=" + fOutput.fileName());
      
      pProcess->start(fMyTool.fileName(), arguments);
      
      qDebug() << endl <<  "=====";
      qDebug() << pProcess->program() << pProcess->arguments();
      qDebug() << "=====" << endl;
      

      And I can also read the output:

      void MainWindow::readOutput() {
          QByteArray bArray = pProcess->readAllStandardOutput().simplified().trimmed();
          qDebug() << bArray;
      }
      

      My program itself works. Also the external program gets executed and starts working but terminates then. I first thought it might be a typing mistake somewhere but when I copy the result of pProcess->arguments() to a cmd window the execution works absolutely fine. So there is no issue with the filepaths or so.
      Then I tried executing the external programm without QProcess:

      
      std::array<char, 128> buffer;
      std::string result;
      std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(fMyTool.fileName().toUtf8().constData() + arguments.at(0).toUtf8(), "r"), pclose);
      if (!pipe) {
         throw std::runtime_error("popen() failed!");
      }
      while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
          result += buffer.data();
      }
      qDebug() << QString::fromStdString(result);
      

      ... and it works absolutely fine. The external program works as expected.

      So I wonder what is different when using QProcess.

      Using QProcess the external program terminates with "illegal parameter" and I haven't noticed it first but the output of readOutput(); is:

      blablabla ... ILLEGAL PARAMETER inputfile=measurementfile.zip signalfile=signalfile outputfile=output.csv\x07
      

      ... what is the meaning of "\x07" here? That is the only difference I can figure out in all my test runs. And most important: how to remove it? For me it seems that this is the illegal parameter?
      I hope you can give me a hint here despite I cannot provide the full code or even the external program.

      Thanks!

      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by
      #2

      @LeoC said in result of QProcess and Windows cmd is not the same - why?:

      QStringList arguments;
      arguments << "inputfile=" + fMeasurementFile.fileName() + " signalfile" + fSignalFile.fileName() + " outputfile=" + fOutput.fileName());

      This is wrong, should be:

      QStringList arguments;
      arguments << "inputfile=" + fMeasurementFile.fileName() 
                << "signalfile=" + fSignalFile.fileName() 
                << "outputfile=" + fOutput.fileName());
      

      Each argument must be an item in the string list.

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      1 Reply Last reply
      5
      • LeoCL Offline
        LeoCL Offline
        LeoC
        wrote on last edited by
        #3

        It was the intention of writing all in one line because doing it like you proposed sets a comma after each argument which is not what my external program expects.

        jsulmJ KroMignonK 2 Replies Last reply
        0
        • LeoCL LeoC

          It was the intention of writing all in one line because doing it like you proposed sets a comma after each argument which is not what my external program expects.

          jsulmJ Online
          jsulmJ Online
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @LeoC How should coma come into the parameter list?! @KroMignon is right, you can also check QProcess documentation to see how parameters are passed to the process.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • LeoCL LeoC

            It was the intention of writing all in one line because doing it like you proposed sets a comma after each argument which is not what my external program expects.

            KroMignonK Offline
            KroMignonK Offline
            KroMignon
            wrote on last edited by KroMignon
            #5

            @LeoC said in result of QProcess and Windows cmd is not the same - why?:

            It was the intention of writing all in one line because doing it like you proposed sets a comma after each argument which is not what my external program expects.

            Can you explain why there will be comas in command?
            QProcess expect a list of parameters, this is because QProcess has to managed the "special characters" in arguments like spaces.
            Otherwise why use QStringList and not QString as parameters type?

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            1 Reply Last reply
            0
            • LeoCL LeoC

              Hey,

              I need a hint where to look for my current issue. Basically I want to use an external cmd tool within my program. The purpose of this external tool is to convert measurement files into another format. Therefore it takes three input parameters like inputfile, signalfile and outputfile.

              Therefore I did something like:

              QFile fMeasurementFile("measurementfile.zip");
              QFile fSignalFile("signalfile");
              QFile fOutput("output.csv");
              QFile fMyTool("mytool.exe");
              
              QStringList arguments;
              arguments << "inputfile=" + fMeasurementFile.fileName() + " signalfile" + fSignalFile.fileName() + " outputfile=" + fOutput.fileName());
              
              pProcess->start(fMyTool.fileName(), arguments);
              
              qDebug() << endl <<  "=====";
              qDebug() << pProcess->program() << pProcess->arguments();
              qDebug() << "=====" << endl;
              

              And I can also read the output:

              void MainWindow::readOutput() {
                  QByteArray bArray = pProcess->readAllStandardOutput().simplified().trimmed();
                  qDebug() << bArray;
              }
              

              My program itself works. Also the external program gets executed and starts working but terminates then. I first thought it might be a typing mistake somewhere but when I copy the result of pProcess->arguments() to a cmd window the execution works absolutely fine. So there is no issue with the filepaths or so.
              Then I tried executing the external programm without QProcess:

              
              std::array<char, 128> buffer;
              std::string result;
              std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(fMyTool.fileName().toUtf8().constData() + arguments.at(0).toUtf8(), "r"), pclose);
              if (!pipe) {
                 throw std::runtime_error("popen() failed!");
              }
              while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
                  result += buffer.data();
              }
              qDebug() << QString::fromStdString(result);
              

              ... and it works absolutely fine. The external program works as expected.

              So I wonder what is different when using QProcess.

              Using QProcess the external program terminates with "illegal parameter" and I haven't noticed it first but the output of readOutput(); is:

              blablabla ... ILLEGAL PARAMETER inputfile=measurementfile.zip signalfile=signalfile outputfile=output.csv\x07
              

              ... what is the meaning of "\x07" here? That is the only difference I can figure out in all my test runs. And most important: how to remove it? For me it seems that this is the illegal parameter?
              I hope you can give me a hint here despite I cannot provide the full code or even the external program.

              Thanks!

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @LeoC said in result of QProcess and Windows cmd is not the same - why?:

              ... what is the meaning of "\x07" here?

              Just for this bit. (For the rest follow what the others are telling you.) Rather weirdly, ASCII character 7 is the "bell" sound :) I'm pretty sure that if you ran whatever bad command from a terminal it would output what you see as the error message and you would hear a "beep", which it's outputting to alarm you! :)

              1 Reply Last reply
              2
              • LeoCL Offline
                LeoCL Offline
                LeoC
                wrote on last edited by
                #7

                ooohm, maybe I misunderstood the output of pProcess->arguments() because writing something like this:

                arguments << "outputfile=" + fMeasurementFile.fileName();
                arguments << "signalfile=" + fSignalFile.fileName();
                arguments << "outputfile=" + fOutput.fileName();
                

                gives me

                "outputfile=measurement.zip", "signalfile=signalfile", "outputfile=output1338.csv"
                

                as an output. My assumption was that I can copy exactly this result to the windows cmd and it should work. But it doesn't.
                pProcess->arguments() seems to be rather a list of all the arguments.

                However, I adapted my code but I still get this "\x07" at the end of my last paremter...

                KroMignonK JonBJ 2 Replies Last reply
                0
                • LeoCL LeoC

                  ooohm, maybe I misunderstood the output of pProcess->arguments() because writing something like this:

                  arguments << "outputfile=" + fMeasurementFile.fileName();
                  arguments << "signalfile=" + fSignalFile.fileName();
                  arguments << "outputfile=" + fOutput.fileName();
                  

                  gives me

                  "outputfile=measurement.zip", "signalfile=signalfile", "outputfile=output1338.csv"
                  

                  as an output. My assumption was that I can copy exactly this result to the windows cmd and it should work. But it doesn't.
                  pProcess->arguments() seems to be rather a list of all the arguments.

                  However, I adapted my code but I still get this "\x07" at the end of my last paremter...

                  KroMignonK Offline
                  KroMignonK Offline
                  KroMignon
                  wrote on last edited by
                  #8

                  @LeoC said in result of QProcess and Windows cmd is not the same - why?:

                  gives me
                  "outputfile=measurement.zip", "signalfile=signalfile", "outputfile=output1338.csv"

                  as an output. My assumption was that I can copy exactly this result to the windows cmd and it should work. But it doesn't.
                  pProcess->arguments() seems to be rather a list of all the arguments.

                  Please do not mixup what different classes does with a QStringList.
                  I suppose that outputfile=measurement.zip", "signalfile=signalfile", "outputfile=output1338.csv" was the output generated with qDebug() << arguments;. This does only show the content of the QStringList, but there is no link with QProcess.

                  And what do you mean with "\x07" at the end of the last parameter?
                  It is the output of the QProcess called application?

                  As @JonB already wrote before '\x07' is the ASCII code for BEL, which is often use to make a sound in console application.
                  I guess this is added by the application to alert user about error.

                  Are you sure you are using the rigth parameter format?
                  Most of the program I use needs a dash (or double dash) before parameters, maybe it is the case for your application?

                  It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                  1 Reply Last reply
                  1
                  • LeoCL LeoC

                    ooohm, maybe I misunderstood the output of pProcess->arguments() because writing something like this:

                    arguments << "outputfile=" + fMeasurementFile.fileName();
                    arguments << "signalfile=" + fSignalFile.fileName();
                    arguments << "outputfile=" + fOutput.fileName();
                    

                    gives me

                    "outputfile=measurement.zip", "signalfile=signalfile", "outputfile=output1338.csv"
                    

                    as an output. My assumption was that I can copy exactly this result to the windows cmd and it should work. But it doesn't.
                    pProcess->arguments() seems to be rather a list of all the arguments.

                    However, I adapted my code but I still get this "\x07" at the end of my last paremter...

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #9

                    @LeoC said in result of QProcess and Windows cmd is not the same - why?:

                    However, I adapted my code but I still get this "\x07" at the end of my last paremter...

                    That character is not a parameter to your command, is it? It's a character tagged onto the end of the output "ILLEGAL PARAMETER ...." mesage, isn't it? Please don't call that a "parameter" (they are used as arguments/options/parameters TO the program), that just confuses. Since you don't care about it, simply remove or ignore that character when looking at what you get back.

                    What exactly do you type into a Command Prompt to run your external command to make it work from there? Please copy & paste an exact, working command line.

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mchinand
                      wrote on last edited by
                      #10

                      @LeoC said in result of QProcess and Windows cmd is not the same - why?:

                      QStringList arguments;
                      arguments << "inputfile=" + fMeasurementFile.fileName() + " signalfile" + fSignalFile.fileName() + " outputfile=" + fOutput.fileName());

                      Create a stringlist separating out your arguments as others have said, but your original code was also missing an equal sign after 'signalfile'.

                      1 Reply Last reply
                      0
                      • LeoCL Offline
                        LeoCL Offline
                        LeoC
                        wrote on last edited by LeoC
                        #11

                        First of all I would like to thank you for all the inputs.
                        Let's try to sort everything out.

                        This is the command which works fine when using the windows cmd

                        C:\myTool.exe inputfile=C:/mytmp/measurement.zip;signalfile=C:/mytmp/signals outputfile=C:/mytemp/output.csv
                        

                        The cmd then gives me the following output:

                        UNZIP C:\mytmp\measurement.zip... OK c:\users\...\appdata\...\measurement.unzipped
                        READING c:\users\...\appdata\...\measurement.unzipped ... OK
                        PROCESSING FILE c:\mytemp\signallist ... OK
                        WRITING C:\mytemp\output.csv ... OK
                        FINISHED
                        ...
                        

                        After adjusting my argument list the result of pProcess->arguments is:

                        "C:\myTool.exe" ("inputfile=C:/mytmp/measurement.zip", ";signalfile=C:/mytmp/output.csv", "outputfile=C:/mytmp/output.csv")
                        

                        Since I connected the signal readyReadStandardOutput() I can see the result which is:

                        ILLEGAL PARAMETER ;signalfile=C:/mytmp/output.csv\x07"
                        

                        And now the question is: where is the difference between using the windows cmd and QProcess since cmd works fine and QProcess does not.

                        edit: After reading my own post again I guess it should rather look like this... ?

                        "C:\myTool.exe" ("inputfile=C:/mytmp/measurement.zip;signalfile=C:/mytmp/output.csv", "outputfile=C:/mytmp/output.csv")
                        
                        KroMignonK 1 Reply Last reply
                        0
                        • LeoCL LeoC

                          First of all I would like to thank you for all the inputs.
                          Let's try to sort everything out.

                          This is the command which works fine when using the windows cmd

                          C:\myTool.exe inputfile=C:/mytmp/measurement.zip;signalfile=C:/mytmp/signals outputfile=C:/mytemp/output.csv
                          

                          The cmd then gives me the following output:

                          UNZIP C:\mytmp\measurement.zip... OK c:\users\...\appdata\...\measurement.unzipped
                          READING c:\users\...\appdata\...\measurement.unzipped ... OK
                          PROCESSING FILE c:\mytemp\signallist ... OK
                          WRITING C:\mytemp\output.csv ... OK
                          FINISHED
                          ...
                          

                          After adjusting my argument list the result of pProcess->arguments is:

                          "C:\myTool.exe" ("inputfile=C:/mytmp/measurement.zip", ";signalfile=C:/mytmp/output.csv", "outputfile=C:/mytmp/output.csv")
                          

                          Since I connected the signal readyReadStandardOutput() I can see the result which is:

                          ILLEGAL PARAMETER ;signalfile=C:/mytmp/output.csv\x07"
                          

                          And now the question is: where is the difference between using the windows cmd and QProcess since cmd works fine and QProcess does not.

                          edit: After reading my own post again I guess it should rather look like this... ?

                          "C:\myTool.exe" ("inputfile=C:/mytmp/measurement.zip;signalfile=C:/mytmp/output.csv", "outputfile=C:/mytmp/output.csv")
                          
                          KroMignonK Offline
                          KroMignonK Offline
                          KroMignon
                          wrote on last edited by KroMignon
                          #12

                          @LeoC said in result of QProcess and Windows cmd is not the same - why?:

                          This is the command which works fine when using the windows cmd

                          C:\myTool.exe inputfile=C:/mytmp/measurement.zip;signalfile=C:/mytmp/signals outputfile=C:/mytemp/output.csv
                          

                          If this is what you want to have, you should do it like this:

                          QStringList arguments;
                          arguments << "inputfile=" + fMeasurementFile.fileName()  + ";signalfile=" + fSignalFile.fileName() 
                                    << "outputfile=" + fOutput.fileName());
                          

                          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                          1 Reply Last reply
                          2
                          • LeoCL Offline
                            LeoCL Offline
                            LeoC
                            wrote on last edited by
                            #13

                            Sorry for not comming back earlier. I was forced to do some other things in between...
                            But it seems that doing something different for 3 weeks was the right thing to do.

                            I opened the project again, checked every line , adjusted it a bit (including the proposal of @KroMignon ) and now... it works :)

                            Thanks to everyone involved here.

                            1 Reply Last reply
                            0

                            • Login

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