Good evening.

Can you please tell me how to control applications using c ++ (it would be cool without winapi)? Do not let it run too long, read the results? If there is no winapi without it - how then do it with it?

Maybe someone has a thread / literature courses on this?

Closed due to the fact that it is necessary to reformulate the question so that it was possible to give an objectively correct answer by the participants of Oceinic , redL1ne , LEQADA , xaja , Aries Oct 15 '15 at 10:30 .

The question gives rise to endless debates and discussions based not on knowledge, but on opinions. To get an answer, rephrase your question so that it can be given an unambiguously correct answer, or delete the question altogether. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 6
    What does it mean to control the application and read the results? - skegg
  • Good day! As I understand it, it is necessary to interrupt the execution of a certain pre-specified process when the time limit is exceeded and read the information sent by this process from the standard output stream (for display in a window for example). If so, then I did this in C ++ using the Qt library, since I needed a cross-plasma solution. If necessary, I can share the source)) - progzdeveloper
  • Yes, it would be cool :) - Arc

2 answers 2

Create a second thread, it will follow the workflow.

  • not clear ... nothing. - Arc

Here is the solution using the Qt library:

// qprocesscontroller.h #pragma once #include <QProcess> class QProcessInfo; class QProcessControllerPrivate; class QProcessController : public QProcess { Q_OBJECT Q_PROPERTY(int timeout READ timeout WRITE setTimeout) public: // Конструктор explicit QProcessController(QObject* parent = 0); // Деструктор ~QProcessController(); // Установить лимит по времени void setTimeout(int value); // Получить лимит по времени int timeout() const; private: // Указатель на внутренние данные (pImpl) QProcessControllerPrivate *d_ptr; Q_DECLARE_PRIVATE(QProcessController) Q_DISABLE_COPY(QProcessController) }; // qprocesscontroller.cpp #include "qprocesscontroller.h" #include <QTimer> class QProcessControllerPrivate { public: QTimer *timer; // таймер }; QProcessController::QProcessController(QObject* parent /*= 0*/) : QProcess(parent), d_ptr(new QProcessControllerPrivate) { Q_D(QProcessController); // создаем объект таймера // дочерний для объекта QProcessController, чтобы он // удалялся вместе с this d->timer = new QTimer(this); d->timer->setSingleShot(true); // устанавливаем единственное срабатывание // соединяем сигнал запуска процесса со слотом старта таймера connect(this, SIGNAL(started()), d->timer, SLOT(start())); // соединяем сигнал таймера со слотом принудительного завершения connect(d->timer, SIGNAL(timeout()), this, SLOT(kill())); } QProcessController::~QProcessController() { delete d_ptr; } void QProcessController::setTimeout(int value) { Q_D(const QProcessController); return d->timer->setInterval(value); } int QProcessController::timeout() const { Q_D(const QProcessController); return d->timer->interval(); } // main.cpp - пример использования класса QProcessController #include <QApplication> #include "qprocesscontroller.h" int main(int argc, char* argv[]) { // Создаем объект приложения // Это необходимо для запуска цикла сообщений, // если этого не сделать, то наше приложение завершится раньше // процесса который мы запустим QApplication app(argc, argv); // создаем контроллер процесса QProcessController ctrl; // соединяем сигнал завершения процесса со // слотом завершения текущего процесса // чтобы прервать цикл обработки сообщений // и корректно завершить текущий процесс // если вы пишите оконное приложение - этого делать не надо QObject::connect(&ctrl, SIGNAL(finished(int, int)), &app, SLOT(quit())); // задаем лимит по времени (3 сек) ctrl.setTimeout(3000); // стартуем процесс ... ctrl.start("calc.exe"); return app.exec(); } 

Solution idea: the QProcessController class is a successor of the QProcess class and inherits all its capabilities (in particular, receiving process data from stdio, which is not covered in the example, but is easily found in the Qt documentation). The QProcess class QProcess is part of the Qt class system and is a QObject descendant, which allows us to use the Qt signal and slot system to control the timer (and through it, the lifetime of the child process). Actually, the control takes place as follows: when the child process starts, a timer starts as soon as the child receives a signal from the timer kill() (if of course it did not have time to end earlier). It would be possible to set limits on the used memory and processor load, but such decisions affect the platform-dependent functions of the OS ... Therefore, in the answer I limited myself to this implementation. I wish you success!

  • You can ask to paint in more detail, I did not understand almost nothing :( - Arc
  • What exactly is not clear to you? If you did not work with the Qt library, I can advise you to look at the Qt documentation, which is available online, in particular, about signals and slots and the QObject base class, and see those classes that are used in the implementation) Success!) - progzdeveloper
  • @progzdeveloper, but can you write any explanations for those who have not studied such a mystical library? For example, I absolutely do not understand where the pointer d is taken from you here is return d-> timer-> interval (); or here d-> timer = new QTimer (this); Is it really connected with the part of the _ptr name in QProcessControllerPrivate * d_ptr; ??? Just very interesting. If this is actually the case, then such a product can hardly be recommended (although, as I know, it is widely used). - avp
  • You guessed it correctly: I use the so-called idiom Pointer to Implementation. The fact is that such an approach does not destroy binary compatibility of libraries) Moreover, this approach is used in all classes of the Qt library) Here is a collection of links on the issue: habrahabr.ru/post/76248 qt-project.org/wiki/Dpointer zchydem.enume. net / 2010/01/19 / ... - progzdeveloper
  • Mmdaaa ... Something tells me that programs using this technology that are over 10,000 lines in size do not expand well and are in a state of chronic debugging. - avp