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. Writing to the standard input of a process
Forum Updated to NodeBB v4.3 + New Features

Writing to the standard input of a process

Scheduled Pinned Locked Moved General and Desktop
23 Posts 3 Posters 15.8k 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.
  • A Offline
    A Offline
    A1exander_Z
    wrote on last edited by
    #4

    It does wait for an input from stdin. In the simplest case, it has only two following lines in its Main method:

    string command = Console.ReadLine();
    Console.WriteLine(command);

    When run from the console it works as expected, but when I run it from my Qt application using QProcess, it waits indefinitely. Passing command line parameters works.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #5

      Then can you show the code where you use QProcess ?

      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
      • A Offline
        A Offline
        A1exander_Z
        wrote on last edited by
        #6

        Process creation ('QProcess* process' declaration is in a header file):

        @process = new QProcess();
        connect(process, SIGNAL(finished(int, QProcess::ExitStatus)),
        this, SLOT(finished(int, QProcess::ExitStatus)));
        connect(process, SIGNAL(started()), this, SLOT(started()));
        process->setProgram("test.exe");
        process->open();@

        Slots:

        @void MainWindow::started()
        {
        qDebug() << process->write("test\n");
        }@

        @void MainWindow::finished(int exitCode, Process::ExitStatus status)
        {
        qDebug() << "Finished";
        qDebug() << exitCode;
        }@

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #7

          You are using QProcess in an unusual way (at least to me)

          Since its a command line application, I would rather use the synchronous approach described "here":http://qt-project.org/doc/qt-5/qprocess.html#synchronous-process-api

          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
          1
          • A Offline
            A Offline
            A1exander_Z
            wrote on last edited by
            #8

            Thank you, it helped - at least now I can get the response. I tried the following code:

            @process = new QProcess();
            connect(process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(finished(int, QProcess::ExitStatus)));
            process->setProgram("test.exe");
            process->start();
            process->write("test\n");
            process->waitForFinished();
            @

            Slot:

            @void MainWindow::finished(int exitCode, QProcess::ExitStatus status)
            {
            qDebug() << "Finished";
            qDebug() << exitCode;
            qDebug() << process->readAllStandardOutput();
            }@

            The code in the 'finished' slot is executed and prints the output produced by the called program (echoed "test"). The actual difference seems to be in the 'process->waitForFinished()' call. If I remove it, the C# program does not terminate (the created process remains in the list of processes).

            The problem is than I need to execute it asynchronously from my GUI application, with a possibility for user to terminate it, so the GUI should remain responsive.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #9

              Does it also happen if you add waitForStarted before writing to it ?

              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
              • A Offline
                A Offline
                A1exander_Z
                wrote on last edited by
                #10

                Yes, adding process->waitForStarted() before process->write("test\n") does not change anything, the process does not terminate if there is no process->waitForFinished() line in the end. Closing (or not closing) the write channel has no effect in all cases.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #11

                  I just forgot one basic question… What version of Qt are you using on what version of Windows ?

                  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
                  • A Offline
                    A Offline
                    A1exander_Z
                    wrote on last edited by
                    #12

                    Qt 5.2.0 on Windows 7 Ultimate 64 bit, Service Pack 1.

                    I have tried to build my program using two compilers, MinGW 32 bit and MSVC2012 64 bit. The behavior is the same in both cases.

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #13

                      Does it also happen if you call a standard cmd command ?

                      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
                      • A Offline
                        A Offline
                        A1exander_Z
                        wrote on last edited by
                        #14

                        I am not sure how to call a standard cmd command from a Qt application (I have tried process->setProgram("dir"), it runs but produces no output to stdout), but I did further research.

                        I wrote a simple program in C++ and compiled it with MinGW:

                        @#include <iostream>
                        #include <string>

                        int main(int argc, char *argv[])
                        {
                        std::string s;
                        std::cin >> s;
                        std::cout << s << std::endl;
                        }@

                        It works both with and without process->waitForFinished() call, my Qt application can write to its standard input and retreive echoed string from its output. Writing from a slot connected to the 'QProsess::started' signal also works. It seems that the problem is with my C# program after all.

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #15

                          Did you try with a similar minimal application written in C# ?

                          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
                          • A Offline
                            A Offline
                            A1exander_Z
                            wrote on last edited by
                            #16

                            Yes, I have tried this:

                            @class Program
                            {
                            static int Main(string[] args)
                            {
                            string s;
                            s = Console.ReadLine();
                            Console.WriteLine(s);
                            return 0;
                            }
                            }@

                            Works if I place waitForFinished() call after writing to the standard input of the process, but never terminates without it. Changing configuration of the C# program from Debug to Release in Visual Studio does not help.

                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #17

                              Maybe a silly question (I don't know much about C#) but aren't you missing a "\n" here ?

                              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
                              • A Offline
                                A Offline
                                A1exander_Z
                                wrote on last edited by
                                #18

                                No, Console.WriteLine() method inserts it automatically. And with waitForFinished() call it works even when I change Console.WriteLine() to Console.Write(), which does not insert a newline symbol.

                                1 Reply Last reply
                                0
                                • SGaistS Offline
                                  SGaistS Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #19

                                  Could there be some subtle difference on how the C# runtime uses stdin/stdout ?

                                  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
                                  • A Offline
                                    A Offline
                                    A1exander_Z
                                    wrote on last edited by
                                    #20

                                    Yes, it seems that there is some difference, but I am not a C# expert and I have no idea what it is. I will try a simple program compiled using Mono but for now, I think it is easier to send all data as command line parameters.

                                    1 Reply Last reply
                                    0
                                    • SGaistS Offline
                                      SGaistS Offline
                                      SGaist
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #21

                                      As long as it's clean and works, do what is best.

                                      Happy Coding !

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

                                      M 1 Reply Last reply
                                      0
                                      • SGaistS SGaist

                                        As long as it's clean and works, do what is best.

                                        Happy Coding !

                                        M Offline
                                        M Offline
                                        mhmdkanj
                                        wrote on last edited by
                                        #22

                                        @SGaist I am currently having the same issue for C# executables and writing to their standard input. Assuming that I cannot change the C# code, how would I fix this in Qt in case you can enlighten me after a few years.. Many thanks!

                                        1 Reply Last reply
                                        0
                                        • SGaistS Offline
                                          SGaistS Offline
                                          SGaist
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #23

                                          hi,

                                          Did you apply the same technique as @A1exander_Z did ?

                                          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

                                          • Login

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