Performance Counters Functi ...


PdhAddCounter Function

Adds the specified counter to the query.

Syntax

C++
PDH_STATUS PdhAddCounter(
  __in   PDH_HQUERY hQuery,
  __in   LPCTSTR szFullCounterPath,
  __in   DWORD_PTR dwUserData,
  __out  PDH_HCOUNTER *phCounter
);

Parameters

hQuery [in]

Handle to the query to which you want to add the counter. This handle is returned by the PdhOpenQuery function.

szFullCounterPath [in]

Null-terminated string that contains the counter path. For details on the format of a counter path, see Specifying a Counter Path. The maximum length of a counter path is PDH_MAX_COUNTER_PATH.

dwUserData [in]

User-defined value. This value becomes part of the counter information. To retrieve this value later, call the PdhGetCounterInfo function and access the dwUserData member of the PDH_COUNTER_INFO structure.

phCounter [out]

Handle to the counter that was added to the query. You may need to reference this handle in subsequent calls.

Return Value

Return ERROR_SUCCESS if the function succeeds.

If the function fails, the return value is a system error code or a PDH error code. The following are possible values.

Return codeDescription
PDH_CSTATUS_BAD_COUNTERNAME

The counter path could not be parsed or interpreted.

PDH_CSTATUS_NO_COUNTER

Unable to find the specified counter on the computer or in the log file.

PDH_CSTATUS_NO_COUNTERNAME

The counter path is empty.

PDH_CSTATUS_NO_MACHINE

The path did not contain a computer name, and the function was unable to retrieve the local computer name.

PDH_CSTATUS_NO_OBJECT

Unable to find the specified object on the computer or in the log file.

PDH_FUNCTION_NOT_FOUND

Unable to determine the calculation function to use for this counter.

PDH_INVALID_ARGUMENT

One or more arguments are not valid.

PDH_INVALID_HANDLE

The query handle is not valid.

PDH_MEMORY_ALLOCATION_FAILURE

Unable to allocate memory required to complete the function.

 

Remarks

If the counter path contains a wildcard character, all counter names matching the wildcard character are added to the query.

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

To remove the counter from the query, use the PdhRemoveCounter function.

Examples

For an example, see Browsing Performance Counters or Reading Performance Data from a Log File.

Requirements

Minimum supported clientWindows 2000 Professional
Minimum supported serverWindows 2000 Server
HeaderPdh.h
LibraryPdh.lib
DLLPdh.dll
Unicode and ANSI namesPdhAddCounterW (Unicode) and PdhAddCounterA (ANSI)

See Also

PdhAddEnglishCounter
PdhBrowseCounters
PdhMakeCounterPath
PdhOpenQuery
PdhRemoveCounter

Send comments about this topic to Microsoft

Build date: 11/12/2009

Tags :


Community Content

ibse
Counter path should be localized
The counter path specified in szFullCounterPath should be localized, or the function may fail on non-English versions of Windows.

This is mentioned in the remarks for PdhAddEnglishCounter (Vista+), but neither here nor in the documentation on Specifying a Counter Path.

For more information:

http://support.microsoft.com/?scid=kb%3Ben-us%3B287159&x=11&y=9

http://www.hardtoc.com/archives/88
Tags :

Page view tracker