Reading Performance Data from a Log File
#define UNICODE
#define _UNICODE
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <pdh.h>
#include <pdhmsg.h>
void
DisplayCommandLineHelp ()
{
_tprintf (TEXT("\nA valid log name must be specified as the"));
_tprintf (TEXT("\ncommand line argument. The log file must be"));
_tprintf (TEXT("\nin the CSV file format."));
return;
}
int __cdecl _tmain (int argc, TCHAR **argv)
{
HQUERY hQuery;
PDH_STATUS pdhStatus;
DWORD dwFormat = PDH_FMT_DOUBLE;
PDH_FMT_COUNTERVALUE *lpItemBuffer;
HCOUNTER hCounter;
CHAR szCounterPath[45] =
TEXT("\\Processor(0)\\% Processor Time");
if (argc != 2) {
DisplayCommandLineHelp ();
return -1;
}
// Open a query object.
pdhStatus = PdhOpenQuery (argv[1],
0,
&hQuery);
// Add the counter that created the data in the log file.
pdhStatus = PdhAddCounter (hQuery,
szCounterPath,
0,
&hCounter);
// Allocate the counter value structure.
lpItemBuffer = (PDH_FMT_COUNTERVALUE *) GlobalAlloc
(GPTR, sizeof(PDH_FMT_COUNTERVALUE));
// Read the performance data records.
pdhStatus = PdhCollectQueryData(hQuery);
while (pdhStatus == ERROR_SUCCESS) {
// Format the performance data record.
pdhStatus = PdhGetFormattedCounterValue (hCounter,
dwFormat,
(LPDWORD)NULL,
lpItemBuffer);
// Print the performance data record.
_tprintf(TEXT("\nLog Record Value = %4.8f\n"),
lpItemBuffer->doubleValue);
// Read the next record
pdhStatus = PdhCollectQueryData(hQuery);
}
// Close the query and the log file.
pdhStatus = PdhCloseQuery(hQuery);
return 0;
}