Adding Counters to a Query
After you create a query, call the PdhAddCounter function to add a counter to the query. Call PdhAddCounter for each counter to be added to the query. You can use one of the following methods to provide PdhAddCounter with a fully qualified counter string.
- Define the counter path as a static string and pass it into PdhAddCounter. Use this method if you always monitor the same counter, and if you are familiar with the correct syntax of a counter path. Refer to Performance Objects and Counters for a description of this syntax. Keep in mind that you can specify wildcard characters in your counter string.
- Initialize a PDH_COUNTER_PATH_ELEMENTS structure with the names of the machine, object, counter, and instance, then pass this structure into a call to PdhMakeCounterPath. Given a counter name and the name of its object, PdhMakeCounterPath will return a string containing the specified performance object elements in the counter path syntax described in Performance Objects and Counters. Use this method if the syntax of the fully qualified counter path is not determined at run time.
- Call PdhExpandCounterPath to get a list of counter names that match the wildcard characters in the counter name you passed in. Then scan through the returned array of counter names and add the counters you want from this array with PdhAddCounter. PdhExpandWildCardPath can also be used for this purpose if you want to use a log file as the source of the counter name list, or if you have wildcard characters in the counter and instance names and want to expand either the counter or the instance name, or you want to expand the names of counters that reside on remote machines.
- Call the PdhBrowseCounters function to display the PDH browse dialog box. The dialog box allows the user to browse and select performance counters. For more information, refer to Browsing Counters.
If you're returning performance data from a log file, you can also define a time range within which the query collects performance data. For more information on how to set and get query-time ranges, refer to the following sections.