Obtain the CPU usage of a process in the system

Source: Internet
Author: User

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.