# Include <windows. h>
# Include <conio. h>
# Include <stdio. h>
# Define systembasicinformation 0
# Define systemperformanceinformation 2
# Define systemtimeinformation 3
# Define li2double (x) (double) (x). highpart) * 4.294967296e9 + (double) (x). lowpart ))
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;
// NTDLL! Ntquerysysteminformation (NT specific !)
//
// The function copies the system information of
// Specified type into a buffer
//
// Ntsysapi
// Ntstatus
// Ntapi
// Ntquerysysteminformation (
// In uint systeminformationclass, // information type
// Out pvoid systeminformation, // pointer to buffer
// In ulong systeminformationlength, // buffer size in bytes
// Out Pulong returnlength optional // pointer to a 32-bit
/// Variable that generated es
/// The number of bytes
/// Written to the buffer
//);
Typedef long (winapi * procntqsi) (uint, pvoid, ulong, Pulong );
Procntqsi ntquerysysteminformation;
Void main (void)
{
System_performance_information sysperfinfo;
System_time_information extends imeinfo;
System_basic_information sysbaseinfo;
Double dbidletime;
Double dbsystemtime;
Long status;
Large_integer lioldidletime = {0, 0 };
Large_integer lioldsystemtime = {0, 0 };
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;
Printf ("/ncpu usage (press any key to exit)/n ");
While (! _ Kbhit ())
{
// Get new system time
Status = ntquerysysteminformation (systemtimeinformation, & policimeinfo, sizeof (policimeinfo), 0 );
If (status! = No_error)
Return;
// Get new CPU's idle time
Status = ntquerysysteminformation (systemperformanceinformation, & sysperfinfo, sizeof (sysperfinfo), null );
If (status! = No_error)
Return;
// 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;
Printf ("CPU usage (press any key to exit): % 3d %/N", (uint) dbidletime );
}
// Store new CPU's idle and system time
Lioldidletime = sysperfinfo. liidletime;
Lioldsystemtime = policimeinfo. likesystemtime;
// Wait one second
Sleep (1000 );
}
Printf ("/N ");
}