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

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



  • 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

    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);  
    

  • Lifetime Qt Champion

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



  •    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


  • Lifetime Qt Champion

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



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

  • Lifetime Qt Champion

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



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



  • 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 
    


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


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


Log in to reply