PerfMon
OS: WinXP Pro
Compiler: VC++6
SDK: Platform/latest
I have some code that uses PDH which, I've been using since '98 to access
and pull perf counters.. Recently, I pulled the code again because I needed
it for a project and I notices some of the numbers are skewed or possibly
cached.
My class has 4 methods..
1) OpenPerfMon
2) OpenPerfDialog
3) GetCounter
4) ClosePerfMon
Below is the GetCounter, but when I call:
\\\\127.0.0.1\\Network Interface(Broadcom NetXtreme 57xx Gigabit
Controller)\\Packets Received/sec
I get 12.xxxxxxx back.. I start downloading a large file and call it again,
I get the exact same number back. I opened PerfMon and put a watch on the
same counter and it moves, but when I call it from my code it doesn't..
What am I missing?
Here is the kicker, when I call: "\\\\127.0.0.1\\Memory\\Available Bytes" it
always seems to come back with the right number and changes each time I call
it. It seems all the Network Interface calles are all coming back with the
same number, no matter what the network card is doing. Yes, I have only 1
Network card and yes, that is the name of it.
===================================
double CPerfCounters::GetCounter(char* pstrCounter)
{
DWORD ctrType;
HRESULT hr = S_OK;
HCOUNTER *pCounterHandle;
PDH_FMT_COUNTERVALUE fmtValue;
// Allocate the counter handle array. Allocate room for
// one handle per command line arg, not including the
// executable file name.
pCounterHandle = (HCOUNTER *)GlobalAlloc(GPTR, sizeof(HCOUNTER));
pdhStatus = PdhAddCounter(hQuery, pstrCounter, 0, pCounterHandle);
if (pdhStatus != ERROR_SUCCESS)
{
throw(pdhStatus);
return -1;
}
// "Prime" counters that need two values to display a
// formatted value.
pdhStatus = PdhCollectQueryData (hQuery);
if (pdhStatus != ERROR_SUCCESS)
{
throw(pdhStatus);
return -1;
}
// Get the current value of this counter.
pdhStatus = PdhGetFormattedCounterValue (*pCounterHandle, PDH_FMT_DOUBLE,
&ctrType, &fmtValue);
if (pdhStatus != ERROR_SUCCESS)
{
throw(pdhStatus);
return -1;
}
return fmtValue.doubleValue;
}
===================================