使用 PdhAddCounter 函数获取特定进程的网络发送速率和接收速率,需要先获取进程的实例名称,然后根据实例名称构建相应的性能计数器路径。

下面是一个示例代码,用于获取特定进程的网络发送速率和接收速率:

#include <windows.h>
#include <pdh.h>
#include <pdhmsg.h>
#include <iostream>
#pragma comment(lib, "pdh.lib")

int main()
{
    PDH_STATUS pdhStatus;
    HQUERY hQuery;
    HCOUNTER hCounterSent, hCounterReceived;
    PDH_HCOUNTER hCounter;
    DWORD dwBufferSize = 0;
    DWORD dwCounterType;
    WCHAR szInstanceName[MAX_PATH];
    WCHAR szCounterPath[MAX_PATH];

    // 获取进程的实例名称
    DWORD dwProcessId = 1234; // 替换为目标进程的进程ID
    HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwProcessId);
    if (hProcess == NULL)
    {
        std::cout << "Failed to open process." << std::endl;
        return 1;
    }
    if (!GetProcessImageFileName(hProcess, szInstanceName, MAX_PATH))
    {
        std::cout << "Failed to get process image file name." << std::endl;
        CloseHandle(hProcess);
        return 1;
    }
    CloseHandle(hProcess);

    // 构建性能计数器路径
    swprintf_s(szCounterPath, L"\Process(%s)\IO Data Bytes/sec", szInstanceName);

    // 打开性能计数器查询
    pdhStatus = PdhOpenQuery(NULL, 0, &hQuery);
    if (pdhStatus != ERROR_SUCCESS)
    {
        std::cout << "Failed to open query. Error code: " << pdhStatus << std::endl;
        return 1;
    }

    // 添加网络发送速率计数器
    pdhStatus = PdhAddCounter(hQuery, szCounterPath, 0, &hCounterSent);
    if (pdhStatus != ERROR_SUCCESS)
    {
        std::cout << "Failed to add counter. Error code: " << pdhStatus << std::endl;
        PdhCloseQuery(hQuery);
        return 1;
    }

    // 修改计数器路径为接收速率
    wcscpy_s(szCounterPath, L"\Process(%s)\IO Data Bytes/sec");
    pdhStatus = PdhAddCounter(hQuery, szCounterPath, 0, &hCounterReceived);
    if (pdhStatus != ERROR_SUCCESS)
    {
        std::cout << "Failed to add counter. Error code: " << pdhStatus << std::endl;
        PdhCloseQuery(hQuery);
        return 1;
    }

    // 收集和显示性能计数器数据
    pdhStatus = PdhCollectQueryData(hQuery);
    if (pdhStatus != ERROR_SUCCESS)
    {
        std::cout << "Failed to collect query data. Error code: " << pdhStatus << std::endl;
        PdhCloseQuery(hQuery);
        return 1;
    }

    // 读取并显示发送速率
    pdhStatus = PdhGetFormattedCounterValue(hCounterSent, PDH_FMT_DOUBLE, &dwCounterType, &dwBufferSize, NULL);
    if (pdhStatus != ERROR_SUCCESS)
    {
        std::cout << "Failed to get formatted counter value. Error code: " << pdhStatus << std::endl;
        PdhCloseQuery(hQuery);
        return 1;
    }
    double dSentRate = 0.0;
    pdhStatus = PdhGetFormattedCounterValue(hCounterSent, PDH_FMT_DOUBLE, &dwCounterType, &dwBufferSize, &dSentRate);
    if (pdhStatus != ERROR_SUCCESS)
    {
        std::cout << "Failed to get formatted counter value. Error code: " << pdhStatus << std::endl;
        PdhCloseQuery(hQuery);
        return 1;
    }
    std::cout << "Sent rate: " << dSentRate << " bytes/sec" << std::endl;

    // 读取并显示接收速率
    pdhStatus = PdhGetFormattedCounterValue(hCounterReceived, PDH_FMT_DOUBLE, &dwCounterType, &dwBufferSize, NULL);
    if (pdhStatus != ERROR_SUCCESS)
    {
        std::cout << "Failed to get formatted counter value. Error code: " << pdhStatus << std::endl;
        PdhCloseQuery(hQuery);
        return 1;
    }
    double dReceivedRate = 0.0;
    pdhStatus = PdhGetFormattedCounterValue(hCounterReceived, PDH_FMT_DOUBLE, &dwCounterType, &dwBufferSize, &dReceivedRate);
    if (pdhStatus != ERROR_SUCCESS)
    {
        std::cout << "Failed to get formatted counter value. Error code: " << pdhStatus << std::endl;
        PdhCloseQuery(hQuery);
        return 1;
    }
    std::cout << "Received rate: " << dReceivedRate << " bytes/sec" << std::endl;

    // 关闭查询和计数器
    PdhCloseQuery(hQuery);
    PdhRemoveCounter(hCounterSent);
    PdhRemoveCounter(hCounterReceived);

    return 0;
}

注意替换代码中的dwProcessId为目标进程的进程ID。此示例代码使用了Windows API函数GetProcessImageFileName来获取进程的实例名称。根据实际情况,你可能需要根据自己的需求来获取进程的实例名称。

此示例代码仅获取了网络发送速率和接收速率,你可以根据需要添加其他的性能计数器来获取更多的性能数据。

C++ 使用 PdhAddCounter 获取特定进程网络发送和接收速率

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

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