get all running processes info using QProcess

前端 未结 2 1240
独厮守ぢ
独厮守ぢ 2020-12-11 03:56

few days ago i asked about how to get all running processes in the system using QProcess. i found a command line that can output all processes to a file:

C:\\WINDOWS

相关标签:
2条回答
  • 2020-12-11 04:19

    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.

    0 讨论(0)
  • 2020-12-11 04:30

    Try this it will work well.

    process.start("cmd", QStringList() << "/C" << "echo" << "process" << "get" << "caption" << "|" << "wmic");
    
    0 讨论(0)
提交回复
热议问题