2009-02-10 18:37:48 +00:00
|
|
|
//====================================
|
|
|
|
// File: OBJINST.CPP
|
|
|
|
// Author: Matt Pietrek
|
|
|
|
// From: Microsoft Systems Journal
|
|
|
|
// "Under the Hood", April 1996
|
|
|
|
//====================================
|
|
|
|
|
|
|
|
#ifndef WIN32_LEAN_AND_MEAN
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
#include <winperf.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#pragma hdrstop
|
|
|
|
#include "titledb.h"
|
|
|
|
#include "objinst.h"
|
|
|
|
#include "perfcntr.h"
|
|
|
|
#include "makeptr.h"
|
|
|
|
|
|
|
|
CPerfObjectInstance::CPerfObjectInstance(
|
2011-03-29 19:21:57 +00:00
|
|
|
PPERF_INSTANCE_DEFINITION const pPerfInstDef,
|
|
|
|
PPERF_COUNTER_DEFINITION const pPerfCntrDef,
|
|
|
|
DWORD nCounters, CPerfTitleDatabase * const pPerfCounterTitles,
|
|
|
|
BOOL fDummy)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2011-03-29 19:21:57 +00:00
|
|
|
m_pPerfInstDef = pPerfInstDef;
|
|
|
|
m_pPerfCntrDef = pPerfCntrDef;
|
|
|
|
m_nCounters = nCounters;
|
|
|
|
m_pPerfCounterTitles = pPerfCounterTitles;
|
|
|
|
|
|
|
|
m_fDummy = fDummy;
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL
|
|
|
|
CPerfObjectInstance::GetObjectInstanceName(
|
2011-03-29 19:21:57 +00:00
|
|
|
PTSTR pszObjInstName, DWORD nSize )
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2011-03-29 19:21:57 +00:00
|
|
|
if ( m_fDummy )
|
|
|
|
{
|
|
|
|
*pszObjInstName = 0; // Return an empty string
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( nSize < (m_pPerfInstDef->NameLength / sizeof(TCHAR)) )
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
PWSTR pszName = MakePtr(PWSTR, m_pPerfInstDef, m_pPerfInstDef->NameOffset);
|
|
|
|
|
|
|
|
#ifdef UNICODE
|
|
|
|
lstrcpy( pszObjInstName, pszName );
|
|
|
|
#else
|
|
|
|
wcstombs( pszObjInstName, pszName, nSize );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return TRUE;
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CPerfCounter *
|
|
|
|
CPerfObjectInstance::MakeCounter( PPERF_COUNTER_DEFINITION const pCounterDef )
|
|
|
|
{
|
2011-03-29 19:21:57 +00:00
|
|
|
// Look up the name of this counter in the title database
|
|
|
|
PTSTR pszName = m_pPerfCounterTitles->GetTitleStringFromIndex(
|
|
|
|
pCounterDef->CounterNameTitleIndex );
|
|
|
|
|
|
|
|
DWORD nInstanceDefSize = m_fDummy ? 0 : m_pPerfInstDef->ByteLength;
|
|
|
|
|
|
|
|
// Create a new CPerfCounter. The caller is responsible for deleting it.
|
|
|
|
return new CPerfCounter(pszName,
|
|
|
|
pCounterDef->CounterType,
|
|
|
|
MakePtr( PBYTE, m_pPerfInstDef,
|
|
|
|
nInstanceDefSize +
|
|
|
|
pCounterDef->CounterOffset ),
|
|
|
|
pCounterDef->CounterSize );
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CPerfCounter *
|
|
|
|
CPerfObjectInstance::GetCounterByIndex( DWORD index )
|
|
|
|
{
|
2011-03-29 19:21:57 +00:00
|
|
|
PPERF_COUNTER_DEFINITION pCurrentCounter;
|
|
|
|
|
|
|
|
if ( index >= m_nCounters )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
pCurrentCounter = m_pPerfCntrDef;
|
|
|
|
|
|
|
|
// Find the correct PERF_COUNTER_DEFINITION by looping
|
|
|
|
for ( DWORD i = 0; i < index; i++ )
|
|
|
|
{
|
|
|
|
pCurrentCounter = MakePtr( PPERF_COUNTER_DEFINITION,
|
|
|
|
pCurrentCounter,
|
|
|
|
pCurrentCounter->ByteLength );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( pCurrentCounter->ByteLength == 0 )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return MakeCounter( pCurrentCounter );
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CPerfCounter *
|
|
|
|
CPerfObjectInstance::GetFirstCounter( void )
|
|
|
|
{
|
2011-03-29 19:21:57 +00:00
|
|
|
m_currentCounter = 0;
|
|
|
|
return GetCounterByIndex( m_currentCounter );
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CPerfCounter *
|
|
|
|
CPerfObjectInstance::GetNextCounter( void )
|
|
|
|
{
|
2011-03-29 19:21:57 +00:00
|
|
|
m_currentCounter++;
|
|
|
|
return GetCounterByIndex( m_currentCounter );
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CPerfCounter *
|
|
|
|
CPerfObjectInstance::GetCounterByName( PCTSTR const pszName )
|
|
|
|
{
|
2011-03-29 19:21:57 +00:00
|
|
|
DWORD cntrIdx = m_pPerfCounterTitles->GetIndexFromTitleString(pszName);
|
|
|
|
if ( cntrIdx == 0 )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
PPERF_COUNTER_DEFINITION pCurrentCounter = m_pPerfCntrDef;
|
|
|
|
|
|
|
|
// Find the correct PERF_COUNTER_DEFINITION by looping and comparing
|
|
|
|
for ( DWORD i = 0; i < m_nCounters; i++ )
|
|
|
|
{
|
|
|
|
if ( pCurrentCounter->CounterNameTitleIndex == cntrIdx )
|
|
|
|
return MakeCounter( pCurrentCounter );
|
|
|
|
|
|
|
|
// Nope. Not this one. Advance to the next counter
|
|
|
|
pCurrentCounter = MakePtr( PPERF_COUNTER_DEFINITION,
|
|
|
|
pCurrentCounter,
|
|
|
|
pCurrentCounter->ByteLength );
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|