#include <Windows.h>
#include <Winternl.h>
#include <stdio.h>
#include "GetCpuUsage.h"
BYTE g_NumOfProcessor = 0;
LARGE_INTEGER g_liOldIdleTime;
LARGE_INTEGER g_liOldSystemTime;
PROCNTQSI NtQuerySystemInformation;
int InitQuerySystem()
{
SYSTEM_BASIC_INFORMATION SysBaseInfo;
LONG status;
UINT iCpuUsage = 0;
NtQuerySystemInformation = (PROCNTQSI)GetProcAddress(GetModuleHandleA("ntdll"),
"NtQuerySystemInformation");
if (!NtQuerySystemInformation)
{
return -1;
}
// get number of processors in the system
status = NtQuerySystemInformation(SystemBasicInformation,
&SysBaseInfo,sizeof(SysBaseInfo),NULL);
if (status != NO_ERROR)
{
return -2;
}
g_NumOfProcessor = SysBaseInfo.bKeNumberProcessors;
if(GetCpuUsage(&iCpuUsage) != 0)
{
return -3;
}
return 0;
}
int GetCpuUsage(UINT *iUsages)
{
SYSTEM_PERFORMANCE_INFORMATION SysPerfInfo;
SYSTEM_TIME_INFORMATION SysTimeInfo;
double dbIdleTime;
double dbSystemTime;
LONG status;
status = NtQuerySystemInformation(SystemTimeInformation,
&SysTimeInfo,sizeof(SysTimeInfo),0);
if (status!=NO_ERROR)
{
return -1;
}
// get new CPU's idle time
status = NtQuerySystemInformation(SystemPerformanceInformation,
&SysPerfInfo,sizeof(SysPerfInfo),NULL);
if (status != NO_ERROR)
{
return -1;
}
// if it's a first call - skip it
if (g_liOldIdleTime.QuadPart != 0)
{
// CurrentValue = NewValue - OldValue
dbIdleTime = Li2Double(SysPerfInfo.liIdleTime) - Li2Double(g_liOldIdleTime);
dbSystemTime = Li2Double(SysTimeInfo.liKeSystemTime) - Li2Double(g_liOldSystemTime);
// CurrentCpuIdle = IdleTime / SystemTime
dbIdleTime = dbIdleTime / dbSystemTime;
// CurrentCpuUsage% = 100 - (CurrentCpuIdle * 100) / NumberOfProcessors
dbIdleTime = 100.0 - dbIdleTime * 100.0 / (double)g_NumOfProcessor + 0.5;
*iUsages = (UINT)dbIdleTime;
}
// store new CPU's idle and system time
g_liOldIdleTime = SysPerfInfo.liIdleTime;
g_liOldSystemTime = SysTimeInfo.liKeSystemTime;
return 0;
}
/*
{
CStatic * showratebox = (CStatic *)pParam;
NtQuerySystemInformation = (PROCNTQSI)GetProcAddress(GetModuleHandle("ntdll"), "NtQuerySystemInformation");
if (!NtQuerySystemInformation) return 0;
status = NtQuerySystemInformation(SystemBasicInformation,&SysBaseInfo,sizeof(SysBaseInfo),NULL);
if (status != NO_ERROR) return 0;
while(stopflag == FALSE)
{
status = NtQuerySystemInformation(SystemTimeInformation,&SysTimeInfo,sizeof(SysTimeInfo),0);
if (status!=NO_ERROR) return 0;
status = NtQuerySystemInformation(SystemPerformanceInformation,&SysPerfInfo,sizeof(SysPerfInfo),NULL);
if (status != NO_ERROR) return 0;
if (liOldIdleTime.QuadPart != 0)
{
dbIdleTime = Li2Double(SysPerfInfo.liIdleTime) - Li2Double(liOldIdleTime);
dbSystemTime = Li2Double(SysTimeInfo.liKeSystemTime) - Li2Double(liOldSystemTime);
dbIdleTime = dbIdleTime / dbSystemTime;
// CurrentCpuUsage% = 100 - (CurrentCpuIdle * 100) / NumberOfProcessors
dbIdleTime = 100.0 - dbIdleTime * 100.0 / (double)SysBaseInfo.bKeNumberProcessors + 0.5;
showrate.Format("%d%%",(UINT)dbIdleTime);
showratebox->SetWindowText(showrate);
m_HistogramCtrl.SetPos((UINT)dbIdleTime);
}
liOldIdleTime = SysPerfInfo.liIdleTime;
liOldSystemTime = SysTimeInfo.liKeSystemTime;
Sleep(1000);
}
return 0;
}
*/
获取CPU使用率的C代码
3星 · 超过75%的资源 需积分: 9 44 浏览量
2009-11-25
15:01:41
上传
评论
收藏 2KB ZIP 举报
LeonBlizzard
- 粉丝: 1
- 资源: 1
最新资源
- Windows 常见运行运行库32+64
- 基于3KW光伏并网单相逆变器设计(TMS320F28035控制板+显示板+STM32F103功率板)硬件(原理图+PCB)工程
- 正点原子HAL库 STM32F4 外部中断(学习自用附源码)
- delphi rzcombobox DropDownList 灰色背景改为白色
- sap sd.docsap sd.doc
- torch-1.10.2-cp38-cp38-win-amd64.whl
- 菜单栏实现增加数据,修改数据,查询数据,删除数据
- 全国省市区三级联动json文件,带code
- C8_全局&局部&static.zip
- Unity和安卓交互插件Unity调Android Native Goodies PRO
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
前往页