title: Using pdh functions to get multiple performance counters
body: <p>Please have a look on the below code(taken from <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa373228%28v=vs.85%29.aspx" rel="nofollow">here</a>):</p>
  
  <pre><code>#include &lt;windows.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;pdh.h&gt;
  #include &lt;pdhmsg.h&gt;
  
  #pragma comment(lib, "pdh.lib")
  
  CONST PWSTR COUNTER_PATH    = L"\\Processor(0)\\% Processor Time";
  CONST ULONG SAMPLE_INTERVAL_MS = 1000;
  
  void DisplayCommandLineHelp(void)
  {
      wprintf(L"The command line must include a valid log file name.\n"); 
  }
  
  void wmain(int argc, WCHAR **argv)
  {
      HQUERY hQuery = NULL;
      HLOG hLog = NULL;
      PDH_STATUS pdhStatus;
      DWORD dwLogType = PDH_LOG_TYPE_CSV;
      HCOUNTER hCounter;
      DWORD dwCount;
  
      if (argc != 2) 
      {
          DisplayCommandLineHelp();
          goto cleanup;
      }
  
      // Open a query object.
      pdhStatus = PdhOpenQuery(NULL, 0, &amp;hQuery);
  
      if (pdhStatus != ERROR_SUCCESS)
      {
          wprintf(L"PdhOpenQuery failed with 0x%x\n", pdhStatus);
          goto cleanup;
      }
  
      // Add one counter that will provide the data.
      pdhStatus = PdhAddCounter(hQuery,
          COUNTER_PATH,
          0,
          &amp;hCounter);
  
      if (pdhStatus != ERROR_SUCCESS)
      {
          wprintf(L"PdhAddCounter failed with 0x%x\n", pdhStatus);
          goto cleanup;
      }
  
      // Open the log file for write access.
      pdhStatus = PdhOpenLog(argv[1], 
          PDH_LOG_WRITE_ACCESS | PDH_LOG_CREATE_ALWAYS,
          &amp;dwLogType,
          hQuery,
          0, 
          NULL,
          &amp;hLog);
  
      if (pdhStatus != ERROR_SUCCESS)
      {
          wprintf(L"PdhOpenLog failed with 0x%x\n", pdhStatus);
          goto cleanup;
      }
  
      // Write 10 records to the log file.
      for (dwCount = 0; dwCount &lt; 10; dwCount++) 
      {
          wprintf(L"Writing record %d\n", dwCount);
  
          pdhStatus = PdhUpdateLog (hLog, NULL);
          if (ERROR_SUCCESS != pdhStatus)
          {
              wprintf(L"PdhUpdateLog failed with 0x%x\n", pdhStatus);
              goto cleanup;
          }
  
          // Wait one second between samples for a counter update.
          Sleep(SAMPLE_INTERVAL_MS); 
      }
  
  cleanup:
  
      // Close the log file.
      if (hLog)
          PdhCloseLog (hLog, 0);
  
      // Close the query object.
      if (hQuery)
          PdhCloseQuery (hQuery);
  }
  </code></pre>
  
  <p>How do I add more counters to collect data, say 10 counters?</p>
  
  <p>If I do it this way,</p>
  
  <pre><code>CONST PWSTR COUNTER_PATH    = L"\\Processor(0)\\% Processor Time";
  CONST PWSTR COUNTER_PATH1    = L"\\Processor\\% Processor Time";
  
  HQUERY hQuery = NULL;
  HQUERY hQuery1 = NULL;
  
  pdhStatus = PdhOpenQuery(NULL, 0, &amp;hQuery);
  pdhStatus1 = PdhOpenQuery(NULL, 0, &amp;hQuery1);
  </code></pre>
  
  <p>etc for each counter. This will be tedious as I will need to make changes to the whole code accordingly. Can you please suggest any other way out ?</p>