以下是一个使用PDH库获取特定进程网络流量的发送和接收速率的C++代码示例:

#include <iostream>
#include <windows.h>
#include <pdh.h>
#include <pdhmsg.h>

#pragma comment(lib, "pdh.lib")

#define MAX_BUF_SIZE 4096

int main()
{
    PDH_STATUS pdhStatus;
    HQUERY hQuery;
    HCOUNTER hCounterSent, hCounterReceived;
    PDH_FMT_COUNTERVALUE counterValueSent, counterValueReceived;
    DWORD bufferSize = MAX_BUF_SIZE;
    char counterPath[MAX_BUF_SIZE];

    // 初始化PDH库
    pdhStatus = PdhOpenQuery(NULL, NULL, &hQuery);
    if (pdhStatus != ERROR_SUCCESS) {
        std::cerr << "PdhOpenQuery failed with status: " << pdhStatus << std::endl;
        return 1;
    }

    // 获取特定进程的网络发送速率
    sprintf_s(counterPath, MAX_BUF_SIZE, "\\Process(%d)\\IO Data Bytes/sec", GetCurrentProcessId());
    pdhStatus = PdhAddEnglishCounterA(hQuery, counterPath, NULL, &hCounterSent);
    if (pdhStatus != ERROR_SUCCESS) {
        std::cerr << "PdhAddEnglishCounterA (Sent) failed with status: " << pdhStatus << std::endl;
        PdhCloseQuery(hQuery);
        return 1;
    }

    // 获取特定进程的网络接收速率
    sprintf_s(counterPath, MAX_BUF_SIZE, "\\Process(%d)\\IO Data Bytes/sec", GetCurrentProcessId());
    pdhStatus = PdhAddEnglishCounterA(hQuery, counterPath, NULL, &hCounterReceived);
    if (pdhStatus != ERROR_SUCCESS) {
        std::cerr << "PdhAddEnglishCounterA (Received) failed with status: " << pdhStatus << std::endl;
        PdhCloseQuery(hQuery);
        return 1;
    }

    // 查询并显示网络发送和接收速率
    while (true) {
        pdhStatus = PdhCollectQueryData(hQuery);
        if (pdhStatus != ERROR_SUCCESS) {
            std::cerr << "PdhCollectQueryData failed with status: " << pdhStatus << std::endl;
            PdhCloseQuery(hQuery);
            return 1;
        }

        pdhStatus = PdhGetFormattedCounterValue(hCounterSent, PDH_FMT_DOUBLE, NULL, &counterValueSent);
        if (pdhStatus != ERROR_SUCCESS) {
            std::cerr << "PdhGetFormattedCounterValue (Sent) failed with status: " << pdhStatus << std::endl;
            PdhCloseQuery(hQuery);
            return 1;
        }

        pdhStatus = PdhGetFormattedCounterValue(hCounterReceived, PDH_FMT_DOUBLE, NULL, &counterValueReceived);
        if (pdhStatus != ERROR_SUCCESS) {
            std::cerr << "PdhGetFormattedCounterValue (Received) failed with status: " << pdhStatus << std::endl;
            PdhCloseQuery(hQuery);
            return 1;
        }

        std::cout << "Sent: " << counterValueSent.doubleValue << " bytes/sec" << std::endl;
        std::cout << "Received: " << counterValueReceived.doubleValue << " bytes/sec" << std::endl;

        Sleep(1000); // 等待1秒钟
    }

    // 清理资源
    PdhRemoveCounter(hCounterSent);
    PdhRemoveCounter(hCounterReceived);
    PdhCloseQuery(hQuery);

    return 0;
}

请注意,此代码仅适用于Windows平台,并且需要链接PDH库(pdh.lib)。在代码中,我们使用PdhOpenQuery打开一个查询对象,然后使用PdhAddEnglishCounterA添加特定进程的网络发送和接收速率计数器。然后,我们使用PdhCollectQueryData查询数据并使用PdhGetFormattedCounterValue获取格式化的计数器值。最后,我们使用Sleep函数等待1秒钟,并在循环中重复查询和显示网络发送和接收速率。

windows10下用PDH库获取特定进程网络流量的发送和接收速率C++代码

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

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