Getting Exitcode from Powershell from within C++ Program with QProcess

Viewed 142

I wonder if someone has an Answer for this Problem.
I use QProcess to start a Powershell script. The start of the QProcess is initiated from a QThread. This Thread finishes like expected but the QProcess never calls its readyReadStandardOutput() Signal nor its finished() Signal. Both are bound to 2 different slots and I can see that the script gets executed and I also know that the script finshes with an exitCode.

My goal with this is basically to read the Exitcode of the Powershell script I wrote. I tried different approaches but could not find a solution to get the Exitcode of the Powershell script which is either ran with cstdlib function system() or QProcess. So I decided I would read it from stdout. For that I would simply print the $LastExitCode before exiting the script and read that from within the C++ Program.

Thanks in advance.

Edit:

Updated example:

main.cpp

#include <QtCore/QCoreApplication>
#include <QTextStream>
#include "ManagerClass.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QTextStream wrappedStream(stdin);
    QString scriptPath;
    if (argc < 2)
    {
        std::cout << "Path to script:" << "\n";
        wrappedStream >> scriptPath;
    }
    ManagerClass manager(scriptPath);

    return a.exec();
}

ManagerClass.h

#pragma once

#include <QObject>
#include <QProcess>
#include <iostream>
#include "ExecutionThread.h"

class ManagerClass : public QObject
{
    Q_OBJECT

public:
    ManagerClass(const QString& p_scriptPath, QObject *parent = nullptr);
    ~ManagerClass();

public slots:
    void threadFinished();
    void processStarted();
    void processFinished(int exitCode, QProcess::ExitStatus exitStatus);

private:
    ExecutionThread m_thread;
    const QString SCRIPT_PATH;
};

ManagerClass.cpp

#include "ManagerClass.h"

ManagerClass::ManagerClass(const QString& p_scriptPath, QObject* parent)
    : QObject(parent), SCRIPT_PATH(p_scriptPath)
{
    m_thread.getProcessPointer();
    m_thread.registerScriptPath(p_scriptPath);
    QObject::connect(m_thread.getProcessPointer(), QOverload<int, QProcess::ExitStatus> ::of(&QProcess::finished), this, &ManagerClass::processFinished);
    QObject::connect(m_thread.getProcessPointer(), &QProcess::started, this, &ManagerClass::processStarted);
    QObject::connect(&m_thread, &ExecutionThread::finished, this, &ManagerClass::threadFinished);
    m_thread.start();
}

ManagerClass::~ManagerClass() {}

void ManagerClass::processFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
    std::cout << " Process finished successfully." << "\n";
    std::cin.get();
}

void ManagerClass::threadFinished()
{
    std::cout << " Thread finished successfully." << "\n";
}

void ManagerClass::processStarted()
{
    std::cout << " Process started." << "\n";
}

ExecutionThread.h

#pragma once

#include <QThread>
#include <QProcess>
#include <iostream>

class ExecutionThread : public QThread
{
    Q_OBJECT

public:
    ExecutionThread();
    ~ExecutionThread();

    void run();
    void registerScriptPath(const QString& p_scriptPath);
    QProcess* getProcessPointer();

signals:
    void finished();

private:
    QProcess* m_Process;
    QString m_scriptPath;
};

ExecutionThread.cpp

#include "ExecutionThread.h"

ExecutionThread::ExecutionThread() : m_Process(new QProcess) {}

ExecutionThread::~ExecutionThread()
{
    std::cout << "ExecutionThread::~ExecutionThread()";
}

void ExecutionThread::run()
{
    QStringList commands;
    m_Process->start(m_scriptPath, commands);
    emit finished();
}

void ExecutionThread::registerScriptPath(const QString& p_scriptPath)
{
    m_scriptPath = p_scriptPath;
}
QProcess* ExecutionThread::getProcessPointer()
{
    return m_Process;
}

EDIT: I solved the issue I was facing with always getting 1 as Exitcode. It turns out that I misunderstood the Qt API for QProcess. Still I dont get Signals from that QProcess object when it finishes. The above Example uses m_Process->start() and there is no signal whenever the given powershell script finishes. As mentioned before m_Process->execute() doesnt signal anything too.

0 Answers
Related