C++ 使用 PdhAddCounter 获取特定进程网络发送和接收速率
C++ 使用 PdhAddCounter 获取特定进程网络发送和接收速率
本文提供了一个 C++ 示例代码,展示如何使用 PdhAddCounter 函数获取特定进程的网络发送和接收速率。cpp#include <Windows.h>#include <Pdh.h>#include
#pragma comment(lib, 'Pdh.lib')
int main(){ PDH_STATUS pdhStatus; PDH_HQUERY query; PDH_HCOUNTER sendCounter, recvCounter; DWORD processId = 1234; // 替换为特定进程的ID wchar_t instanceName[MAX_PATH];
// 获取进程实例名称 if (!PdhLookupPerfNameByIndex(NULL, processId, instanceName)) { std::cout << 'Failed to lookup instance name for process ID: ' << processId << std::endl; return 1; }
// 初始化查询 pdhStatus = PdhOpenQuery(NULL, 0, &query); if (pdhStatus != ERROR_SUCCESS) { std::cout << 'Failed to open query. Error code: ' << pdhStatus << std::endl; return 1; }
// 添加发送速率计数器 wchar_t sendCounterPath[MAX_PATH]; swprintf_s(sendCounterPath, L'\Process(%s)\IO Data Bytes/sec', instanceName); pdhStatus = PdhAddCounter(query, sendCounterPath, 0, &sendCounter); if (pdhStatus != ERROR_SUCCESS) { std::cout << 'Failed to add send counter. Error code: ' << pdhStatus << std::endl; PdhCloseQuery(query); return 1; }
// 添加接收速率计数器 wchar_t recvCounterPath[MAX_PATH]; swprintf_s(recvCounterPath, L'\Process(%s)\IO Data Bytes/sec', instanceName); pdhStatus = PdhAddCounter(query, recvCounterPath, 0, &recvCounter); if (pdhStatus != ERROR_SUCCESS) { std::cout << 'Failed to add recv counter. Error code: ' << pdhStatus << std::endl; PdhCloseQuery(query); return 1; }
// 执行查询 pdhStatus = PdhCollectQueryData(query); if (pdhStatus != ERROR_SUCCESS) { std::cout << 'Failed to collect query data. Error code: ' << pdhStatus << std::endl; PdhCloseQuery(query); return 1; }
// 读取计数器值 PDH_FMT_COUNTERVALUE sendValue, recvValue; pdhStatus = PdhGetFormattedCounterValue(sendCounter, PDH_FMT_DOUBLE, NULL, &sendValue); if (pdhStatus != ERROR_SUCCESS) { std::cout << 'Failed to get send counter value. Error code: ' << pdhStatus << std::endl; PdhCloseQuery(query); return 1; }
pdhStatus = PdhGetFormattedCounterValue(recvCounter, PDH_FMT_DOUBLE, NULL, &recvValue); if (pdhStatus != ERROR_SUCCESS) { std::cout << 'Failed to get recv counter value. Error code: ' << pdhStatus << std::endl; PdhCloseQuery(query); return 1; }
// 输出发送和接收速率 std::cout << 'Send rate: ' << sendValue.doubleValue << ' bytes/sec' << std::endl; std::cout << 'Recv rate: ' << recvValue.doubleValue << ' bytes/sec' << std::endl;
// 清理资源 PdhRemoveCounter(sendCounter); PdhRemoveCounter(recvCounter); PdhCloseQuery(query);
return 0;}
代码说明:
- 包含头文件: 代码首先包含了必要的头文件
Windows.h、Pdh.h和iostream。2. 链接库: 使用#pragma comment(lib, 'Pdh.lib')链接Pdh.lib库。3. 获取进程实例名称: 使用PdhLookupPerfNameByIndex函数根据进程 ID 获取进程实例名称。4. 初始化查询: 使用PdhOpenQuery函数初始化一个性能计数器查询。5. 添加计数器: 使用PdhAddCounter函数添加网络发送和接收速率计数器。计数器路径使用\Process(%s)\IO Data Bytes/sec格式,其中%s替换为进程实例名称。6. 执行查询: 使用PdhCollectQueryData函数执行查询,获取计数器值。7. 读取计数器值: 使用PdhGetFormattedCounterValue函数读取格式化的计数器值。8. 输出结果: 将网络发送和接收速率输出到控制台。9. 清理资源: 使用PdhRemoveCounter和PdhCloseQuery函数清理资源。
注意:
- 将代码中的
processId变量替换为要监控的进程的实际 ID。* 代码输出的网络发送和接收速率单位为字节/秒。
希望这篇文章能够帮助您理解如何使用 PdhAddCounter 函数获取特定进程的网络发送和接收速率。
原文地址: https://www.cveoy.top/t/topic/fSxH 著作权归作者所有。请勿转载和采集!