Click here to Skip to main content
15,893,588 members
Articles / Desktop Programming / MFC

CPdh v1.03 - NT Performance Info

Rate me:
Please Sign up or sign in to vote.
4.63/5 (4 votes)
3 Mar 2000 79.6K   1.1K   26  
A collection of freeware MFC classes to encapsulate the NT Performance Counters.
#include "stdafx.h"
#include "cpdh.h"


CWinApp theApp(_T("CPDH Test program"));


void main()
{
  //Initialise MFC
  if (!AfxWinInit(GetModuleHandle(NULL), NULL, NULL, SW_SHOW))
    return;

  //Test all of the functions in CPdh.h
  try
  {
    //CPdhCounter::ConnectMachine, Change the name of the machine 
    //or comment out to skip over
    //CPdh::ConnectMachine(_T("DECLANM")); //Uncomment to try out on your network

    //CPdhCounter::EnumObjects
    CStringArray Objects;
    CPdh::EnumObjects(NULL, Objects, PERF_DETAIL_WIZARD, TRUE);
    TRACE(_T("Objects on the local machine include:\n"));
    for (int i=0; i<Objects.GetSize(); i++)
      TRACE(_T("Object %d: %s\n"), i, Objects.ElementAt(i));

    //CPdhCounter::EnumMachine
    CStringArray Machines;
    CPdh::EnumMachines(Machines);
    TRACE(_T("Machines PDH are connected to include:\n"));
    for (i=0; i<Machines.GetSize(); i++)
      TRACE(_T("Machine %d: %s\n"), i, Machines.ElementAt(i));

    //CPdhCounter::EnumObjectItems
    CStringArray Counters;
    CStringArray Instances;
    CPdh::EnumObjectItems(NULL, _T("Process"), Counters, Instances, PERF_DETAIL_WIZARD);
    TRACE(_T("Process Instances on this machine include:\n"));
    for (i=0; i<Instances.GetSize(); i++)
      TRACE(_T("Instance %d: %s\n"), i, Instances.ElementAt(i));

    TRACE(_T("Process Counters on the local machine include:\n"));
    for (i=0; i<Counters.GetSize(); i++)
      TRACE(_T("Counter %d: %s\n"), i, Counters.ElementAt(i));

    //CPdhCounter::ExpandPath
    CStringArray Paths;
    CPdh::ExpandPath(_T("\\LogicalDisk(*/*#*)\\*"), Paths);
    TRACE(_T("All of the counters related to the Logical Disk on this machine are:\n"));
    for (i=0; i<Instances.GetSize(); i++)
      TRACE(_T("%s\n"), Paths.ElementAt(i));

    //CPdhCounter::ValidatePath  
    CPdh::ValidatePath(_T("\\Process(Idle)\\% Processor Time"));

    //CPdhCounter::GetDefaultCounter
    CString sDefaultCounterName;                                                              
    CPdh::GetDefaultCounter(NULL, _T("Process"), sDefaultCounterName);
    TRACE(_T("Default counter for Process is %s\n"), sDefaultCounterName);

    //CPdhCounter::GetDefaultObject  
    CString sDefaultObjectName;                                                              
    CPdh::GetDefaultObject(NULL, sDefaultObjectName);
    TRACE(_T("Default object for the local machine is %s\n"), sDefaultObjectName);

    //CPdhCounter::ParseInstanceName
    CString sInstanceName;
    CString sParentName;
    DWORD dwIndex;
    CPdh::ParseInstanceName(_T("Process(Idle)\\% CPU Time"), sInstanceName, sParentName, dwIndex);

    //CPdhCounter::BrowseCounters
    PDH_BROWSE_DLG_CONFIG BrowseDlgData;
    ZeroMemory(&BrowseDlgData, sizeof(PDH_BROWSE_DLG_CONFIG));
    BrowseDlgData.bSingleCounterPerAdd = TRUE;
    BrowseDlgData.bSingleCounterPerDialog = TRUE;
    TCHAR pszCounter[_MAX_PATH];
    BrowseDlgData.szReturnPathBuffer  = pszCounter;
    BrowseDlgData.cchReturnPathLength = _MAX_PATH;
    BrowseDlgData.dwDefaultDetailLevel = PERF_DETAIL_WIZARD;
    CPdh::BrowseCounters(BrowseDlgData);

    //Constructors
    CPdhQuery query;
    CPdhCounter counter(pszCounter);

    //CPdhQuery::Open
    query.Open();

    //CPdhQuery::Add
    query.Add(counter);

    //CPdhQuery::Collect
    query.Collect();

    //CPdhCounter::GetInfo
    CPdhCounterInfo info;
    counter.GetInfo(info);

    //CPdhCounter::GetRawValue
    PDH_RAW_COUNTER rc1;
    DWORD dwType = counter.GetRawValue(rc1);

    //CPdhCounter::GetFormattedValue
    PDH_FMT_COUNTERVALUE fv;
    dwType = counter.GetFormattedValue(PDH_FMT_LONG, fv);

    //CPdhCounter::CalculateFromRawValue
    query.Collect();
    PDH_RAW_COUNTER rc2;
    query.Collect();
    dwType = counter.GetRawValue(rc2);
    //PDH_FMT_COUNTERVALUE fv2;
    //counter.CalculateFromRawValue(PDH_FMT_LONG, rc1, rc2, fv2);  //Uncomment to try out, Remember only
                                                                   //Some Pdh instances make sense to do this

    //SetScaleFactor
    counter.SetScaleFactor(PDH_MIN_SCALE);
    dwType = counter.GetFormattedValue(PDH_FMT_LONG, fv);

    //CPdhCounter::SplitPath
    CString sMachineName;
    CString sObjectName;
    CString sParentInstance;
    DWORD dwInstanceIndex = 0;
    CString sCounterName;  
    counter.SplitPath(sMachineName, sObjectName, sInstanceName, sParentInstance, dwInstanceIndex, sCounterName);

    //The second form of the CPdhCounter constructor
    PDH_COUNTER_PATH_ELEMENTS CounterPathElements;
    CounterPathElements.szMachineName = _T("\\PJ2");
    CounterPathElements.szObjectName = _T("System");
    CounterPathElements.szInstanceName = NULL;
    CounterPathElements.szParentInstance = NULL;
    CounterPathElements.dwInstanceIndex = (DWORD) -1;
    CounterPathElements.szCounterName = _T("System Up Time");
    CPdhCounter counter2(CounterPathElements);

    //CPdhQuery::Remove
    query.Remove(counter);
  }
  catch (CPdhException* pEx)
  {
		// catch errors from PDH
		CString sError;
		pEx->GetErrorMessage(sError);
    TRACE(_T("a CPdhException occurred, Error:%s\n"), sError);

    //Also display a message box displaying the error to the end user
    CString sMsg;
    sMsg.Format(_T("a CPdhException occurred, Error:%s"), sError);
    AfxMessageBox(sMsg);
		pEx->Delete();
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions