PdhCollectQueryDataEx 函数 (pdh.h)

使用单独的线程收集指定查询中所有计数器的当前原始数据值。 然后,函数向应用程序定义的事件发出信号,并在返回之前等待指定的时间间隔。

语法

PDH_FUNCTION PdhCollectQueryDataEx(
  [in] PDH_HQUERY hQuery,
  [in] DWORD      dwIntervalTime,
  [in] HANDLE     hNewDataEvent
);

参数

[in] hQuery

查询的句柄。 查询标识要收集的计数器。 PdhOpenQuery 函数返回此句柄。

[in] dwIntervalTime

等待的时间间隔(以秒为单位)。

[in] hNewDataEvent

希望 PDH 在时间间隔过期后发出信号的事件的句柄。 若要创建事件对象,请调用 CreateEvent 函数。

返回值

如果函数成功,则返回ERROR_SUCCESS。

如果函数失败,则返回值为 系统错误代码PDH 错误代码。 下面是可能的值。

返回代码 说明
PDH_INVALID_HANDLE
查询句柄无效。
PDH_NO_DATA
查询当前没有任何计数器。

注解

调用 PdhCloseQuery 函数时,PDH 将终止线程。 如果多次调用 PdhCollectQueryDataEx ,则每个后续调用都会从上一次调用终止线程,然后启动一个新线程。

如果仅为一个计数器实例中的数据调用 PdhCollectQueryDataEx ,并且计数器实例不存在,则函数将返回PDH_NO_DATA。 但是,如果查询来自多个计数器的数据, PdhCollectQueryDataEx 可能会返回ERROR_SUCCESS,即使其中一个计数器实例尚不存在。 这是因为不知道指定的计数器实例是否不存在,或者它是否存在但尚未创建。 在这种情况下,请为每个感兴趣的计数器实例调用 PdhGetRawCounterValuePdhGetFormattedCounterValue ,以确定它们是否存在。

PDH 存储当前和上一个集合的原始计数器值。 如果要检索当前原始计数器值,请调用 PdhGetRawCounterValue 函数。 如果要计算计数器值的可显示值,请调用 PdhGetFormattedCounterValue。 如果计数器路径包含实例名称的通配符,请分别调用 PdhGetRawCounterArrayPdhGetFormattedCounterArray 函数。

示例

以下示例演示如何使用此函数。


#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <pdh.h>

#pragma comment(lib, "pdh.lib")

CONST PWSTR COUNTER_NAME    = L"\\Processor(0)\\% Processor Time";
CONST ULONG SAMPLE_COUNT    = 10;
CONST ULONG SAMPLE_INTERVAL = 2;

void wmain(void)
{
    PDH_STATUS Status;
    HANDLE Event = NULL;
    PDH_HQUERY Query = NULL;
    PDH_HCOUNTER Counter;
    ULONG WaitResult;
    ULONG CounterType;
    PDH_FMT_COUNTERVALUE DisplayValue;

    Status = PdhOpenQuery(NULL, 0, &Query);
    if (Status != ERROR_SUCCESS) 
    {
        wprintf(L"\nPdhOpenQuery failed with status 0x%x.", Status);
        goto Cleanup;
    }

    Status = PdhAddCounter(Query, COUNTER_NAME, 0, &Counter);
    if (Status != ERROR_SUCCESS) 
    {
        wprintf(L"\nPdhAddCounter failed with 0x%x.", Status);
        goto Cleanup;
    }

    //
    // Calculating the formatted value of some counters requires access to the
    // value of a previous sample. Make this call to get the first sample value
    // populated, to be used later for calculating the next sample.
    //

    Status = PdhCollectQueryData(Query);
    if (Status != ERROR_SUCCESS) 
    {
        wprintf(L"\nPdhCollectQueryData failed with status 0x%x.", Status);
        goto Cleanup;
    }

    //
    // This will create a separate thread that will collect raw counter data
    // every 2 seconds and set the supplied Event.
    //

    Event = CreateEvent(NULL, FALSE, FALSE, L"MyEvent");
    if (Event == NULL) 
    {
        wprintf(L"\nCreateEvent failed with status 0x%x.", GetLastError());
        goto Cleanup;
    }

    Status = PdhCollectQueryDataEx(Query, SAMPLE_INTERVAL, Event);
    if (Status != ERROR_SUCCESS) 
    {
        wprintf(L"\nPdhCollectQueryDataEx failed with status 0x%x.", Status);
        goto Cleanup;
    }

    //
    // Collect and format 10 samples, 2 seconds apart.
    //

    for (ULONG i = 0; i < SAMPLE_COUNT; i++) 
    {
        WaitResult = WaitForSingleObject(Event, INFINITE);

        if (WaitResult == WAIT_OBJECT_0) 
        {
            Status = PdhGetFormattedCounterValue(Counter, PDH_FMT_DOUBLE, &CounterType, &DisplayValue);

            if (Status == ERROR_SUCCESS) 
            {
                wprintf(L"\nCounter Value: %.20g", DisplayValue.doubleValue);
            } 
            else 
            {
                wprintf(L"\nPdhGetFormattedCounterValue failed with status 0x%x.", Status);
                goto Cleanup;
            }
        } 
        else if (WaitResult == WAIT_FAILED) 
        {
            wprintf(L"\nWaitForSingleObject failed with status 0x%x.", GetLastError());
            goto Cleanup;
        }
    }

Cleanup:

    if (Event) 
    {
        CloseHandle(Event);
    }

    //
    // This will close both the Query handle and all associated Counter handles
    // returned by PdhAddCounter.
    //

    if (Query) 
    {
        PdhCloseQuery(Query);
    }
}

要求

要求
最低受支持的客户端 Windows XP [仅限桌面应用]
最低受支持的服务器 Windows Server 2003 [仅限桌面应用]
目标平台 Windows
标头 pdh.h
Library Pdh.lib
DLL Pdh.dll

另请参阅

CreateEvent

PdhCollectQueryData

PdhOpenQuery