Getting CPU usage in C++, and close a program,( windows 7)

My computer often gets CPU spikes, and I want to use C++ to find out what is causing the problem the close it. I have looked around a bit and seen that system() might be good for closing something but I'm not sure how to, + i need to find out how to get info about CPU usage.

Ever hear about the task manager? It tells you cpu usage and you can close programs. System is also kind of bad to use ( also only for windows but so is task manager also I suppose. ) If you really want to make a program to mimic the task manager someone else will have to help you out with that :p
There's also Process Explorer, which gives a lot of information, including nice graphs of CPU, I/O and memory usage of a particular process. I think of it as a step up from task manager.
Last edited on
I know I could use Task Manager but i would like to automate it. Do you think it is to hard for a beginner to attempt?
I would assume so but you won't know till you try it ;P anything can be done if you put your mind to it and are willing to try even after failing.
Thanks for the support, do you know any good learning sources for c++?
There's also the Windows Performance Monitor MMC snap-in, if your talking about apps. Either launch it via the shell, or you can run it from a command console as perfmon.exe actually perfmon.msc (edit: more correct, for the modern version.)

The Performance Monitor snap-in (edit: to use its new name) is kind of a more souped up version of Task Manager which can save configurations, log output, etc.

Windows Performance Monitor

i would like to automate it.

What do you mean by automate it? For example, are you only interested in what your own app is up to??

You can control the Performance Monitor via the Automation interface explosed by sysmon.ocx.

System Monitor

System Monitor Reference

You can also get hold of the performance data using either the older Performance Counters mechanism, or the newer WMI API.

Performance Counters

Windows Management Instrumentation

Monitoring Performance Data

Last edited on
As far as I was aware he wants to actually create a program similar to the task manager where he can view cpu usages and close programs.
I think I may have worded it wrong, If a program gets over x% cpu it would close automatically, that is my goal.
Well, then you need to use the Performance Counters or the WMI API to monitor the processes, and then close them if they get out of hand.

To close an app gracefully, you should ask it politely to close, by posting it a WM_CLOSE message. Only if it ignores you should you use TerminateProcess.

You also have to have the right grant yourself the privilege to terminate the process before you can actually do so, using AdjustTokenPrivileges, etc. Which might be a bit of a problem.


How to gracefully terminate a process?

TerminateProcess function

AdjustTokenPrivileges function
Last edited on
where could i get info on that?
where could i get info on that?

What's that ??


One of the things I mentioned in an early post, along with a link.

Except it's spelt in all-caps : WMI

Last edited on
Topic archived. No new replies allowed.