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.

http://technet.microsoft.com/en-gb/sysinternals/bb896653.aspx
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
http://technet.microsoft.com/en-us/library/cc749249.aspx

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
http://msdn.microsoft.com/en-us/library/windows/desktop/dd408124%28v=vs.85%29.aspx

System Monitor Reference
http://msdn.microsoft.com/en-us/library/windows/desktop/aa379757%28v=vs.85%29.aspx

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

Performance Counters
http://msdn.microsoft.com/en-us/library/windows/desktop/aa373083%28v=vs.85%29.aspx

Windows Management Instrumentation
http://msdn.microsoft.com/en-us/library/aa392397%28VS.85%29.aspx

Monitoring Performance Data
http://msdn.microsoft.com/en-us/library/aa392397%28VS.85%29.aspx

Andy
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.

Andy

How to gracefully terminate a process?
http://stackoverflow.com/questions/2055753/how-to-gracefully-terminate-a-process

TerminateProcess function
http://msdn.microsoft.com/en-us/library/windows/desktop/ms686714%28v=vs.85%29.aspx

AdjustTokenPrivileges function
http://msdn.microsoft.com/en-us/library/windows/desktop/aa375202%28v=vs.85%29.aspx
Last edited on
where could i get info on that?
where could i get info on that?

What's that ??

Andy
Wmi
Wmi

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

Except it's spelt in all-caps : WMI

Andy
Last edited on
Topic archived. No new replies allowed.