Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

CMD write/read issue



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


  • Lifetime Qt Champion

    @Cobra91151

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



  • @mrjj

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


  • Lifetime Qt Champion

    @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


  • Moderators

    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.


  • Lifetime Qt Champion

    He can then read the changed prompt?



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



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


  • Moderators

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



  • @mrjj

    Strange, it works on the test project:

    0_1561829769462_2019-06-29_203537.png

    I will investigate it and reply later.



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



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


Log in to reply