C++20 并没有提供直接获取进程列表的标准库函数,但可以通过操作系统提供的 API 来实现。以下是 Windows 平台上获取进程列表的示例代码:

#include <windows.h>
#include <tlhelp32.h>
#include <iostream>

int main()
{
    HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    if (hSnapshot == INVALID_HANDLE_VALUE)
    {
        std::cerr << "CreateToolhelp32Snapshot failed" << std::endl;
        return -1;
    }

    PROCESSENTRY32 pe32 = { sizeof(pe32) };
    if (!Process32First(hSnapshot, &pe32))
    {
        std::cerr << "Process32First failed" << std::endl;
        CloseHandle(hSnapshot);
        return -1;
    }

    std::cout << "PID\tName" << std::endl;
    do
    {
        std::cout << pe32.th32ProcessID << "\t" << pe32.szExeFile << std::endl;
    } while (Process32Next(hSnapshot, &pe32));

    CloseHandle(hSnapshot);
    return 0;
}

该代码使用了 Windows API 中的 CreateToolhelp32Snapshot 和 Process32First/Process32Next 函数来获取进程列表。在 Linux 上,可以使用 proc 文件系统来获取进程信息。

c++20 获取进程列表

原文地址: https://www.cveoy.top/t/topic/sEy 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录