Results 1 to 7 of 7

Thread: QProcess not communicating with .net framework > 3.5

  1. #1
    Join Date
    Apr 2015
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows

    Question QProcess not communicating with .net framework > 3.5

    Hi all,

    I have a main Qt application, launching a C# app with QProcess.
    I would like to communicate with the C# app using the QProcess tools, the standard channels.
    Although the communication is working perfectly when I compile the C# app with .net framework 3.5, I am not receiving any data from the process when it is compiled with .net 4.0 or 4.5

    I posted a thread on the msdn forums : https://social.msdn.microsoft.com/Fo...=csharpgeneral

    I have made a minimal console example to replicate the issue:
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QProcess>
    3. #include <iostream>
    4. #include <string>
    5. using namespace std;
    6.  
    7. int main(int argc, char *argv[])
    8. {
    9. QCoreApplication a(argc, argv);
    10.  
    11. // Path to the C# executable
    12. QString program = "D:/Documents/ConsoleApplication1/ConsoleApplication1/bin/Release/ConsoleApplication1.exe";
    13.  
    14. // Start child Process
    15. QProcess * process = new QProcess();
    16. process->setProgram(program);
    17. // Execute lambda function whenever a message from the process is received
    18. QObject::connect(process, &QProcess::readyReadStandardOutput, [&](){ cout << process->readAll().toStdString(); } );
    19. process->start();
    20.  
    21. string text;
    22. cin >> text;
    23. process->write( QByteArray::fromStdString(text) + "\r\n" );
    24.  
    25. return a.exec();
    26. }
    To copy to clipboard, switch view to plain text mode 

    and a minimal C# example :
    Qt Code:
    1. namespace ConsoleApplication1
    2. {
    3. class Program
    4. {
    5. static void Main(string[] args)
    6. {
    7. string text;
    8. while (true)
    9. {
    10. Console.WriteLine("0");
    11. text = Console.ReadLine();
    12. Console.WriteLine("1");
    13. Console.WriteLine(text);
    14. }
    15. }
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

    I am using Qt 5.4.1 on windows 7, and compiling with msvc 2010 32 bits.

    Thanks in advance for your help
    Last edited by SimpleIsBetter; 30th April 2015 at 22:09.

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QProcess not communicating with .net framework > 3.5

    QProcess doesn't care if your program is using .net 3.5, 4.0, or 4.5 of course. Is your QProcess starting your command line program successfully? Use the QProcess::error, QProcess::started, and QProcess::stateChanged signals to figure out what's going on.

    I am also assuming you can successfully manually run your command line program when compiled with .net 4.0 or 4.5, correct?

    Edit: What is the return value from process->write()?
    Last edited by jefftee; 1st May 2015 at 07:14.

  3. #3
    Join Date
    Apr 2015
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QProcess not communicating with .net framework > 3.5

    Is your QProcess starting your command line program successfully ?
    Yes
    I am also assuming you can successfully manually run your command line program when compiled with .net 4.0 or 4.5, correct ?
    Correct
    Edit: What is the return value from process->write() ?
    The return value is equal to the number of byte sent.


    Here is the code that I am actually using for debugging :

    main.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    mainWindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QPushButton>
    6.  
    7. #include "process.h"
    8.  
    9. class MainWindow : public QMainWindow
    10. {
    11. Q_OBJECT
    12.  
    13. public:
    14. explicit MainWindow(QWidget *parent = 0);
    15.  
    16. public slots:
    17. void incomingMessageFromCSharp(QByteArray ba);
    18. void sendMessageToCSharp();
    19.  
    20. private:
    21. Process *process;
    22. };
    23.  
    24. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    mainWindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2.  
    3. #include <QDebug>
    4.  
    5. MainWindow::MainWindow(QWidget *parent) :
    6. QMainWindow(parent)
    7. {
    8. QString program = "D:/Documents/Laverie/Nayax/ConsoleApplication1/ConsoleApplication1/bin/Release/ConsoleApplication1.exe";
    9.  
    10. process = new Process(this);
    11. connect(process, SIGNAL(messageReceived(QByteArray)), this, SLOT(incomingMessageFromCSharp(QByteArray)));
    12. process->setProgram(program);
    13. process->start();
    14.  
    15. QPushButton * pb = new QPushButton("Send ping", this);
    16. connect(pb, SIGNAL(clicked()), this, SLOT(sendMessageToCSharp()));
    17. }
    18.  
    19. void MainWindow::sendMessageToCSharp()
    20. {
    21. QByteArray ba ="ping";
    22. ba += '\r';
    23. ba += '\n';
    24. process->sendMessage(ba);
    25. }
    26.  
    27. void MainWindow::incomingMessageFromCSharp(QByteArray ba)
    28. {
    29. //
    30. }
    To copy to clipboard, switch view to plain text mode 

    process.h
    Qt Code:
    1. #ifndef PROCESS_H
    2. #define PROCESS_H
    3.  
    4. #include <QProcess>
    5.  
    6. class Process : public QProcess
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. explicit Process(QObject * parent = 0);
    12. ~Process();
    13.  
    14. public slots:
    15. void processError(QProcess::ProcessError error);
    16. void processFinished(int exitCode, QProcess::ExitStatus exitStatus);
    17. void processReadyReadStandardError();
    18. void processReadyReadStandardOutput();
    19. void processStarted();
    20. void processStateChanged(QProcess::ProcessState state);
    21.  
    22. void sendMessage(QByteArray ba);
    23.  
    24. signals:
    25. void messageReceived(QByteArray);
    26.  
    27. protected:
    28. QString enumErrorToString(QProcess::ProcessError error);
    29. QString enumStateToString(QProcess::ProcessState state);
    30. };
    31.  
    32. #endif // PROCESS_H
    To copy to clipboard, switch view to plain text mode 

    process.cpp
    Qt Code:
    1. #include "process.h"
    2.  
    3. #include <QDebug>
    4.  
    5. Process::Process(QObject *parent) :
    6. QProcess(parent)
    7. {
    8. // Error signal
    9. connect(this, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
    10.  
    11. // Process finished signal
    12. connect(this, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(processFinished(int,QProcess::ExitStatus)));
    13.  
    14. // Standard error signal
    15. connect(this, SIGNAL(readyReadStandardError()), this, SLOT(processReadyReadStandardError()));
    16.  
    17. // Standard output signal
    18. connect(this, SIGNAL(readyReadStandardOutput()), this, SLOT(processReadyReadStandardOutput()));
    19.  
    20. // Process started signal
    21. connect(this, SIGNAL(started()), this, SLOT(processStarted()));
    22.  
    23. // Process state signal
    24. connect(this, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(processStateChanged(QProcess::ProcessState)));
    25.  
    26. }
    27.  
    28. Process::~Process()
    29. {
    30. this->close();
    31. }
    32.  
    33. void Process::sendMessage(QByteArray ba)
    34. {
    35. qDebug() << "Sending message " << ba;
    36. qint64 nBytes = this->write(ba);
    37. if ( nBytes != ba.size() )
    38. qWarning() << "Message partially sent : " << nBytes << " sent instead of " << ba.size();
    39. }
    40.  
    41. void Process::processError(QProcess::ProcessError error)
    42. {
    43. if ( error == QProcess::FailedToStart )
    44. qCritical() << "Process failed to start. Check that the path is correct: " << enumErrorToString(error);
    45. else {
    46. qWarning() << "QProcess error: " << enumErrorToString(error);
    47. }
    48. }
    49.  
    50. void Process::processFinished(int exitCode, QProcess::ExitStatus exitStatus)
    51. {
    52. if ( exitStatus == QProcess::CrashExit ) {
    53. qWarning() << "Process crashed";
    54. }
    55. else if ( exitStatus == QProcess::NormalExit )
    56. qDebug() << "Process exited normally : " << exitCode;
    57. else
    58. qCritical() << "enum value not recognized : " << exitStatus;
    59. }
    60.  
    61. void Process::processReadyReadStandardError()
    62. {
    63. qWarning() << "Error :\n" << this->readAllStandardError();
    64. }
    65.  
    66. void Process::processReadyReadStandardOutput()
    67. {
    68. QByteArray ba = this->readAllStandardOutput();
    69. qDebug() << "Message Received : " << ba;
    70. emit messageReceived(ba);
    71. }
    72.  
    73. void Process::processStarted()
    74. {
    75. qDebug() << "Process started";
    76. }
    77.  
    78. void Process::processStateChanged(QProcess::ProcessState state)
    79. {
    80. qDebug() << "Process state changed : " << enumStateToString(state);
    81. }
    82.  
    83.  
    84. QString Process::enumErrorToString(QProcess::ProcessError error)
    85. {
    86. switch (error) {
    87. case QProcess::FailedToStart:
    88. return "QProcess::FailedToStart";
    89. case QProcess::Crashed:
    90. return "QProcess::Crashed";
    91. case QProcess::Timedout:
    92. return "QProcess::Timedout";
    93. case QProcess::WriteError:
    94. return "QProcess::WriteError";
    95. case QProcess::ReadError:
    96. return "QProcess::ReadError";
    97. case QProcess::UnknownError:
    98. return "QProcess::UnknownError";
    99. default:
    100. return "Unknown enum value : " + error;
    101. }
    102. }
    103.  
    104. QString Process::enumStateToString(QProcess::ProcessState state) {
    105. switch (state) {
    106. case QProcess::NotRunning:
    107. return "QProcess::NotRunning";
    108. case QProcess::Starting:
    109. return "QProcess::Starting";
    110. case QProcess::Timedout:
    111. return "QProcess::Running";
    112. default:
    113. return "Unknown enum value : " + state;
    114. }
    115. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QProcess not communicating with .net framework > 3.5

    If you are successfully writing the bytes to the running QProcess, and based on the number of bytes written returned from process->write, it would appear that the data is being written correctly to the running QProcess, so my only guess is that something is different on the .net side.

    Your running QProcess receiving the data correctly, however, then your running QProcess writes to stdout, you're not receiving the readyReadStandardOutput signal, correct?

    Have you tried w/o subclassing QProcess? Not sure if that would make a difference but would remove one layer to debug...

  5. #5
    Join Date
    Apr 2015
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QProcess not communicating with .net framework > 3.5

    I tried without subclasssing QProcess, and unfortunately I obtain the exact same behaviour :

    I can read data from the process without any issue,
    but when I write data to the process, the data is never received.

  6. #6
    Join Date
    Sep 2011
    Posts
    16
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QProcess not communicating with .net framework > 3.5

    Wondering if anyone has any success getting QProcess to interact with a C# program compiled with .NET 4.0 or later? I did a test program that does nothing but Console.ReadLine() and Console.WriteLine() in a infinite loop, and QProcess works when the program is compiled with .NET 3.0 but not when it is compiled with .NET 4.0.

  7. #7
    Join Date
    Apr 2015
    Posts
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QProcess not communicating with .net framework > 3.5

    I ended up using a pipe to send message from Qt to the C# code. NamedPipeClientStream on the C# side and QLocalServer on the Qt side.

Similar Threads

  1. Communicating with a USB device in Qt?
    By N3wb in forum Qt Programming
    Replies: 7
    Last Post: 3rd May 2011, 11:30
  2. webpage communicating to a qt apps
    By ezesolares in forum Qt Programming
    Replies: 0
    Last Post: 23rd February 2011, 17:56
  3. Replies: 1
    Last Post: 4th March 2010, 21:47
  4. Qt communicating with USB device
    By jimmydean101 in forum Newbie
    Replies: 6
    Last Post: 2nd March 2010, 18:02
  5. communicating between threads
    By freekill in forum Newbie
    Replies: 2
    Last Post: 27th November 2009, 09:13

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.