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
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
MainWindow w;
w.show();
return a.exec();
}
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
mainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPushButton>
#include "process.h"
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
public slots:
void sendMessageToCSharp();
private:
Process *process;
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPushButton>
#include "process.h"
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
public slots:
void incomingMessageFromCSharp(QByteArray ba);
void sendMessageToCSharp();
private:
Process *process;
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
mainWindow.cpp
#include "mainwindow.h"
#include <QDebug>
MainWindow
::MainWindow(QWidget *parent
) :{
QString program
= "D:/Documents/Laverie/Nayax/ConsoleApplication1/ConsoleApplication1/bin/Release/ConsoleApplication1.exe";
process = new Process(this);
connect(process,
SIGNAL(messageReceived
(QByteArray)),
this,
SLOT(incomingMessageFromCSharp
(QByteArray)));
process->setProgram(program);
process->start();
connect(pb, SIGNAL(clicked()), this, SLOT(sendMessageToCSharp()));
}
void MainWindow::sendMessageToCSharp()
{
ba += '\r';
ba += '\n';
process->sendMessage(ba);
}
void MainWindow
::incomingMessageFromCSharp(QByteArray ba
) {
//
}
#include "mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
QString program = "D:/Documents/Laverie/Nayax/ConsoleApplication1/ConsoleApplication1/bin/Release/ConsoleApplication1.exe";
process = new Process(this);
connect(process, SIGNAL(messageReceived(QByteArray)), this, SLOT(incomingMessageFromCSharp(QByteArray)));
process->setProgram(program);
process->start();
QPushButton * pb = new QPushButton("Send ping", this);
connect(pb, SIGNAL(clicked()), this, SLOT(sendMessageToCSharp()));
}
void MainWindow::sendMessageToCSharp()
{
QByteArray ba ="ping";
ba += '\r';
ba += '\n';
process->sendMessage(ba);
}
void MainWindow::incomingMessageFromCSharp(QByteArray ba)
{
//
}
To copy to clipboard, switch view to plain text mode
process.h
#ifndef PROCESS_H
#define PROCESS_H
#include <QProcess>
{
Q_OBJECT
public:
explicit Process
(QObject * parent
= 0);
~Process();
public slots:
void processError
(QProcess::ProcessError error
);
void processFinished
(int exitCode,
QProcess::ExitStatus exitStatus
);
void processReadyReadStandardError();
void processReadyReadStandardOutput();
void processStarted();
void processStateChanged
(QProcess::ProcessState state
);
signals:
protected:
};
#endif // PROCESS_H
#ifndef PROCESS_H
#define PROCESS_H
#include <QProcess>
class Process : public QProcess
{
Q_OBJECT
public:
explicit Process(QObject * parent = 0);
~Process();
public slots:
void processError(QProcess::ProcessError error);
void processFinished(int exitCode, QProcess::ExitStatus exitStatus);
void processReadyReadStandardError();
void processReadyReadStandardOutput();
void processStarted();
void processStateChanged(QProcess::ProcessState state);
void sendMessage(QByteArray ba);
signals:
void messageReceived(QByteArray);
protected:
QString enumErrorToString(QProcess::ProcessError error);
QString enumStateToString(QProcess::ProcessState state);
};
#endif // PROCESS_H
To copy to clipboard, switch view to plain text mode
process.cpp
#include "process.h"
#include <QDebug>
Process
::Process(QObject *parent
) :{
// Error signal
connect(this,
SIGNAL(error
(QProcess::ProcessError)),
this,
SLOT(processError
(QProcess::ProcessError)));
// Process finished signal
connect(this,
SIGNAL(finished
(int,
QProcess::ExitStatus)),
this,
SLOT(processFinished
(int,
QProcess::ExitStatus)));
// Standard error signal
connect(this, SIGNAL(readyReadStandardError()), this, SLOT(processReadyReadStandardError()));
// Standard output signal
connect(this, SIGNAL(readyReadStandardOutput()), this, SLOT(processReadyReadStandardOutput()));
// Process started signal
connect(this, SIGNAL(started()), this, SLOT(processStarted()));
// Process state signal
connect(this,
SIGNAL(stateChanged
(QProcess::ProcessState)),
this,
SLOT(processStateChanged
(QProcess::ProcessState)));
}
Process::~Process()
{
this->close();
}
{
qDebug() << "Sending message " << ba;
qint64 nBytes = this->write(ba);
if ( nBytes != ba.size() )
qWarning() << "Message partially sent : " << nBytes << " sent instead of " << ba.size();
}
void Process
::processError(QProcess::ProcessError error
) {
qCritical() << "Process failed to start. Check that the path is correct: " << enumErrorToString(error);
else {
qWarning() << "QProcess error: " << enumErrorToString(error);
}
}
void Process
::processFinished(int exitCode,
QProcess::ExitStatus exitStatus
) {
if ( exitStatus
== QProcess::CrashExit ) { qWarning() << "Process crashed";
}
else if ( exitStatus
== QProcess::NormalExit ) qDebug() << "Process exited normally : " << exitCode;
else
qCritical() << "enum value not recognized : " << exitStatus;
}
void Process::processReadyReadStandardError()
{
qWarning() << "Error :\n" << this->readAllStandardError();
}
void Process::processReadyReadStandardOutput()
{
qDebug() << "Message Received : " << ba;
emit messageReceived(ba);
}
void Process::processStarted()
{
qDebug() << "Process started";
}
void Process
::processStateChanged(QProcess::ProcessState state
) {
qDebug() << "Process state changed : " << enumStateToString(state);
}
{
switch (error) {
return "QProcess::FailedToStart";
return "QProcess::Crashed";
return "QProcess::Timedout";
return "QProcess::WriteError";
return "QProcess::ReadError";
return "QProcess::UnknownError";
default:
return "Unknown enum value : " + error;
}
}
switch (state) {
return "QProcess::NotRunning";
return "QProcess::Starting";
return "QProcess::Running";
default:
return "Unknown enum value : " + state;
}
}
#include "process.h"
#include <QDebug>
Process::Process(QObject *parent) :
QProcess(parent)
{
// Error signal
connect(this, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)));
// Process finished signal
connect(this, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(processFinished(int,QProcess::ExitStatus)));
// Standard error signal
connect(this, SIGNAL(readyReadStandardError()), this, SLOT(processReadyReadStandardError()));
// Standard output signal
connect(this, SIGNAL(readyReadStandardOutput()), this, SLOT(processReadyReadStandardOutput()));
// Process started signal
connect(this, SIGNAL(started()), this, SLOT(processStarted()));
// Process state signal
connect(this, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT(processStateChanged(QProcess::ProcessState)));
}
Process::~Process()
{
this->close();
}
void Process::sendMessage(QByteArray ba)
{
qDebug() << "Sending message " << ba;
qint64 nBytes = this->write(ba);
if ( nBytes != ba.size() )
qWarning() << "Message partially sent : " << nBytes << " sent instead of " << ba.size();
}
void Process::processError(QProcess::ProcessError error)
{
if ( error == QProcess::FailedToStart )
qCritical() << "Process failed to start. Check that the path is correct: " << enumErrorToString(error);
else {
qWarning() << "QProcess error: " << enumErrorToString(error);
}
}
void Process::processFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
if ( exitStatus == QProcess::CrashExit ) {
qWarning() << "Process crashed";
}
else if ( exitStatus == QProcess::NormalExit )
qDebug() << "Process exited normally : " << exitCode;
else
qCritical() << "enum value not recognized : " << exitStatus;
}
void Process::processReadyReadStandardError()
{
qWarning() << "Error :\n" << this->readAllStandardError();
}
void Process::processReadyReadStandardOutput()
{
QByteArray ba = this->readAllStandardOutput();
qDebug() << "Message Received : " << ba;
emit messageReceived(ba);
}
void Process::processStarted()
{
qDebug() << "Process started";
}
void Process::processStateChanged(QProcess::ProcessState state)
{
qDebug() << "Process state changed : " << enumStateToString(state);
}
QString Process::enumErrorToString(QProcess::ProcessError error)
{
switch (error) {
case QProcess::FailedToStart:
return "QProcess::FailedToStart";
case QProcess::Crashed:
return "QProcess::Crashed";
case QProcess::Timedout:
return "QProcess::Timedout";
case QProcess::WriteError:
return "QProcess::WriteError";
case QProcess::ReadError:
return "QProcess::ReadError";
case QProcess::UnknownError:
return "QProcess::UnknownError";
default:
return "Unknown enum value : " + error;
}
}
QString Process::enumStateToString(QProcess::ProcessState state) {
switch (state) {
case QProcess::NotRunning:
return "QProcess::NotRunning";
case QProcess::Starting:
return "QProcess::Starting";
case QProcess::Timedout:
return "QProcess::Running";
default:
return "Unknown enum value : " + state;
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks