Search code examples
c++windowscpucpu-usagepdh

Getting current cpu usage in c++/windows for particular process


I want to calculate current cpu usage for particular application in my code. I looked up on internet and found pdh library for windows. When I tried it I am getting overall cpu usage not cpu usage for one process.

PdhAddCounter(hquery, TEXT("\\Processor(_Total)\\% Processor Time"),0,&counter);

So what I do with this line to get cpu usage for particular process? I tried replacing _Total with process name(explorer). At that time I am getting 0 cpu usage. But I checked in resource monitor that opening many windows at a time increased cpu usage upto 20%. Still in log file cpu usage is showing 0.

Can anyone help me with this?

thanks in advance.


Solution

  • You can check this for example. Explained everything in that project. It will give memory based on process id(same way shown in task manager)

    Thanks, Darshan