Lists the CPU usage of all processes)
Typedef struct _ thread_info
{
Large_integer createtime;
DWORD dwunknown1;
DWORD dwstartaddress;
DWORD starteip;
DWORD dwownerpid;
DWORD dwthreadid;
DWORD dwcurrentpriority;
DWORD dwbasepriority;
DWORD dwcontextswitches;
DWORD unknown;
DWORD waitreason;
} Threadinfo, * pthreadinfo;
Typedef struct _ unicode_string
{
Ushort length;
Ushort maxlength;
Pwstr buffer;
} Unicode_string;
Typedef struct _ process_info
{
DWORD dwoffset;
DWORD dwthreadscount;
DWORD dwunused1 [6];
Large_integer createtime;
Large_integer usertime;
Large_integer kerneltime;
Unicode_string processname;
DWORD dwbasepriority;
DWORD dwprocessid;
DWORD dwparentprocessid;
DWORD dwhandlecount;
DWORD dwunused3 [2];
DWORD dwvirtualbytespeak;
DWORD dwvirtualbytes;
Ulong dwpagefaults;
DWORD dwworkingsetpeak;
DWORD dwworkingset;
DWORD dwquotapeakpagedpoolusage;
DWORD dwquotapagedpoolusage;
DWORD dwquotapeaknonpagedpoolusage;
DWORD dwquotanonpagedpoolusage;
DWORD dwpagefileusage;
DWORD dwpagefileusagepeak;
DWORD dcommitcharge;
Threadinfo threadsysinfo [1];
} Processinfo, * pprocessinfo;
// Query every second
Void _ fastcall tform1: timer1timer (tobject * sender)
{
Button2click (null );
}
//---------------------------------------------------------------------------
Void _ fastcall tform1: button2click (tobject * sender)
{
Pvoid pprocinfo = NULL;
DWORD dwinfosize = 0x20000;
Pprocessinfo;
DWORD dwworkingset;
Long (_ stdcall * ntquerysysteminformation) (DWORD, pvoid, DWORD, DWORD );
Static _ int64 lasttotalprocesscpuusage = 0;
Static _ int64 lastcurrentprocesscpuusage = 0;
Int currentdelta;
Int totaldelta;
_ Int64 totalprocesscpuusage = 0;
_ Int64 currentprocesscpuusage = 0;
/////////////////////////////////
Pprocinfo = (pvoid) (new byte [dwinfosize]);
Ntquerysysteminformation = (long (_ stdcall *) (DWORD, pvoid, DWORD, DWORD ))
Getprocaddress (getmodulehandle ("NTDLL. dll"), "ntquerysysteminformation ");
Ntquerysysteminformation (5, pprocinfo, dwinfosize, 0 );
Pprocessinfo = (pprocessinfo) pprocinfo;
Do
{
Totalprocesscpuusage + = (_ int64) pprocessinfo-> kerneltime. quadpart + (_ int64) pprocessinfo-> usertime. quadpart;
If (pprocessinfo-> dwprocessid = getcurrentprocessid ())
{
Dwworkingset = pprocessinfo-> dwworkingset;
Currentprocesscpuusage + = (_ int64) pprocessinfo-> kerneltime. quadpart + (_ int64) pprocessinfo-> usertime. quadpart;
}
/////////
If (pprocessinfo-> dwoffset = 0)
{
Break;
}
Pprocessinfo = (pprocessinfo) (byte *) pprocessinfo + pprocessinfo-> dwoffset );
}
While (true );
Totaldelta = totalprocesscpuusage-lasttotalprocesscpuusage;
Currentdelta = currentprocesscpuusage-lastcurrentprocesscpuusage;
If (totaldelta! = 0)
This-> caption = "CPU =" + inttostr (100 * currentdelta/totaldelta) +
"Memory =" + inttostr (dwworkingset/1024) "K ";
Lasttotalprocesscpuusage = totalprocesscpuusage;
Lastcurrentprocesscpuusage = currentprocesscpuusage;
Delete [] pprocinfo;
}