MFC如何获取系统CPU使用率API
1、通过查阅msdn,可以知道GetSystemTimes函数可以获取系统的时间,可以获得系统(自开机以来)处于Kernel状态下面的CPU时间,以及系统处于User状态下的时间,以及Idle的时间.我们只用Kernel时间和User时间, 不用Idle时间.

3、简单老揉孟阼写个demo测试下,代码如下#include <stdio.h> #include <W足毂忍珩indows.h> #include <time.h> #include <tchar.h>__int64 DiffFileTime(FILETIME time1, FILETIME time2){__int64 a = time1.dwHighDateTime << 32 | time1.dwLowDateTime;__int64 b = time2.dwHighDateTime << 32 | time2.dwLowDateTime;return b - a;}int main(int argc, char* argv[]){double cpuuse;int idle, kernel, user;FILETIME idleTime1, idleTime2;FILETIME kernelTime1, kernelTime2;FILETIME userTime1, userTime2;do{GetSystemTimes(&idleTime1, &kernelTime1, &userTime1);Sleep(1000);GetSystemTimes(&idleTime2, &kernelTime2, &userTime2);idle = (int)DiffFileTime(idleTime1, idleTime2);kernel = (int)DiffFileTime(kernelTime1, kernelTime2);user = (int)DiffFileTime(userTime1, userTime2);if (kernel + user == 0)cpuuse = 0.0;elsecpuuse = abs((kernel + user - idle) * 100 / (kernel + user));//(总的时间-空闲时间)/总的时间=占用cpu的时间就是使用率printf("cpu useage: %01f!\n", cpuuse);} while (1);return 0;}

5、接下来,旅邯佤践进一步对代码进行优化,将获取cpu的封装成一个通用的接口,优化后代码如下#include <stdio.h> #include <Windows.h> #include <time.h> #include <tchar.h>__int64 DiffFileTime(FILETIME time1, FILETIME time2){ __int64 a = time1.dwHighDateTime << 32 | time1.dwLowDateTime; __int64 b = time2.dwHighDateTime << 32 | time2.dwLowDateTime; return b - a;}double GetCpuUsage(){ double cpuuse; int idle, kernel, user; FILETIME idleTime1, idleTime2; FILETIME kernelTime1, kernelTime2; FILETIME userTime1, userTime2; GetSystemTimes(&idleTime1, &kernelTime1, &userTime1); Sleep(1000); GetSystemTimes(&idleTime2, &kernelTime2, &userTime2); idle = (int)DiffFileTime(idleTime1, idleTime2); kernel = (int)DiffFileTime(kernelTime1, kernelTime2); user = (int)DiffFileTime(userTime1, userTime2); if (kernel + user == 0) cpuuse = 0.0; else cpuuse = abs((kernel + user - idle) * 100 / (kernel + user));//(总的时间-空闲时间)/总的时间=占用cpu的时间就是使用率 return cpuuse;}int main(int argc, char* argv[]){ double cpuusage = 0; do { cpuusage = GetCpuUsage(); printf("cpu useage: %01f!\n", cpuuse); } while (1); return 0;}

