大网数据广告活动
性能与资源

Creating a Query

· 52CE 我爱测

To create a new query that collects performance data from a real-time source or log file, call the PdhOpenQuery function. The function returns a handle to the query that you use in subsequent PDH function calls.

To create a new query that collects performance data from a real-time source or log file, call the PdhOpenQuery function. The function returns a handle to the query that you use in subsequent PDH function calls.

After creating the query, call the PdhAddCounter function for each counter that you want to add to the query. You can use one of the following methods to provide the fully qualified counter path.

  • Define the counter path as a static string. Use this method if you always monitor the same counter, and if you are familiar with the correct syntax of a counter path. For information on the correct syntax used to specify a counter, see Specifying a Counter Path.
  • Initialize a PDH_COUNTER_PATH_ELEMENTS structure with the names of the computer, object, counter, and instance. Pass this structure to PdhMakeCounterPath which will return a counter path for the specified elements.
  • Specify a counter path that contains wildcard characters and call PdhExpandWildCardPath to get a list of counter names that match the wildcard characters in the path. Scan through the list of counter names and add to the query the counters that you want from this list.
  • Call the PdhBrowseCounters function to display a dialog box that lets the user browse and select performance counters. For more information, see Browsing Counters.

Note that if a counter instance is specified that does not exist, PdhAddCounter does not report an error condition. Instead, it returns ERROR_SUCCESS. The reason for this behavior is that it is not known if a nonexistent counter instance has been specified or one that will exist but has not yet been created.

The missing counter instance will be reported by either PdhCollectQueryData, PdhGetRawCounterValue, or PdhGetFormattedCounterValue. When calling PdhCollectQueryData for one counter instance only, and the counter instance still does not exist, it is assumed that the counter instance will not exist and the function returns PDH_NO_DATA. However, if more than one counter is queried, PdhCollectQueryData may still return ERROR_SUCCESS even if one of the counter instances does not yet exist. In this case, call PdhGetRawCounterValue or PdhGetFormattedCounterValue for each of the counter instances of interest. If the instance does not exist when calling PdhGetRawCounterValue, the function returns ERROR_SUCCESS and sets the CStatus member of PDH_RAW_COUNTER to PDH_STATUS_NO_INSTANCE. If the instance does not exist when calling PdhGetFormattedCounterValue, the function returns PDH_INVALID_DATA and sets the CStatus member of the PDH_FMT_COUNTERVALUE to PDH_CSTATUS_NO_INSTANCE.

Note that the counter path specified in the PdhAddCounter function must be localized. The PdhAddEnglishCounter function provides a locale-neutral way to add performance counters to the query. This function is the recommended way to add locale-neutral counters to a query.

To remove a counter from a query, call the PdhRemoveCounter function.

After you finish collecting data for a query, call the PdhCloseQuery function to close the query and release all allocated system resources. PdhCloseQuery closes all counter handles associated with the query.

Source: Microsoft Learn / Windows Win32 documentation by Microsoft and contributors. Documentation licensed CC BY 4.0. Code samples are covered by the MIT License. Converted from Markdown, with links normalized and embedded media omitted.

资料来源与适用范围

资料来源
Microsoft Learn / Windows Win32 documentation
适用范围
Windows Win32 API · Source date 07/14/2025
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 4.0 (documentation), MIT (code samples)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。