Get Process List with their current CPU utilization percentage

For a Win32 C++ console application, I need to be able to enumerate the list of processes and get a CPU utilization percentage for each process, similar to what appears in the task manager.

I can't use .NET
I can't use Delphi (for some reason so many examples are for delphi)
I can only use Win32 and C++.

How do I achieve that?
ronenmagid1Asked:
Who is Participating?
 
pgnatyukConnect With a Mentor Commented:
Read the article. There is the code you need. Download it. CpuUsage class has GetUsage method. Take a look.



0
 
pgnatyukCommented:
You can take this program from MSDN:
http://msdn.microsoft.com/en-us/library/ms682623(VS.85).asp
This is a Win32 console application.
0
 
ronenmagid1Author Commented:
thanks pgnatyuk, this is to enumerate processes, anything on getting their current CPU utilization % snapshot?
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
pgnatyukCommented:
Add GetProcessTimes to the previous example:
http://msdn.microsoft.com/en-us/library/ms683223

0
 
pgnatyukCommented:
I think everything's explained in this article:
Determine CPU usage of current process (C++ and C#)
http://www.philosophicalgeek.com/2009/01/03/determine-cpu-usage-of-current-process-c-and-c/

0
 
ronenmagid1Author Commented:
One thing that's unclear about GetProcessTimes is the timespan. This seems like a statistical tool that can tell me,

"your application has been up for 5 weeks"
"your application used 1 week of CPU time"

Therefore I can deduce that overall, my process took 20% of the CPU during its lifetime.

however, what if I need a more "real time" monitoring? what if an app that took 1% over two weeks suddenly peaks to 99% CPU? how will I catch that in real time like TASK MANAGER does? This is really what I'm interested in, not over-time statistics but a rather "current" snapshot, based on the last x seconds.

Is there anything like that I can already use? or do I simply have to track the process for those X seconds and, monitor the elapsed time, and "sample" its CPU usage myself using deltas?

I mean, I can do this myself, but was wondering whether there's a ready made value I can pick up somewhere.
0
 
pgnatyukCommented:
Another approach (performance counters):
http://www.codeproject.com/KB/system/cpuusageByDudiAvramov.aspx
0
 
pgnatyukCommented:
Windows SysInternals. Process Explorer v12.04
http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.