Monitoring CPU Usage: PDH always reporting 0% or 99%

In attempting to monitor CPU usage, I have the following code:

///////////////////////////////////////////////////////////////////////
     HQUERY q;
     HCOUNTER pCnt;
     long cpuload = -1;
     if (!PdhOpenQuery(NULL,0,&q))  {
          printf("PdhAddCounter return value: %d\n", PdhAddCounter(q,"\\Processor(0)\\% Processor Time",1,&pCnt));
          printf("PdhCollectQueryData return value: %d\n", PdhCollectQueryData(q));
          PDH_FMT_COUNTERVALUE cpu;
          printf("PdhGetFormattedCounterValue return value: %d\n", PdhGetFormattedCounterValue(pCnt,PDH_FMT_LONG,NULL,&cpu));
          cpuload =  cpu.longValue;
     }

     PdhRemoveCounter(pCnt);
     PdhCloseQuery(q);

     printf("%d", cpuload);
///////////////////////////////////////////////////////////////////////

Running on Windows 2000, I always get 99 as my return value. On Windows NT, I always get 0.
I have also tried the following counter strings in place of "\\Processor(0)\\% Processor Time":
"\\System\\% Total Processor Time"
"\\Processor(_Total#0)\\% Processor Time"

If I use "\\Memory\\Available Bytes", I do get sensible return values for this, suggesting something is right in my code!

Any idea why I should be getting these return values regardless of the actual CPU activity? What am I doing wrong?

Many Thanks
Daniel
dscrivAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

andrewjbCommented:
Never used these, so this could be bogus....

Do you need to give some time between initialising the query, and collecting the data from it. At the moment you're doing these pretty damn close together. I don't know how often the counters get updated, but I could imaging 0 or 99% being the only answers you'll get, since either 1 'tick' occurs (and you're processing) or doesn't.

Maybe try shoving in a sleep(1000) just after the OpenQuery??
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bcladdCommented:
No comment has been added lately, so it's time to clean up this TA. I will
leave a recommendation in the Cleanup topic area that this question is:

Answered: Points to andrewjb: Grade B

Please leave any comments here within the next seven days.

Experts: Silence means you don't care. Grading recommendations are made in light
of the posted grading guidlines (http://www.experts-exchange.com/help.jsp#hi73).

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

-bcl (bcladd)
EE Cleanup Volunteer
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

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.