Note: 1. Internal use of timer, automatic refresh, so that external use is very clear; 2. The main program must have a message loop
// ================================================ ================================//// Copyright (c) 2000-2004 iwise technologies, Co. ltd. // All Rights Reserved. /// product: iw988 // file: cpuusage. h // created: Tianyi seed /// Description: // valueadded main program for iw988. // contact: // waterpub@mail.csdn.net //// =============================================== ==================================#pragma once # define systembasicinformation 0 # define sys Temperformanceinformation 2 # define systemtimeinformation 3 # define li2double (x) (double) (X ). highpart) * 4.294967296e9 + (double) (X ). lowpart) Class ccpuusage {public: ccpuusage ();~ Ccpuusage (); Public: int getcpuusage (); // obtain the system CPU usage int setrefreshinterval (INT milli_sec); // regularly refresh the interval PRIVATE: // type definition typedef long (winapi * procntqsi) (uint, pvoid, ulong, Pulong); typedef struct {DWORD dwunknown1; ulong distance; ulong upagesize; ulong distance; ulong ummlowestphysicalpage; ulong ummhighestphysicalpage; ulong uallocationgranularity; pvoid plowestuseraddress; pvoid pmmhighest Useraddress; ulong timeout; byte bunknown2; Word wunknown3;} consumed; typedef struct {large_integer liidletime; DWORD dwspare [76];} consumed; typedef struct {large_integer likeboottime; large_integer likesystemtime; large_integer liexptimezonebias; ulong ucurrenttimezoneid; DWORD dwreserved;} system_time_information; // The variable defines system_perfo Rmance_information sysperfinfo; system_time_information systimeinfo; system_basic_information sysbaseinfo; double dbidletime; double dbsystemtime; long status; large_integer hour; procntqsi ntquerysysteminformation; int hour; // timed hwnd m_hwnd; int hour; // The default value is 1000 ms int m_ntimerid; private: static void calccpuusage (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, interval); destroywindow (m_hwnd);} inline void ccpuusage: calccpuusage (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) /numberofprocessorsdbidletime = 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 = policimeinfo. wait; // wait one second return 0;} inline int ccpuusage: getcpuusage () {return m_ncpuusage;} inline int ccpuusage: setrefreshinterval (INT milli_sec) {Limit = milli_sec; if (m_hwnd) {timerproc TP = (timerproc) calccpuusage; settimer (m_hwnd, m_ntimerid, m_nrefreshinterval, TP);} return 0 ;}