Platform SDK: Performance Monitoring

Transferring Data from a Perfmon-format Log File to a CSV-format 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("\nOne or more valid log names must be specified"));
   _tprintf (TEXT("\nas a command line argument. The input log"));
   _tprintf (TEXT("\nfile must be in the Perfmon file format. The"));
   _tprintf (TEXT("\noutput log file will be in the CSV file"));
   _tprintf (TEXT("\nformat."));
   _tprintf (TEXT("\nSyntax: convertlog <input file name>"));
   _tprintf (TEXT("          <output file name>"));
   return;
}

int __cdecl _tmain (int argc, TCHAR **argv)
{

   HQUERY          hQuery;
   HLOG            hOutputLog;
   PDH_STATUS      pdhStatus;
   BOOLEAN         bStatus = FALSE;
   DWORD           dwNumEntries;
   DWORD           dwInputLogType = PDH_LOG_TYPE_PERFMON;
   DWORD           dwOutputLogType = PDH_LOG_TYPE_CSV;
   PDH_TIME_INFO   TimeRange;
   DWORD           dwCount;
   DWORD           dwFormat = PDH_FMT_DOUBLE;
   PDH_FMT_COUNTERVALUE  *lpItemBuffer;
   HCOUNTER        hCounter;
   CHAR            szCounterPath[45] = 
                         TEXT("\\Processor(0)\\% Processor Time");
   DWORD           dwBufferSize;
   DWORD           dwFormattedDataSize = 0;

   if (argc != 3)
   {
      DisplayCommandLineHelp ();
      return -1;
   }

   // Open the query object and open the input log file.
   pdhStatus = PdhOpenQuery (argv[1],
                             0,
                             &hQuery);

   // Add the counter to the query object.
   pdhStatus = PdhAddCounter (hQuery,
                              szCounterPath,
                              0,
                              &hCounter);

   // Get the time range of the input log data.
   pdhStatus = PdhGetDataSourceTimeRange(argv[1],
                                         &dwNumEntries,
                                         &TimeRange,
                                         &dwBufferSize);

   // Create and open the output log file.
   pdhStatus = PdhOpenLog (argv[2], 
                           PDH_LOG_WRITE_ACCESS | 
                           PDH_LOG_CREATE_ALWAYS,
                           &dwOutputLogType,
                           hQuery,
                           0, 
                           NULL,
                           &hOutputLog);

   // Allocate space for the counter value.
   lpItemBuffer = (PDH_FMT_COUNTERVALUE *) GlobalAlloc (GPTR,
                             sizeof(PDH_FMT_COUNTERVALUE));

   // Transfer the values from the input log file to the output 
   //  log file.
   for (dwCount = 0; dwCount < TimeRange.SampleCount; dwCount++) { 
       pdhStatus = PdhUpdateLog (hOutputLog, 
                                 TEXT("This is a comment."));
   }

   // Close the output log file.
   pdhStatus = PdhCloseLog (hOutputLog, 0);

   // Close the query object and input log file.
   pdhStatus = PdhCloseQuery (hQuery);

   return 0;
}