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 ?
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 3 Posters 1.2k 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 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);  
    
    jsulmJ JonBJ 2 Replies Last reply
    0
    • J junzhe fan

      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);  
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on 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 last edited by junzhe fan
        #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

        jsulmJ 1 Reply Last reply
        0
        • J junzhe fan
             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

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on 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 last edited by junzhe fan
            #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 
            
            jsulmJ 1 Reply Last reply
            0
            • J junzhe fan

              @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 
              
              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by jsulm
              #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

                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);  
                
                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #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 last edited by junzhe fan
                  #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 
                  
                  JonBJ 1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    junzhe fan
                    wrote on last edited by junzhe fan
                    #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

                      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 
                      
                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on 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

                      • Login

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