get all running processes info using QProcess

五迷三道 提交于 2019-11-28 12:11:35

You can run wmic.exe with "/OUTPUT:STDOUT" switch to print the process info directly to stdout. However, I was unable to read this info through QProcess API and save it in variable. Here's the code I used:

#include <QtCore/QCoreApplication>
#include <QProcess>
#include <QDebug>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QProcess process;
    process.setReadChannel(QProcess::StandardOutput);
    process.setReadChannelMode(QProcess::MergedChannels);
//    process.start("cmd.exe /C echo test");
    process.start("wmic.exe /OUTPUT:STDOUT PROCESS get Caption");

    process.waitForStarted(1000);
    process.waitForFinished(1000);

    QByteArray list = process.readAll();
    qDebug() << "Read" << list.length() << "bytes";
    qDebug() << list;
}

This code successfully captures output of "cmd.exe /C echo test", but doesn't work on wmic.exe. It seems that process wmic.exe is never finished, and I suppose it's stdout is never flushed so you don't receive anything throught QProcess::readAll().

That's all help I can give you. Maybe you, or some other SO user will find bug in the snippet above.

Sivambigai.M

Try this it will work well.

process.start("cmd", QStringList() << "/C" << "echo" << "process" << "get" << "caption" << "|" << "wmic");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!