利用性能计数器pdh读取cpu,内存,磁盘性能

  1 /*
  2 通过性能计数器获取某个进程的CPU使用率、内存使用量、磁盘读写速率
  3 使用性能计数器中的Process实现
  4 */
  5 
  6 
  7 #pragma    comment(lib,"Pdh.lib")
  8 #include <stdio.h>
  9 #include <stdlib.h>
 10 #include <malloc.h>
 11 #include <locale.h>
 12 #include <Pdh.h>
 13 #include <string>
 14 #include <pdhmsg.h>
 15 #include <tchar.h>
 16 #include <windows.h>
 17 
 18 int nCPU;
 19 using std::string;
 20 using namespace std;
 21 int GetData()
 22 {
 23     HQUERY query;
 24     double dbVal;
 25     long iVal;
 26     //PDH_STATUS status = PdhOpenQuery(NULL, NULL, &query);
 27     PDH_STATUS status = PdhOpenQuery(0, 0, &query);
 28     if (ERROR_SUCCESS != status)
 29     {
 30         MessageBox(NULL, TEXT("打开失败"), TEXT(""), MB_OK);
 31         return -1;
 32 
 33     }
 34     
 35     HCOUNTER cntProcessCPU, cntProcessMemory;
 36     HCOUNTER cntProcessDiskRead, cntProcessDiskWrite;
 37     status = PdhAddCounter(query, "\\Process(_Total)\\% Processor Time", NULL, &cntProcessCPU);
 38     status = PdhAddCounter(query, "\\Process(_Total)\\Working Set - Private", NULL, &cntProcessMemory);
 39     status = PdhAddCounter(query, "\\Process(_Total)\\IO Read Bytes/sec", NULL, &cntProcessDiskRead);
 40     status = PdhAddCounter(query, "\\Process(_Total)\\IO Write Bytes/sec", NULL, &cntProcessDiskWrite);
 41     printf("111");
 42     
 43     if (ERROR_SUCCESS != status)
 44     {
 45         MessageBox(NULL, TEXT("添加失败"), TEXT(""), MB_OK);
 46         return -1;
 47     }
 48     
 49     status = PdhCollectQueryData(query);
 50     Sleep(500);                 //这里要有延时不然结果相当不准确
 51     status = PdhCollectQueryData(query);
 52     if (ERROR_SUCCESS != status)
 53     {
 54         MessageBox(NULL, TEXT("数据请求失败"), TEXT(""), MB_OK);
 55         return -1;
 56     }
 57 
 58     PDH_FMT_COUNTERVALUE pdhValue;
 59     DWORD dwValue;
 60 
 61     status = PdhGetFormattedCounterValue(cntProcessCPU, PDH_FMT_DOUBLE, &dwValue, &pdhValue);
 62     if (ERROR_SUCCESS != status)
 63     {
 64         MessageBox(NULL, TEXT("得到数据失败"), TEXT(""), MB_OK);
 65         return -1;
 66     }
 67     else
 68     {
 69         dbVal = pdhValue.doubleValue;
 70         printf("Process-CPU:          %3d%%          ", (int)(dbVal / nCPU + 0.5));
 71     }
 72 
 73     status = PdhGetFormattedCounterValue(cntProcessMemory, PDH_FMT_DOUBLE, &dwValue, &pdhValue);
 74     if (ERROR_SUCCESS != status)
 75     {
 76         MessageBox(NULL, TEXT("得到数据失败"), TEXT(""), MB_OK);
 77         return -1;
 78     }
 79     else
 80     {
 81         dbVal = pdhValue.doubleValue;
 82         printf("Process-Memory:     %8dK        \n", (int)(dbVal / 1024));
 83     }
 84 
 85     status = PdhGetFormattedCounterValue(cntProcessDiskRead, PDH_FMT_DOUBLE, &dwValue, &pdhValue);
 86     if (ERROR_SUCCESS != status)
 87     {
 88         MessageBox(NULL, TEXT("得到数据失败"), TEXT(""), MB_OK);
 89         return -1;
 90     }
 91     else
 92     {
 93         dbVal = pdhValue.doubleValue;
 94         printf("Process-DiskRead:%8dK/s        ", (int)(dbVal / 1024));
 95     }
 96 
 97     status = PdhGetFormattedCounterValue(cntProcessDiskWrite, PDH_FMT_DOUBLE, &dwValue, &pdhValue);
 98     if (ERROR_SUCCESS != status)
 99     {
100         MessageBox(NULL, TEXT("得到数据失败"), TEXT(""), MB_OK);
101         return -1;
102     }
103     else
104     {
105         dbVal = pdhValue.doubleValue;
106         printf("Process-DiskWrite:%8dK/s        \n*****************************************************************************\n", (int)(dbVal / 1024));
107     }
108 
109     PdhRemoveCounter(cntProcessCPU);
110     PdhRemoveCounter(cntProcessMemory);
111     PdhRemoveCounter(cntProcessDiskRead);
112     PdhRemoveCounter(cntProcessDiskWrite);
113     PdhCloseQuery(query);
114 }
115 
116 
117 int main() {
118     setlocale(LC_ALL, "chs");
119 
120     SYSTEM_INFO si;
121     GetSystemInfo(&si);
122     nCPU = si.dwNumberOfProcessors;
123     printf("hello");
124     while (1)
125     {
126         Sleep(500);
127         GetData();
128     }
129     system("pause");
130     return 0;
131 }

参考https://blog.csdn.net/fengsuinanhan/article/details/73799791

我的运行出现错误 显示得到数据失败

修改参数tfb_manage_system 为_total即可运行成功 

posted @ 2019-12-25 21:33  剑峰随心  阅读(1726)  评论(0编辑  收藏  举报