使用 QProcess 获取所有正在运行的进程信息

发布于 2024-08-28 13:35:24 字数 659 浏览 5 评论 0原文

几天前我问了如何使用QProcess获取系统中所有正在运行的进程。 我找到了一个可以将所有进程输出到文件的命令行:

C:\WINDOWS\system32\wbem\wmic.exe" /OUTPUT:C:\ProcessList.txt PROCESS get Caption

这将创建 C:\ProcessList.txt 文件包含系统中所有正在运行的进程。 我想知道如何使用 QProcess 运行它并将其输出保存到变量中。

似乎每次我尝试运行它并且读到什么都没有发生:

QString program = "C:\\WINDOWS\\system32\\wbem\\wmic.exe";
QStringList arguments;
arguments << "/OUTPUT:C:\\ProcessList.txt" <<"PROCESS"<< "get"<< "Caption";

process->setStandardOutputFile("process.txt");
process->start(program,arguments);

QByteArray result = process->readAll();

我根本不想创建 process.txt 并将所有输出都保存到变量中......

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\system32\wbem\wmic.exe" /OUTPUT:C:\ProcessList.txt PROCESS get Caption

this will create C:\ProcessList.txt file contains all running processes in the system.
i wonder how can i run it using QProcess and take its output to a variable.

it seems every time i try to run it and read nothing happens:

QString program = "C:\\WINDOWS\\system32\\wbem\\wmic.exe";
QStringList arguments;
arguments << "/OUTPUT:C:\\ProcessList.txt" <<"PROCESS"<< "get"<< "Caption";

process->setStandardOutputFile("process.txt");
process->start(program,arguments);

QByteArray result = process->readAll();

i prefer not to create process.txt at all and to take all the output to a variable...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

删除会话 2024-09-04 13:35:24

您可以使用“/OUTPUT:STDOUT”开关运行 wmic.exe,将进程信息直接打印到 stdout。但是,我无法通过 QProcess API 读取此信息并将其保存在变量中。这是我使用的代码:

#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;
}

此代码成功捕获“cmd.exe /C echo test”的输出,但不适用于 wmic.exe。看起来进程 wmic.exe 永远不会完成,我想它的标准输出永远不会刷新,所以你不会通过 QProcess::readAll() 收到任何东西。

这就是我能给你的所有帮助。也许您或其他一些 SO 用户会在上面的代码片段中发现错误。

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.

随风而去 2024-09-04 13:35:24

试试这个,效果会很好。

process.start("cmd", QStringList() << "/C" << "echo" << "process" << "get" << "caption" << "|" << "wmic");

Try this it will work well.

process.start("cmd", QStringList() << "/C" << "echo" << "process" << "get" << "caption" << "|" << "wmic");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文