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. How to write data to the c# stdin via Qprocess ?
Forum Updated to NodeBB v4.3 + New Features

How to write data to the c# stdin via Qprocess ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 3 Posters 1.3k Views
  • 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.
  • J Offline
    J Offline
    junzhe fan
    wrote on 6 Oct 2021, 14:25 last edited by
    #1

    Hello.
    I am having trouble writing data to a c# console application( spawned using QProcess as a sub-process of my Qt application), the stdout works fine(i.e. I can read data from my c# sub-process, but I can't write data to it or it might be c# application somehow not picking up the data that has been written? )
    It seems some people were having the same issue, but none of they post had been solved

    • Qt/C++ - Can't write to stdin of QProcess running a C# application

    • Writing to standard input of a C# based QProcess

    A very minimal approach I have tried :
    Qt code:

    qdebug()<<process.write("Hi");//return 2 
    process.waitForBytesWritten() //block my GUI thred endlessly 
    

    c# code simply :

    console.writeline("C#") // received by my Qt app via StdOUT succeessfully; 
    string data = console.readline(); // block- never revice the data I sent from my Qt app 
    console.writeline(data);  
    
    J J 2 Replies Last reply 6 Oct 2021, 14:30
    0
    • J junzhe fan
      6 Oct 2021, 14:25

      Hello.
      I am having trouble writing data to a c# console application( spawned using QProcess as a sub-process of my Qt application), the stdout works fine(i.e. I can read data from my c# sub-process, but I can't write data to it or it might be c# application somehow not picking up the data that has been written? )
      It seems some people were having the same issue, but none of they post had been solved

      • Qt/C++ - Can't write to stdin of QProcess running a C# application

      • Writing to standard input of a C# based QProcess

      A very minimal approach I have tried :
      Qt code:

      qdebug()<<process.write("Hi");//return 2 
      process.waitForBytesWritten() //block my GUI thred endlessly 
      

      c# code simply :

      console.writeline("C#") // received by my Qt app via StdOUT succeessfully; 
      string data = console.readline(); // block- never revice the data I sent from my Qt app 
      console.writeline(data);  
      
      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 6 Oct 2021, 14:30 last edited by
      #2

      @junzhe-fan Did you wait for the process to start? Please post more of your code, including the starting of the process. You also should add error handling, if not yet done, to see whether there are any errors.

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

      1 Reply Last reply
      0
      • J Offline
        J Offline
        junzhe fan
        wrote on 6 Oct 2021, 14:36 last edited by junzhe fan 10 Jun 2021, 14:37
        #3
           process.setWorkingDirectory("E:/new Qt/CC/PhoneHandler/PhoneHandler/bin/x64/Debug");
            auto program = QString("cmd.exe");
            auto Arguments = QStringList{
                "/C","PhoneHandler.exe"
            };
            process.start(program, Arguments);
            qDebug()<<process.state(); //return running 
        

        @jsulm
        I am running my app on windows10 and building with MSVC compiler, My c# app is basically a console application

        J 1 Reply Last reply 6 Oct 2021, 14:39
        0
        • J junzhe fan
          6 Oct 2021, 14:36
             process.setWorkingDirectory("E:/new Qt/CC/PhoneHandler/PhoneHandler/bin/x64/Debug");
              auto program = QString("cmd.exe");
              auto Arguments = QStringList{
                  "/C","PhoneHandler.exe"
              };
              process.start(program, Arguments);
              qDebug()<<process.state(); //return running 
          

          @jsulm
          I am running my app on windows10 and building with MSVC compiler, My c# app is basically a console application

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 6 Oct 2021, 14:39 last edited by
          #4

          @junzhe-fan said in How to write data to the c# stdin via Qprocess ?:

          auto program = QString("cmd.exe");

          Why do you use cmd.exe? Try to start PhoneHandler.exe directly.

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

          1 Reply Last reply
          1
          • J Offline
            J Offline
            junzhe fan
            wrote on 6 Oct 2021, 14:52 last edited by junzhe fan 10 Jun 2021, 14:53
            #5

            @jsulm I am not sure, I tried that before, but it seems the c# console application can only be triggered by cmd.exe interpreter ?
            if I do this:

                process.setWorkingDirectory("E:/new Qt/CC/PhoneHandler/PhoneHandler/bin/x64/Debug");
                auto program = QString("PhoneHandler.exe");
                process.start(program);
             
                qDebug() << process.state(); // return not running 
            
            J 1 Reply Last reply 6 Oct 2021, 14:54
            0
            • J junzhe fan
              6 Oct 2021, 14:52

              @jsulm I am not sure, I tried that before, but it seems the c# console application can only be triggered by cmd.exe interpreter ?
              if I do this:

                  process.setWorkingDirectory("E:/new Qt/CC/PhoneHandler/PhoneHandler/bin/x64/Debug");
                  auto program = QString("PhoneHandler.exe");
                  process.start(program);
               
                  qDebug() << process.state(); // return not running 
              
              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 6 Oct 2021, 14:54 last edited by jsulm 10 Jun 2021, 14:56
              #6

              @junzhe-fan said in How to write data to the c# stdin via Qprocess ?:

              qDebug() << phoneHandler.state(); // return not running

              You need to wait for it to start https://doc.qt.io/qt-5/qprocess.html#waitForStarted before state() can return running.

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

              1 Reply Last reply
              1
              • J junzhe fan
                6 Oct 2021, 14:25

                Hello.
                I am having trouble writing data to a c# console application( spawned using QProcess as a sub-process of my Qt application), the stdout works fine(i.e. I can read data from my c# sub-process, but I can't write data to it or it might be c# application somehow not picking up the data that has been written? )
                It seems some people were having the same issue, but none of they post had been solved

                • Qt/C++ - Can't write to stdin of QProcess running a C# application

                • Writing to standard input of a C# based QProcess

                A very minimal approach I have tried :
                Qt code:

                qdebug()<<process.write("Hi");//return 2 
                process.waitForBytesWritten() //block my GUI thred endlessly 
                

                c# code simply :

                console.writeline("C#") // received by my Qt app via StdOUT succeessfully; 
                string data = console.readline(); // block- never revice the data I sent from my Qt app 
                console.writeline(data);  
                
                J Offline
                J Offline
                JonB
                wrote on 6 Oct 2021, 15:00 last edited by JonB 10 Jun 2021, 15:00
                #7

                @junzhe-fan
                I hope you get it to work!

                First follow the suggestions @jsulm is giving you.

                However, I am a C#-er and a Qt-er and a QProcess-er! :) I was also the person who tried replying at Writing to standard input of a C# based QProcess, but it seems we didn't resolve.

                I looked at your Qt/C++ - Can't write to stdin of QProcess running a C# application. That question seems to have been written by someone who knows what he was doing and tried various things, yet he was apparently defeated.

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  junzhe fan
                  wrote on 6 Oct 2021, 15:02 last edited by junzhe fan 10 Jun 2021, 15:03
                  #8

                  ok, so it will not block but return false, and I am not a advanced developer, so will the cmd.exe affect the stdin operation ? @jsulm

                      qDebug()<<phoneHandler.waitForStarted(); //return false 
                  
                  J 1 Reply Last reply 6 Oct 2021, 15:33
                  0
                  • J Offline
                    J Offline
                    junzhe fan
                    wrote on 6 Oct 2021, 15:24 last edited by junzhe fan 10 Jun 2021, 15:32
                    #9

                    @JonB on top of that person ,I also tried using QLocalServer class coupled with C#'s NamedPipeClientStream class to form pipe to communicate, I can see that the pipe was successfully created, but stdin of the .net framework was also not working, so I assumed there must be some internal conflicts between Qt and .net stdin as I saw some other posts said the stdin of .net just didn't work with Qt after the release of .net 4

                    The QLocalServer Approach: I am not sure if I am doing it correctly
                    Qt:

                    server.listen("testpipe");
                    connect(server,&QLocalServer::newConnection,[](){
                           QLocalSocket socket = server->nextPendingConnection();
                           socket->write("Hi c# ");
                           socket->waitForBytesWritten() //infinite block here 
                    });
                    

                    c#:

                     using (NamedPipeClientStream pipeClient =
                                new NamedPipeClientStream(".", "testpipe", PipeDirection.In))
                            {
                    
                                // Connect to the pipe or wait until the pipe is available.
                                Console.Write("Attempting to connect to pipe...");
                                pipeClient.Connect();
                                Console.WriteLine("Connected to pipe.");
                                using (StreamReader sr = new StreamReader(pipeClient))
                                {
                                    // Display the read text to the console
                                    string temp;
                                    while ((temp = sr.ReadLine()) != null)
                                    {
                                        Console.WriteLine("Received from server: {0}", temp);
                                       //nothing received 
                                    }
                                }
                            }
                    
                    1 Reply Last reply
                    0
                    • J junzhe fan
                      6 Oct 2021, 15:02

                      ok, so it will not block but return false, and I am not a advanced developer, so will the cmd.exe affect the stdin operation ? @jsulm

                          qDebug()<<phoneHandler.waitForStarted(); //return false 
                      
                      J Offline
                      J Offline
                      JonB
                      wrote on 6 Oct 2021, 15:33 last edited by
                      #10

                      @junzhe-fan said in How to write data to the c# stdin via Qprocess ?:

                      so will the cmd.exe affect the stdin operation ? @jsulm

                      It might. Since we don't know and you (seem to) have no reason/need to be using it to run an exe, it's best if you don't have it.

                        qDebug()<<phoneHandler.waitForStarted(); //return false 
                      

                      Hmm to false. However:

                          process.setWorkingDirectory("E:/new Qt/CC/PhoneHandler/PhoneHandler/bin/x64/Debug");
                          auto program = QString("PhoneHandler.exe");
                          process.start(program);
                      

                      I believe we have been through this, and I don't think it does what you think it does! The setWorkingDirectory() sets the working for the process once it runs. I think you will find it does not use that to locate the executable to run, so I think you will find it cannot find your executable. Do:

                      auto program = QString("E:/new Qt/CC/PhoneHandler/PhoneHandler/bin/x64/Debug/PhoneHandler.exe");
                      

                      Better?

                      1 Reply Last reply
                      1

                      10/10

                      6 Oct 2021, 15:33

                      • Login

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