要获取某个 exe 的网络发送与接收速率,可以使用 Windows API 中的性能计数器来实现。下面是一个示例代码,可以获取指定进程的网络发送与接收速率:

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

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

// 获取指定进程的网络发送与接收速率
void GetProcessNetworkSpeed(DWORD processId, PDH_HQUERY& query, PDH_HCOUNTER& sendCounter, PDH_HCOUNTER& recvCounter)
{
    PDH_STATUS status;

    // 打开性能计数器查询
    status = PdhOpenQuery(NULL, 0, &query);
    if (status != ERROR_SUCCESS)
    {
        std::cout << "PdhOpenQuery failed with status " << status << std::endl;
        return;
    }

    // 添加发送速率性能计数器
    status = PdhAddCounter(query, L"\Network Interface(*)\Bytes Sent/sec", 0, &sendCounter);
    if (status != ERROR_SUCCESS)
    {
        std::cout << "PdhAddCounter (Bytes Sent/sec) failed with status " << status << std::endl;
        return;
    }

    // 添加接收速率性能计数器
    status = PdhAddCounter(query, L"\Network Interface(*)\Bytes Received/sec", 0, &recvCounter);
    if (status != ERROR_SUCCESS)
    {
        std::cout << "PdhAddCounter (Bytes Received/sec) failed with status " << status << std::endl;
        return;
    }

    // 设置查询更新频率
    status = PdhSetQueryTimeRange(query, NULL, NULL);
    if (status != ERROR_SUCCESS)
    {
        std::cout << "PdhSetQueryTimeRange failed with status " << status << std::endl;
        return;
    }

    // 关联查询与进程
    status = PdhCollectQueryData(query);
    if (status != ERROR_SUCCESS)
    {
        std::cout << "PdhCollectQueryData failed with status " << status << std::endl;
        return;
    }

    // 找到指定进程的实例名称
    WCHAR instanceName[256];
    DWORD instanceNameSize = sizeof(instanceName);
    status = PdhLookupPerfNameByIndex(NULL, processId, instanceName, &instanceNameSize);
    if (status != ERROR_SUCCESS)
    {
        std::cout << "PdhLookupPerfNameByIndex failed with status " << status << std::endl;
        return;
    }

    // 设置性能计数器实例
    status = PdhSetCounterSetInstance(sendCounter, instanceName);
    if (status != ERROR_SUCCESS)
    {
        std::cout << "PdhSetCounterSetInstance (sendCounter) failed with status " << status << std::endl;
        return;
    }

    status = PdhSetCounterSetInstance(recvCounter, instanceName);
    if (status != ERROR_SUCCESS)
    {
        std::cout << "PdhSetCounterSetInstance (recvCounter) failed with status " << status << std::endl;
        return;
    }
}

int main()
{
    // 获取指定进程的ID
    DWORD processId = 1234; // 替换为你要监测的进程ID

    // 初始化性能计数器
    PDH_HQUERY query;
    PDH_HCOUNTER sendCounter, recvCounter;
    GetProcessNetworkSpeed(processId, query, sendCounter, recvCounter);

    // 循环获取并输出网络发送与接收速率
    PDH_FMT_COUNTERVALUE counterValue;
    while (true)
    {
        // 更新查询数据
        PDH_STATUS status = PdhCollectQueryData(query);
        if (status != ERROR_SUCCESS)
        {
            std::cout << "PdhCollectQueryData failed with status " << status << std::endl;
            break;
        }

        // 获取发送速率
        status = PdhGetFormattedCounterValue(sendCounter, PDH_FMT_DOUBLE, NULL, &counterValue);
        if (status == ERROR_SUCCESS)
        {
            std::cout << "Send speed: " << counterValue.doubleValue << " bytes/sec" << std::endl;
        }
        else
        {
            std::cout << "PdhGetFormattedCounterValue (sendCounter) failed with status " << status << std::endl;
            break;
        }

        // 获取接收速率
        status = PdhGetFormattedCounterValue(recvCounter, PDH_FMT_DOUBLE, NULL, &counterValue);
        if (status == ERROR_SUCCESS)
        {
            std::cout << "Recv speed: " << counterValue.doubleValue << " bytes/sec" << std::endl;
        }
        else
        {
            std::cout << "PdhGetFormattedCounterValue (recvCounter) failed with status " << status << std::endl;
            break;
        }

        // 延时一段时间
        Sleep(1000);
    }

    // 关闭性能计数器
    PdhCloseQuery(query);

    return 0;
}

请注意,上述代码需要链接pdh.lib库。

C++ 获取指定 EXE 进程网络发送接收速率

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

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