Browsing Performance Counters

Performance Counters
Browsing Performance Counters

The following example shows how to call PdhBrowseCounters to browse performance counters. The example also shows how to collect and format raw counter data for display.

 

 

#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#include <pdh.h>
#include <pdhmsg.h>

#define SAMPLE_INTERVAL_MS  1000

long __cdecl _tmain(int argc, TCHAR **argv)
{

   HQUERY          hQuery;
   HCOUNTER        hCounter;
   PDH_STATUS      pdhStatus;
   PDH_FMT_COUNTERVALUE   fmtValue;
   DWORD           ctrType;
   SYSTEMTIME      stSampleTime;
   PDH_BROWSE_DLG_CONFIG  BrowseDlgData;
   CHAR            szPathBuffer[PDH_MAX_COUNTER_PATH];

   // Create a query.
   pdhStatus = PdhOpenQuery(NULL, NULL, &hQuery);
   if (ERROR_SUCCESS != pdhStatus)
   {
      _tprintf(TEXT("PdhOpenQuery failed with %ld./n"), pdhStatus);
      goto cleanup;
   }

   ZeroMemory(&szPathBuffer, sizeof(szPathBuffer));

   // Initialize the browser dialog window settings.
   ZeroMemory(&BrowseDlgData, sizeof(PDH_BROWSE_DLG_CONFIG));
   BrowseDlgData.bIncludeInstanceIndex = FALSE;   
   BrowseDlgData.bSingleCounterPerAdd = TRUE;
   BrowseDlgData.bSingleCounterPerDialog = TRUE;  
   BrowseDlgData.bLocalCountersOnly = FALSE;      
   BrowseDlgData.bWildCardInstances = TRUE;
   BrowseDlgData.bHideDetailBox = TRUE;
   BrowseDlgData.bInitializePath = FALSE;     
   BrowseDlgData.bDisableMachineSelection = FALSE;
   BrowseDlgData.bIncludeCostlyObjects = FALSE;
   BrowseDlgData.bShowObjectBrowser = FALSE;
   BrowseDlgData.hWndOwner = NULL;   
   BrowseDlgData.szReturnPathBuffer = szPathBuffer;
   BrowseDlgData.cchReturnPathLength = sizeof(szPathBuffer);
   BrowseDlgData.pCallBack = NULL;   
   BrowseDlgData.dwCallBackArg = 0;
   BrowseDlgData.CallBackStatus = ERROR_SUCCESS;
   BrowseDlgData.dwDefaultDetailLevel = PERF_DETAIL_WIZARD;
   BrowseDlgData.szDialogBoxCaption = "Select a counter to monitor.";

   // Display the counter browser window. The dialog is configured
   // to return a single selection from the counter list.
   pdhStatus = PdhBrowseCounters (&BrowseDlgData);
   if (ERROR_SUCCESS != pdhStatus)
   {
      if (PDH_DIALOG_CANCELLED != pdhStatus)
      {
         _tprintf(TEXT("PdhBrowseCounters failed with %ld./n"), pdhStatus);
      }

      goto cleanup;
   }

   // Add the selected counter to the query.
   pdhStatus = PdhAddCounter (hQuery,
                              szPathBuffer, 
                              0, 
                              hCounter);
   if (ERROR_SUCCESS != pdhStatus)
   {
      _tprintf(TEXT("PdhBrowseCounters failed with %ld./n"), pdhStatus);
      goto cleanup;
   }

   // Most counter require two sample values to display a formatted value.
   // PDH stores the current sample value and the previously collected
   // sample value. This call retrieves the first value that will be used
   // by PdhGetFormattedCounterValueand in the first iteration of the loop
   // Note that this value is lost if the counter does not require two
   // values to compute a displayable value.
   pdhStatus = PdhCollectQueryData (hQuery);
   if (ERROR_SUCCESS != pdhStatus)
   {
      _tprintf(TEXT("PdhCollectQueryData failed with %ld./n"), pdhStatus);
      goto cleanup;
   }

   // Print counter values until a key is pressed.
   while (!_kbhit()) {

     // Wait one interval.
     Sleep(SAMPLE_INTERVAL_MS);

     // Get the sample time.
     GetLocalTime (&stSampleTime);

     // Get the current data value.
     pdhStatus = PdhCollectQueryData (hQuery);

     // Print the time stamp for the sample.
     _tprintf (
           TEXT("/n/"%2.2d/%2.2d/%4.4d %2.2d:%2.2d:%2.2d.%3.3d/""),
               stSampleTime.wMonth, 
               stSampleTime.wDay, 
               stSampleTime.wYear,
               stSampleTime.wHour, 
               stSampleTime.wMinute, 
               stSampleTime.wSecond,
               stSampleTime.wMilliseconds);

     // Compute a displayable value for the counter.
     pdhStatus = PdhGetFormattedCounterValue (hCounter,
                                              PDH_FMT_DOUBLE,
                                              &ctrType,
                                              &fmtValue);

     if (pdhStatus == ERROR_SUCCESS)
     {
         _tprintf (TEXT(",/"%.20g/""), fmtValue.doubleValue);
     }
     else
     {
        _tprintf(TEXT("/nPdhGetFormattedCounterValue failed with %ld./n"), pdhStatus);
        goto cleanup;
     }
   }

cleanup:
   // Close the query.
   if (hQuery)
      PdhCloseQuery (hQuery);

   return pdhStatus;
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值