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. CMD write/read issue

CMD write/read issue

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 3 Posters 1.1k 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.
  • Cobra91151C Offline
    Cobra91151C Offline
    Cobra91151
    wrote on last edited by Cobra91151
    #1

    Hi! I want to write/read CMD data from GUI application.

    For example:

        QProcess *process = new QProcess();
        connect(process , &QProcess::started, [this]() {
           qDebug() << "Started!";
        });
        connect(process, &QProcess::readyRead, [this]() {
            QTextStream outputStream(process->readAllStandardOutput());
            outputStream.setCodec("IBM 866");
            ui->textBrowser->append(outputStream.readAll());
        });
        connect(process , &QProcess::readyReadStandardOutput, [this]() {
            QTextStream outputStream(process->readAllStandardOutput());
            outputStream.setCodec("IBM 866");
            ui->textBrowser->append(outputStream.readAll());
        });
        connect(process, &QProcess::readyReadStandardError, [this]() {
            QTextStream errorStream(process->readAllStandardError());
            errorStream.setCodec("IBM 866");
            ui->textBrowser->append(errorStream.readAll());
        });
        connect(process, qOverload<int, QProcess::ExitStatus>(&QProcess::finished), [this](int exitCode, QProcess::ExitStatus exitStatus) {
            ui->textBrowser->append(QString::number(exitCode) + " " + QString::number(exitStatus));
            process->deleteLater();
        });
        process->start("cmd.exe", "/K cd %USERPROFILE%");
    

    I write to CMD:

    process->write(QString("cd ..").toUtf8());
    

    I checked that it writes: "cd .." 5 bytes but nothing is in output:
    0_1561825941105_2019-06-29_193140.png

    Any ideas? Thanks in advance.

    mrjjM 1 Reply Last reply
    0
    • Cobra91151C Cobra91151

      Hi! I want to write/read CMD data from GUI application.

      For example:

          QProcess *process = new QProcess();
          connect(process , &QProcess::started, [this]() {
             qDebug() << "Started!";
          });
          connect(process, &QProcess::readyRead, [this]() {
              QTextStream outputStream(process->readAllStandardOutput());
              outputStream.setCodec("IBM 866");
              ui->textBrowser->append(outputStream.readAll());
          });
          connect(process , &QProcess::readyReadStandardOutput, [this]() {
              QTextStream outputStream(process->readAllStandardOutput());
              outputStream.setCodec("IBM 866");
              ui->textBrowser->append(outputStream.readAll());
          });
          connect(process, &QProcess::readyReadStandardError, [this]() {
              QTextStream errorStream(process->readAllStandardError());
              errorStream.setCodec("IBM 866");
              ui->textBrowser->append(errorStream.readAll());
          });
          connect(process, qOverload<int, QProcess::ExitStatus>(&QProcess::finished), [this](int exitCode, QProcess::ExitStatus exitStatus) {
              ui->textBrowser->append(QString::number(exitCode) + " " + QString::number(exitStatus));
              process->deleteLater();
          });
          process->start("cmd.exe", "/K cd %USERPROFILE%");
      

      I write to CMD:

      process->write(QString("cd ..").toUtf8());
      

      I checked that it writes: "cd .." 5 bytes but nothing is in output:
      0_1561825941105_2019-06-29_193140.png

      Any ideas? Thanks in advance.

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Cobra91151

      Hi
      But the cd command do not give any output ?
      so what do you expect that it should return ?

      Cobra91151C 1 Reply Last reply
      0
      • mrjjM mrjj

        @Cobra91151

        Hi
        But the cd command do not give any output ?
        so what do you expect that it should return ?

        Cobra91151C Offline
        Cobra91151C Offline
        Cobra91151
        wrote on last edited by
        #3

        @mrjj

        When I write cd .. it should return C:\Users>.

        mrjjM 1 Reply Last reply
        0
        • Cobra91151C Cobra91151

          @mrjj

          When I write cd .. it should return C:\Users>.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @Cobra91151
          Hi
          I see your point, but i am not sure it works that way.
          The change in the prompt is not directly from the cd command.
          it does not return the new path or anything so i dont think there is anything to read.

          needed \n

          Cobra91151C 1 Reply Last reply
          1
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by
            #5

            You're sending input as if you typed it in the command line window, therefore you need to pass "enter" to execute the statement i.e. cd ..\n.

            Cobra91151C 1 Reply Last reply
            1
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              He can then read the changed prompt?

              Chris KawaC Cobra91151C 2 Replies Last reply
              0
              • mrjjM mrjj

                @Cobra91151
                Hi
                I see your point, but i am not sure it works that way.
                The change in the prompt is not directly from the cd command.
                it does not return the new path or anything so i dont think there is anything to read.

                needed \n

                Cobra91151C Offline
                Cobra91151C Offline
                Cobra91151
                wrote on last edited by
                #7

                @mrjj

                Yes, you right. I just provided the wrong example. But what about this, when update integrated using .cab file (dism.exe) it ask to restart by pressing Y/N. So how to write to stdin appropriate command using QProcess? Thanks.

                1 Reply Last reply
                0
                • Chris KawaC Chris Kawa

                  You're sending input as if you typed it in the command line window, therefore you need to pass "enter" to execute the statement i.e. cd ..\n.

                  Cobra91151C Offline
                  Cobra91151C Offline
                  Cobra91151
                  wrote on last edited by Cobra91151
                  #8

                  @Chris-Kawa said in CMD write/read issue:

                  cd ..\n

                  Yes, I know that. I tried it but the output is the same.

                  I will create the small example to reproduce it.

                  1 Reply Last reply
                  0
                  • mrjjM mrjj

                    He can then read the changed prompt?

                    Chris KawaC Offline
                    Chris KawaC Offline
                    Chris Kawa
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @mrjj said:

                    He can then read the changed prompt?

                    Yes, it will be available for reading in those readyRead... signals.

                    @Cobra91151

                    So how to write to stdin appropriate command using QProcess?

                    Just as you would with cd i.e. y\n. But first check if that tool doesn't have some sort of switch to automatically accept prompts. Some utilities have a switch line /quiet, /silent, /autoconfirm, /unattended etc. Maybe that one has too.

                    Yes, I know that. I tried it but the output is the same.

                    I checked. It works. You must have something different in your actual code than shown here then.

                    Cobra91151C 1 Reply Last reply
                    3
                    • mrjjM mrjj

                      He can then read the changed prompt?

                      Cobra91151C Offline
                      Cobra91151C Offline
                      Cobra91151
                      wrote on last edited by
                      #10

                      @mrjj

                      Strange, it works on the test project:

                      0_1561829769462_2019-06-29_203537.png

                      I will investigate it and reply later.

                      1 Reply Last reply
                      0
                      • Chris KawaC Chris Kawa

                        @mrjj said:

                        He can then read the changed prompt?

                        Yes, it will be available for reading in those readyRead... signals.

                        @Cobra91151

                        So how to write to stdin appropriate command using QProcess?

                        Just as you would with cd i.e. y\n. But first check if that tool doesn't have some sort of switch to automatically accept prompts. Some utilities have a switch line /quiet, /silent, /autoconfirm, /unattended etc. Maybe that one has too.

                        Yes, I know that. I tried it but the output is the same.

                        I checked. It works. You must have something different in your actual code than shown here then.

                        Cobra91151C Offline
                        Cobra91151C Offline
                        Cobra91151
                        wrote on last edited by Cobra91151
                        #11

                        @Chris-Kawa

                        Yes, you are right, dism has /NoRestart parameter (Suppresses automatic reboots and reboot prompts.). As for the current write issue, I am still working on it.

                        1 Reply Last reply
                        2
                        • Cobra91151C Offline
                          Cobra91151C Offline
                          Cobra91151
                          wrote on last edited by Cobra91151
                          #12

                          Ok. I found the problem: process->closeReadChannel(QProcess::StandardOutput); So, it closes the read channel and that's why can't output anything. My mistake. I am working on website/app dev simultaneously :) The issue is resolved.

                          1 Reply Last reply
                          2

                          • Login

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