獲取cpu占有率的c++類:windows系統cpu占有率,實時監視cpu占有率,對cpu使用率有要求的系統可以參考使用。
#pragma once
#define systembasicinformation 0
#define systemperformanceinformation 2
#define systemtimeinformation 3
#define li2double(x) ((double)((x).highpart) * 4.294967296e9 + (double)((x).lowpart))
class ccpuusage
public:
ccpuusage();
~ccpuusage();
public:
int getcpuusage(); //得到系統cpu利用率
int setrefreshinterval(int milli_sec); //定時重新整理間隔
private:
//型別定義
typedef long (winapi *procntqsi)(uint,pvoid,ulong,pulong);
typedef struct
dword dwunknown1;
ulong ukemaximumincrement;
ulong upagesize;
ulong ummnumberofphysicalpages;
ulong ummlowestphysicalpage;
ulong ummhighestphysicalpage;
ulong uallocationgranularity;
pvoid plowestuseraddress;
pvoid pmmhighestuseraddress;
ulong ukeactiveprocessors;
byte bkenumberprocessors;
byte bunknown2;
word wunknown3;
} system_basic_information;
typedef struct
large_integer liidletime;
dword dwspare[76];
} system_performance_information;
typedef struct
large_integer likeboottime;
large_integer likesystemtime;
large_integer liexptimezonebias;
ulong ucurrenttimezoneid;
dword dwreserved;
} system_time_information;
//變數定義
system_performance_information sysperfinfo;
system_time_information systimeinfo;
system_basic_information sysbaseinfo;
double dbidletime;
double dbsystemtime;
long status;
large_integer lioldidletime;
large_integer lioldsystemtime;
procntqsi ntquerysysteminformation;
int m_ncpuusage;
//定時
hwnd m_hwnd;
int m_nrefreshinterval;//預設為1000毫秒
int m_ntimerid;
private:
static void calccpuusage(hwnd hwnd, uint umsg, uint_ptr idevent, dword dwtime);
int ontimer();
inline ccpuusage::ccpuusage()
m_hwnd = null;
m_nrefreshinterval = 1000;
m_ntimerid = 1000;
m_ncpuusage = 0;
memset(&lioldidletime , 0, sizeof(large_integer));
memset(&lioldsystemtime, 0, sizeof(large_integer));
ntquerysysteminformation = (procntqsi)getprocaddress(
getmodulehandle("ntdll") , "ntquerysysteminformation");
if (!ntquerysysteminformation)
return;
// get number of processors in the system
status = ntquerysysteminformation(systembasicinformation,&sysbaseinfo,sizeof(sysbaseinfo),null);
if (status != no_error)
return;
// create control for timer
m_hwnd = ::createwindow("static", "", 0, 0, 0, 0, 0, null, null, 0, null);
::setwindowlong(m_hwnd , gwl_userdata , (long)(this) );
timerproc tp = (timerproc)calccpuusage;
settimer(m_hwnd , m_ntimerid, m_nrefreshinterval, tp);
inline ccpuusage::~ccpuusage()
killtimer(m_hwnd , m_ntimerid);
destroywindow(m_hwnd);
inline void ccpuusage::calccpuusage(
hwnd hwnd,
uint umsg,
uint_ptr idevent,
dword dwtime
ccpuusage* pcpu = (ccpuusage*)::getwindowlong(hwnd , gwl_userdata);
if ( pcpu )
pcpu->ontimer();
inline int ccpuusage::ontimer()
status = ntquerysysteminformation(systemtimeinformation,&systimeinfo,sizeof(systimeinfo),0);
if (status!=no_error)
return 0;
// get new cpu's idle time
status = ntquerysysteminformation(systemperformanceinformation,&sysperfinfo,sizeof(sysperfinfo),null);
if (status != no_error)
return 0;
// if it's a first call - skip it
if (lioldidletime.quadpart != 0)
// currentvalue = newvalue - oldvalue
dbidletime = li2double(sysperfinfo.liidletime) - li2double(lioldidletime);
dbsystemtime = li2double(systimeinfo.likesystemtime) - li2double(lioldsystemtime);
// currentcpuidle = idletime / systemtime
dbidletime = dbidletime / dbsystemtime;
// currentcpuusage% = 100 - (currentcpuidle * 100) / numberofprocessors
dbidletime = 100.0 - dbidletime * 100.0 / (double)sysbaseinfo.bkenumberprocessors + 0.5;
m_ncpuusage = (uint)dbidletime;
// store new cpu's idle and system time
lioldidletime = sysperfinfo.liidletime;
lioldsystemtime = systimeinfo.likesystemtime;
// wait one second
return 0;
inline int ccpuusage::getcpuusage()
return m_ncpuusage;
inline int ccpuusage::setrefreshinterval(int milli_sec)
m_nrefreshinterval = milli_sec;
if ( m_hwnd )
timerproc tp = (timerproc)calccpuusage;
settimer(m_hwnd, m_ntimerid, m_nrefreshinterval ,tp);
return 0;
(iwgh)
c 獲得cpu廠商 C 獲取程序CPU佔用率
核心 時間轉換 static int64 file time 2 utc const filetime ftime large integer li li.lowpart ftime dwlowdatetime li.highpart ftime dwhighdatetime return li.q...
C 獲得cpu個數
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!本文 cpp view plain copy print?測試cpu核心個數 if defined win32 defined win64 define linux include else define windows include endif...
C 獲取CPU資訊
include windows.h include iostream include string using namespace std 用來儲存資訊 dword deax dword debx dword decx dword dedx void execpuid dword veax 初始化c...