|
#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.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.