常用PDH计数器语句使用汇总

Windows 下使用PDH 获取CPU 使用率,网络使用率,内存使用率等

一、概述

使用pdh,获取windows下CPU占用率、网络占用率、内存占用率、网络上下行速度等。

说明:本篇文章为在网上收集资料并写代码测试整理后形成的文档,在实际测试过程中同样遇到了很多问题,为方便以后使用,现整理如下。

1、CPU占用率

HQUERY query;

PDH_FMT_COUNTERVALUE pdhValue;

DWORD dwValue;

HCOUNTER cpuCounter;

PDH_STATUS status = PdhAddCounter(query,TEXT("\\Processor Information(_Total)\\% Processor Times"),NULL,&&cpuCounter);

status = PdhGetFormattedCounterValue(cpuCounter,PDH_FMT_DOUBLE,&dwValue,&pdhValue);

if(status==ERROR_SUCCESS)

       qDebug()  <<  "CPU占用率="  <<  pdhValue.doubleValue << "%";

2、内存占用率

status = PdhAddCounter(query,TEXT("\\PhysicalDisk(_Total)\\% Disk Time"),NULL,&&memCounter);

status = PdhGetFormattedCounterValue(memCounter,PDH_FMT_DOUBLE,&dwValue,&pdhValue);

if(status==ERROR_SUCCESS)

       qDebug()  <<  "内存占用率="  <<  pdhValue.doubleValue << "%";

3、可用内存

status = PdhAddCounter(query,TEXT("\\Memory\\Available MBytes"),NULL,&&memAvaCounter);

status = PdhGetFormattedCounterValue(memAvaCounter,PDH_FMT_LONG,&dwValue,&pdhValue);

if(status==ERROR_SUCCESS)

       qDebug()  <<  "可用内存="  <<  pdhValue.longValue << "MB";

4、磁盘读速度

status = PdhAddCounter(query,TEXT("\\PhysicalDisk(_Total)\\Disk Read Bytes/sec"),NULL,&&diskReadCounter);

status = PdhGetFormattedCounterValue(diskReadCounter,PDH_FMT_DOUBLE,&dwValue,&pdhValue);

if(status==ERROR_SUCCESS)

       qDebug()  <<  "磁盘读速度="  <<  pdhValue.doubleValue/1024.0 << "K/s";

5、磁盘写速度

status = PdhAddCounter(query,TEXT("\\PhysicalDisk(_Total)\\Disk Write Bytes/sec"),NULL,&&diskWriteCounter);

status = PdhGetFormattedCounterValue(diskWriteCounter,PDH_FMT_DOUBLE,&dwValue,&pdhValue);

if(status==ERROR_SUCCESS)

       qDebug()  <<  "磁盘写速度="  <<  pdhValue.doubleValue/1024.0 << "K/s";

  以下讲述网络相关资源信息  ///

说明:此处仅归类网络相关计数器信息,内部网卡信息(strInterface)获取可从下篇博文获取

6、网络接收速度(下载速度)

status = PdhAddCounter(query,(strInterface + L"Bytes Received/sec").c_str(),NULL,&&netRcvCounter);

status = PdhGetFormattedCounterValue(netRcvCounter,PDH_FMT_LARGE,&dwValue,&pdhValue);

if(status==ERROR_SUCCESS)

       qDebug()  <<  "每秒接收字节数="  <<  pdhValue.largeValue;

7、网络发送速度(上传速度)

status = PdhAddCounter(query,(strInterface + L"Bytes Sent/sec").c_str(),NULL,&&netSentCounter);

status = PdhGetFormattedCounterValue(netSentCounter,PDH_FMT_LARGE,&dwValue,&pdhValue);

if(status==ERROR_SUCCESS)

       qDebug()  <<  "每秒发送字节数="  <<  pdhValue.largeValue;

8、网络线路速度(网络带宽、链路速度)

status = PdhAddCounter(query,(strInterface + L"Current Bandwidth").c_str(),NULL,&&netBwCounter);

status = PdhGetFormattedCounterValue(netBwCounter,PDH_FMT_LARGE,&dwValue,&pdhBwValue);

if(status==ERROR_SUCCESS)

{

        qDebug()  <<  "网络带宽="  <<  pdhBwValue.largeValue;

         qDebug()  <<  "网卡:"  << QString::fromStdString(strGetNet) << ",当前链路速度=" << pdhBwValue.largeValue/1000/1000 << "Mbps";

}

9、网络速度(每间隔总字节数)(用于计算网络占用率)

status = PdhAddCounter(query,(strInterface + L"Bytes Total/sec").c_str(),NULL,&&netSpeedCounter);

status = PdhGetFormattedCounterValue(netSpeedCounter,PDH_FMT_DOUBLE,&dwValue,&pdhSpeedValue);

if(status==ERROR_SUCCESS)

{

        qDebug()  <<  "每秒总字节数="  <<  pdhValue.doubleValue;

        double dNeUseRate = (double)pdhSpeedValue.doubleValue*8/pdhBwValue.largeValue * 100.0;

         qDebug()  <<  "网卡:"  << QString::fromStdString(strGetNet) << ",网络使用率=" << dNeUseRate  << "%";

}

       

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
可以使用性能计数器来获取CPU使用率,步骤如下: 1. 导入头文件 "Windows.h" 和 "pdh.h"。 2. 定义变量: ```c HQUERY query; // 查询句柄 HCOUNTER counter; // 计数器句柄 PDH_STATUS status; // 状态 DWORD type; // 计数器类型 ``` 3. 初始化查询句柄和计数器句柄: ```c status = PdhOpenQuery(NULL, 0, &query); status = PdhAddCounter(query, L"\\Processor(_Total)\\% Processor Time", 0, &counter); status = PdhCollectQueryData(query); ``` 其中,第二个参数是要获取的计数器名称,这里的 "\_Total" 表示获取全部 CPU 的使用率。 4. 循环获取数据并输出: ```c DWORD value; while (true) { Sleep(1000); // 等待 1 秒 status = PdhCollectQueryData(query); status = PdhGetFormattedCounterValue(counter, PDH_FMT_LONG, &type, &value); printf("CPU Usage: %d%%\n", value); } ``` 其中,Sleep(1000) 表示每隔 1 秒获取一次数据,PdhGetFormattedCounterValue() 函数用来获取计数器值。 完整代码示例: ```c #include <Windows.h> #include <pdh.h> #include <stdio.h> #pragma comment(lib, "pdh.lib") int main() { HQUERY query; // 查询句柄 HCOUNTER counter; // 计数器句柄 PDH_STATUS status; // 状态 DWORD type; // 计数器类型 // 初始化查询句柄和计数器句柄 status = PdhOpenQuery(NULL, 0, &query); status = PdhAddCounter(query, L"\\Processor(_Total)\\% Processor Time", 0, &counter); status = PdhCollectQueryData(query); DWORD value; while (true) { Sleep(1000); // 等待 1 秒 status = PdhCollectQueryData(query); status = PdhGetFormattedCounterValue(counter, PDH_FMT_LONG, &type, &value); printf("CPU Usage: %d%%\n", value); } return 0; } ``` 注意:需要在项目属性中设置附加依赖项为 "pdh.lib",否则会出现链接错误。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值