使用 PdhAddCounter 函数获取指定进程的网络发送和接收速率,可以按照以下步骤进行:

  1. 首先,需要使用 PdhOpenQuery 函数创建一个性能数据查询对象。示例代码如下:
PDH_HQUERY hQuery;
PdhOpenQuery(NULL, NULL, &hQuery);
  1. 然后,使用 PdhAddCounter 函数添加网络发送和接收速率的性能计数器。示例代码如下:
PDH_HCOUNTER hCounterSent, hCounterReceived;
PdhAddCounter(hQuery, L"\Process(<进程名>)\IO Data Bytes/sec", NULL, &hCounterSent);
PdhAddCounter(hQuery, L"\Process(<进程名>)\IO Other Bytes/sec", NULL, &hCounterReceived);

其中,<进程名>需要替换为具体的进程名。

  1. 接下来,使用 PdhCollectQueryData 函数收集性能数据。示例代码如下:
PdhCollectQueryData(hQuery);
  1. 最后,使用 PdhGetFormattedCounterValue 函数获取性能计数器的值。示例代码如下:
PDH_FMT_COUNTERVALUE counterValueSent, counterValueReceived;
PdhGetFormattedCounterValue(hCounterSent, PDH_FMT_DOUBLE, NULL, &counterValueSent);
PdhGetFormattedCounterValue(hCounterReceived, PDH_FMT_DOUBLE, NULL, &counterValueReceived);

double sentRate = counterValueSent.doubleValue;
double receivedRate = counterValueReceived.doubleValue;

其中,sentRate 和 receivedRate 分别表示网络发送和接收速率。

完整示例代码如下:

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

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

int main()
{
    PDH_STATUS pdhStatus;

    // 创建性能数据查询对象
    PDH_HQUERY hQuery;
    pdhStatus = PdhOpenQuery(NULL, NULL, &hQuery);
    if (pdhStatus != ERROR_SUCCESS)
    {
        printf("PdhOpenQuery failed with status 0x%x\n", pdhStatus);
        return 1;
    }

    // 添加网络发送和接收速率的性能计数器
    PDH_HCOUNTER hCounterSent, hCounterReceived;
    pdhStatus = PdhAddCounter(hQuery, L"\Process(<进程名>)\IO Data Bytes/sec", NULL, &hCounterSent);
    if (pdhStatus != ERROR_SUCCESS)
    {
        printf("PdhAddCounter for sent failed with status 0x%x\n", pdhStatus);
        return 1;
    }
    pdhStatus = PdhAddCounter(hQuery, L"\Process(<进程名>)\IO Other Bytes/sec", NULL, &hCounterReceived);
    if (pdhStatus != ERROR_SUCCESS)
    {
        printf("PdhAddCounter for received failed with status 0x%x\n", pdhStatus);
        return 1;
    }

    // 收集性能数据
    pdhStatus = PdhCollectQueryData(hQuery);
    if (pdhStatus != ERROR_SUCCESS)
    {
        printf("PdhCollectQueryData failed with status 0x%x\n", pdhStatus);
        return 1;
    }

    // 获取性能计数器的值
    PDH_FMT_COUNTERVALUE counterValueSent, counterValueReceived;
    pdhStatus = PdhGetFormattedCounterValue(hCounterSent, PDH_FMT_DOUBLE, NULL, &counterValueSent);
    if (pdhStatus != ERROR_SUCCESS)
    {
        printf("PdhGetFormattedCounterValue for sent failed with status 0x%x\n", pdhStatus);
        return 1;
    }
    pdhStatus = PdhGetFormattedCounterValue(hCounterReceived, PDH_FMT_DOUBLE, NULL, &counterValueReceived);
    if (pdhStatus != ERROR_SUCCESS)
    {
        printf("PdhGetFormattedCounterValue for received failed with status 0x%x\n", pdhStatus);
        return 1;
    }

    // 输出结果
    double sentRate = counterValueSent.doubleValue;
    double receivedRate = counterValueReceived.doubleValue;
    printf("Sent Rate: %.2f bytes/sec\n", sentRate);
    printf("Received Rate: %.2f bytes/sec\n", receivedRate);

    // 关闭性能数据查询对象
    PdhCloseQuery(hQuery);

    return 0;
}

注意:以上代码仅为示例代码,需要将“<进程名>”替换为具体的进程名。另外,需要确保已经链接了 pdh.lib 库。

使用 PdhAddCounter 获取指定进程的网络发送和接收速率

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

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