Cosmetic changes to code.

This commit is contained in:
Birunthan Mohanathas
2011-03-29 19:21:57 +00:00
parent 5882f12c49
commit a92bdd9b18
65 changed files with 1642 additions and 1642 deletions

View File

@ -19,119 +19,119 @@
#include "makeptr.h"
CPerfObjectInstance::CPerfObjectInstance(
PPERF_INSTANCE_DEFINITION const pPerfInstDef,
PPERF_COUNTER_DEFINITION const pPerfCntrDef,
DWORD nCounters, CPerfTitleDatabase * const pPerfCounterTitles,
BOOL fDummy)
PPERF_INSTANCE_DEFINITION const pPerfInstDef,
PPERF_COUNTER_DEFINITION const pPerfCntrDef,
DWORD nCounters, CPerfTitleDatabase * const pPerfCounterTitles,
BOOL fDummy)
{
m_pPerfInstDef = pPerfInstDef;
m_pPerfCntrDef = pPerfCntrDef;
m_nCounters = nCounters;
m_pPerfCounterTitles = pPerfCounterTitles;
m_fDummy = fDummy;
m_pPerfInstDef = pPerfInstDef;
m_pPerfCntrDef = pPerfCntrDef;
m_nCounters = nCounters;
m_pPerfCounterTitles = pPerfCounterTitles;
m_fDummy = fDummy;
}
BOOL
CPerfObjectInstance::GetObjectInstanceName(
PTSTR pszObjInstName, DWORD nSize )
PTSTR pszObjInstName, DWORD nSize )
{
if ( m_fDummy )
{
*pszObjInstName = 0; // Return an empty string
return FALSE;
}
if ( nSize < (m_pPerfInstDef->NameLength / sizeof(TCHAR)) )
return FALSE;
if ( m_fDummy )
{
*pszObjInstName = 0; // Return an empty string
return FALSE;
}
PWSTR pszName = MakePtr(PWSTR, m_pPerfInstDef, m_pPerfInstDef->NameOffset);
#ifdef UNICODE
lstrcpy( pszObjInstName, pszName );
#else
wcstombs( pszObjInstName, pszName, nSize );
#endif
return TRUE;
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;
}
CPerfCounter *
CPerfObjectInstance::MakeCounter( PPERF_COUNTER_DEFINITION const pCounterDef )
{
// 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;
// Look up the name of this counter in the title database
PTSTR pszName = m_pPerfCounterTitles->GetTitleStringFromIndex(
pCounterDef->CounterNameTitleIndex );
// 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 );
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 );
}
CPerfCounter *
CPerfObjectInstance::GetCounterByIndex( DWORD index )
{
PPERF_COUNTER_DEFINITION pCurrentCounter;
if ( index >= m_nCounters )
return 0;
pCurrentCounter = m_pPerfCntrDef;
PPERF_COUNTER_DEFINITION pCurrentCounter;
// Find the correct PERF_COUNTER_DEFINITION by looping
for ( DWORD i = 0; i < index; i++ )
{
pCurrentCounter = MakePtr( PPERF_COUNTER_DEFINITION,
pCurrentCounter,
pCurrentCounter->ByteLength );
}
if ( index >= m_nCounters )
return 0;
if ( pCurrentCounter->ByteLength == 0 )
return 0;
pCurrentCounter = m_pPerfCntrDef;
return MakeCounter( pCurrentCounter );
// 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 );
}
CPerfCounter *
CPerfObjectInstance::GetFirstCounter( void )
{
m_currentCounter = 0;
return GetCounterByIndex( m_currentCounter );
m_currentCounter = 0;
return GetCounterByIndex( m_currentCounter );
}
CPerfCounter *
CPerfObjectInstance::GetNextCounter( void )
{
m_currentCounter++;
return GetCounterByIndex( m_currentCounter );
m_currentCounter++;
return GetCounterByIndex( m_currentCounter );
}
CPerfCounter *
CPerfObjectInstance::GetCounterByName( PCTSTR const pszName )
{
DWORD cntrIdx = m_pPerfCounterTitles->GetIndexFromTitleString(pszName);
if ( cntrIdx == 0 )
return 0;
PPERF_COUNTER_DEFINITION pCurrentCounter = m_pPerfCntrDef;
DWORD cntrIdx = m_pPerfCounterTitles->GetIndexFromTitleString(pszName);
if ( cntrIdx == 0 )
return 0;
// 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 );
}
PPERF_COUNTER_DEFINITION pCurrentCounter = m_pPerfCntrDef;
return 0;
// 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;
}

View File

@ -19,65 +19,65 @@
#include "makeptr.h"
CPerfObjectList::CPerfObjectList(
CPerfSnapshot * const pPerfSnapshot,
CPerfTitleDatabase * const pPerfTitleDatabase )
CPerfSnapshot * const pPerfSnapshot,
CPerfTitleDatabase * const pPerfTitleDatabase )
{
m_pPerfSnapshot = pPerfSnapshot;
m_pPerfCounterTitles = pPerfTitleDatabase;
m_pPerfSnapshot = pPerfSnapshot;
m_pPerfCounterTitles = pPerfTitleDatabase;
}
CPerfObject *
CPerfObjectList::GetFirstPerfObject( void )
{
m_currentObjectListIndex = 0;
if ( m_currentObjectListIndex >= m_pPerfSnapshot->GetNumObjectTypes() )
return 0;
m_currentObjectListIndex = 0;
if ( m_currentObjectListIndex >= m_pPerfSnapshot->GetNumObjectTypes() )
return 0;
m_pCurrObjectType =
(PPERF_OBJECT_TYPE)m_pPerfSnapshot->GetPostHeaderPointer();
m_pCurrObjectType =
(PPERF_OBJECT_TYPE)m_pPerfSnapshot->GetPostHeaderPointer();
return new CPerfObject( m_pCurrObjectType, m_pPerfCounterTitles );
return new CPerfObject( m_pCurrObjectType, m_pPerfCounterTitles );
}
CPerfObject *
CPerfObjectList::GetNextPerfObject( void )
{
// Are we at the last object in the list? Return NULL if so.
if ( ++m_currentObjectListIndex >= m_pPerfSnapshot->GetNumObjectTypes() )
return 0;
// Are we at the last object in the list? Return NULL if so.
if ( ++m_currentObjectListIndex >= m_pPerfSnapshot->GetNumObjectTypes() )
return 0;
// Advance to the next PERF_OBJECT_TYPE structure
m_pCurrObjectType = MakePtr(PPERF_OBJECT_TYPE,
m_pCurrObjectType,
m_pCurrObjectType->TotalByteLength );
return new CPerfObject( m_pCurrObjectType, m_pPerfCounterTitles );
// Advance to the next PERF_OBJECT_TYPE structure
m_pCurrObjectType = MakePtr(PPERF_OBJECT_TYPE,
m_pCurrObjectType,
m_pCurrObjectType->TotalByteLength );
return new CPerfObject( m_pCurrObjectType, m_pPerfCounterTitles );
}
CPerfObject *
CPerfObjectList::GetPerfObject( PCTSTR const pszObjListName )
{
DWORD objListIdx
= m_pPerfCounterTitles->GetIndexFromTitleString( pszObjListName );
if ( 0 == objListIdx )
return 0;
DWORD objListIdx
= m_pPerfCounterTitles->GetIndexFromTitleString( pszObjListName );
if ( 0 == objListIdx )
return 0;
// Point at first PERF_OBJECT_TYPE, and loop through the list, looking
// for one that matches.
PPERF_OBJECT_TYPE pCurrObjectType =
(PPERF_OBJECT_TYPE)m_pPerfSnapshot->GetPostHeaderPointer();
// Point at first PERF_OBJECT_TYPE, and loop through the list, looking
// for one that matches.
PPERF_OBJECT_TYPE pCurrObjectType =
(PPERF_OBJECT_TYPE)m_pPerfSnapshot->GetPostHeaderPointer();
for ( unsigned i=0; i < m_pPerfSnapshot->GetNumObjectTypes(); i++ )
{
// Is this the one that matches?
if ( pCurrObjectType->ObjectNameTitleIndex == objListIdx )
return new CPerfObject(pCurrObjectType, m_pPerfCounterTitles);
for ( unsigned i=0; i < m_pPerfSnapshot->GetNumObjectTypes(); i++ )
{
// Is this the one that matches?
if ( pCurrObjectType->ObjectNameTitleIndex == objListIdx )
return new CPerfObject(pCurrObjectType, m_pPerfCounterTitles);
// Nope... try the next object type
pCurrObjectType = MakePtr( PPERF_OBJECT_TYPE,
pCurrObjectType,
pCurrObjectType->TotalByteLength );
}
return 0;
// Nope... try the next object type
pCurrObjectType = MakePtr( PPERF_OBJECT_TYPE,
pCurrObjectType,
pCurrObjectType->TotalByteLength );
}
return 0;
}

View File

@ -19,105 +19,105 @@
#include "perfcntr.h"
CPerfCounter::CPerfCounter( PTSTR const pszName, DWORD type,
PBYTE const pData, DWORD cbData )
PBYTE const pData, DWORD cbData )
{
m_pszName = _tcsdup( pszName );
m_type = type;
m_cbData = cbData;
m_pData = new BYTE[m_cbData];
memcpy( m_pData, pData, m_cbData );
m_pszName = _tcsdup( pszName );
m_type = type;
m_cbData = cbData;
m_pData = new BYTE[m_cbData];
memcpy( m_pData, pData, m_cbData );
}
CPerfCounter::~CPerfCounter( void )
{
free( m_pszName );
delete []m_pData;
free( m_pszName );
delete []m_pData;
}
BOOL
CPerfCounter::GetData( PBYTE pBuffer, DWORD cbBuffer, DWORD *pType )
{
if ( cbBuffer < m_cbData ) // Make sure the buffer is big enough
return FALSE;
memcpy( pBuffer, m_pData, m_cbData ); // copy the data
if ( cbBuffer < m_cbData ) // Make sure the buffer is big enough
return FALSE;
if ( pType ) // If the user wants the type, give it to them
*pType = m_type;
return TRUE;
memcpy( pBuffer, m_pData, m_cbData ); // copy the data
if ( pType ) // If the user wants the type, give it to them
*pType = m_type;
return TRUE;
}
BOOL
CPerfCounter::Format( PTSTR pszBuffer, DWORD nSize, BOOL fHex )
{
// Do better formatting!!! Check length!!!
// Do better formatting!!! Check length!!!
PTSTR pszPrefix = TEXT("");
TCHAR szTemp[512];
// First, ascertain the basic type (number, counter, text, or zero)
switch ( m_type & 0x00000C00 )
{
case PERF_TYPE_ZERO:
{
wsprintf( pszBuffer, TEXT("ZERO") ); return TRUE;
}
case PERF_TYPE_TEXT:
{
wsprintf( pszBuffer, TEXT("text counter") ); return TRUE;
}
case PERF_TYPE_COUNTER:
{
switch( m_type & 0x00070000 )
{
case PERF_COUNTER_RATE:
pszPrefix = TEXT("counter rate "); break;
case PERF_COUNTER_FRACTION:
pszPrefix = TEXT("counter fraction "); break;
case PERF_COUNTER_BASE:
pszPrefix = TEXT("counter base "); break;
case PERF_COUNTER_ELAPSED:
pszPrefix = TEXT("counter elapsed "); break;
case PERF_COUNTER_QUEUELEN:
pszPrefix = TEXT("counter queuelen "); break;
case PERF_COUNTER_HISTOGRAM:
pszPrefix = TEXT("counter histogram "); break;
default:
pszPrefix = TEXT("counter value "); break;
}
}
}
PTSTR pszFmt = fHex ? TEXT("%s%Xh") : TEXT("%s%u");
switch ( m_cbData )
{
case 1: wsprintf(szTemp, pszFmt, pszPrefix, *(PBYTE)m_pData);
break;
case 2: wsprintf(szTemp, pszFmt, pszPrefix, *(PWORD)m_pData);
break;
case 4: wsprintf(szTemp, pszFmt, pszPrefix, *(PDWORD)m_pData);
break;
case 8: // Danger! Assumes little-endian (X86) byte ordering
wsprintf( szTemp, TEXT("%s%X%X"), pszPrefix,
*(PDWORD)(m_pData+4), *(PDWORD)m_pData ); break;
PTSTR pszPrefix = TEXT("");
TCHAR szTemp[512];
// First, ascertain the basic type (number, counter, text, or zero)
switch ( m_type & 0x00000C00 )
{
case PERF_TYPE_ZERO:
{
wsprintf( pszBuffer, TEXT("ZERO") ); return TRUE;
}
case PERF_TYPE_TEXT:
{
wsprintf( pszBuffer, TEXT("text counter") ); return TRUE;
}
case PERF_TYPE_COUNTER:
{
switch( m_type & 0x00070000 )
{
case PERF_COUNTER_RATE:
pszPrefix = TEXT("counter rate "); break;
case PERF_COUNTER_FRACTION:
pszPrefix = TEXT("counter fraction "); break;
case PERF_COUNTER_BASE:
pszPrefix = TEXT("counter base "); break;
case PERF_COUNTER_ELAPSED:
pszPrefix = TEXT("counter elapsed "); break;
case PERF_COUNTER_QUEUELEN:
pszPrefix = TEXT("counter queuelen "); break;
case PERF_COUNTER_HISTOGRAM:
pszPrefix = TEXT("counter histogram "); break;
default:
pszPrefix = TEXT("counter value "); break;
}
}
}
PTSTR pszFmt = fHex ? TEXT("%s%Xh") : TEXT("%s%u");
switch ( m_cbData )
{
case 1: wsprintf(szTemp, pszFmt, pszPrefix, *(PBYTE)m_pData);
break;
case 2: wsprintf(szTemp, pszFmt, pszPrefix, *(PWORD)m_pData);
break;
case 4: wsprintf(szTemp, pszFmt, pszPrefix, *(PDWORD)m_pData);
break;
case 8: // Danger! Assumes little-endian (X86) byte ordering
wsprintf( szTemp, TEXT("%s%X%X"), pszPrefix,
*(PDWORD)(m_pData+4), *(PDWORD)m_pData ); break;
break;
default: wsprintf( szTemp, TEXT("<unhandled size %u>"), m_cbData );
}
switch ( m_type & 0x70000000 )
{
case PERF_DISPLAY_SECONDS:
_tcscat( szTemp, TEXT(" secs") ); break;
case PERF_DISPLAY_PERCENT:
_tcscat( szTemp, TEXT(" %") ); break;
case PERF_DISPLAY_PER_SEC:
_tcscat( szTemp, TEXT(" /sec") ); break;
}
default: wsprintf( szTemp, TEXT("<unhandled size %u>"), m_cbData );
}
lstrcpyn( pszBuffer, szTemp, nSize );
return TRUE;
switch ( m_type & 0x70000000 )
{
case PERF_DISPLAY_SECONDS:
_tcscat( szTemp, TEXT(" secs") ); break;
case PERF_DISPLAY_PERCENT:
_tcscat( szTemp, TEXT(" %") ); break;
case PERF_DISPLAY_PER_SEC:
_tcscat( szTemp, TEXT(" /sec") ); break;
}
lstrcpyn( pszBuffer, szTemp, nSize );
return TRUE;
}

View File

@ -42,7 +42,7 @@ static std::map<UINT, PerfMeasure*> g_Measures;
/*
This function is called when the measure is initialized.
The function must return the maximum value that can be measured.
The function must return the maximum value that can be measured.
The return value can also be 0, which means that Rainmeter will
track the maximum value automatically. The parameters for this
function are:
@ -105,21 +105,21 @@ double Update2(UINT id)
double value = 0;
std::map<UINT, PerfMeasure*>::iterator i = g_Measures.find(id);
if(i != g_Measures.end())
if (i != g_Measures.end())
{
PerfMeasure* measure = (*i).second;
if(measure)
if (measure)
{
ULONGLONG longvalue;
longvalue = GetPerfData(measure->ObjectName.c_str(),
measure->InstanceName.c_str(),
longvalue = GetPerfData(measure->ObjectName.c_str(),
measure->InstanceName.c_str(),
measure->CounterName.c_str());
if(measure->Difference)
if (measure->Difference)
{
// Compare with the old value
if(!measure->FirstTime)
if (!measure->FirstTime)
{
value = (double)(longvalue - measure->OldValue);
}
@ -145,7 +145,7 @@ void Finalize(HMODULE instance, UINT id)
{
// delete the measure
std::map<UINT, PerfMeasure*>::iterator i = g_Measures.find(id);
if(i != g_Measures.end())
if (i != g_Measures.end())
{
delete (*i).second;
g_Measures.erase(i);
@ -166,7 +166,7 @@ ULONGLONG GetPerfData(PCTSTR ObjectName, PCTSTR InstanceName, PCTSTR CounterName
WCHAR name[256];
ULONGLONG value = 0;
if(ObjectName == NULL || CounterName == NULL || wcslen(ObjectName) == 0 || wcslen(CounterName) == 0)
if (ObjectName == NULL || CounterName == NULL || wcslen(ObjectName) == 0 || wcslen(CounterName) == 0)
{
// Unable to continue
return 0;
@ -175,21 +175,21 @@ ULONGLONG GetPerfData(PCTSTR ObjectName, PCTSTR InstanceName, PCTSTR CounterName
CPerfSnapshot snapshot(&g_CounterTitles);
CPerfObjectList objList(&snapshot, &g_CounterTitles);
if(snapshot.TakeSnapshot(ObjectName))
if (snapshot.TakeSnapshot(ObjectName))
{
pPerfObj = objList.GetPerfObject(ObjectName);
if(pPerfObj)
if (pPerfObj)
{
for(pObjInst = pPerfObj->GetFirstObjectInstance();
for (pObjInst = pPerfObj->GetFirstObjectInstance();
pObjInst != NULL;
pObjInst = pPerfObj->GetNextObjectInstance())
{
if (InstanceName != NULL && wcslen(InstanceName) > 0)
{
if(pObjInst->GetObjectInstanceName(name, 256))
if (pObjInst->GetObjectInstanceName(name, 256))
{
if(_wcsicmp(InstanceName, name) != 0)
if (_wcsicmp(InstanceName, name) != 0)
{
delete pObjInst;
continue;
@ -203,23 +203,23 @@ ULONGLONG GetPerfData(PCTSTR ObjectName, PCTSTR InstanceName, PCTSTR CounterName
}
pPerfCntr = pObjInst->GetCounterByName(CounterName);
if(pPerfCntr != NULL)
if (pPerfCntr != NULL)
{
pPerfCntr->GetData(data, 256, NULL);
if(pPerfCntr->GetSize() == 1)
if (pPerfCntr->GetSize() == 1)
{
value = *(BYTE*)data;
}
else if(pPerfCntr->GetSize() == 2)
}
else if (pPerfCntr->GetSize() == 2)
{
value = *(WORD*)data;
}
else if(pPerfCntr->GetSize() == 4)
else if (pPerfCntr->GetSize() == 4)
{
value = *(DWORD*)data;
}
else if(pPerfCntr->GetSize() == 8)
else if (pPerfCntr->GetSize() == 8)
{
value = *(ULONGLONG*)data;
}

View File

@ -19,78 +19,78 @@
#include "makeptr.h"
CPerfObject::CPerfObject( PPERF_OBJECT_TYPE const pObjectList,
CPerfTitleDatabase * const pPerfCounterTitles)
CPerfTitleDatabase * const pPerfCounterTitles)
{
m_pObjectList = pObjectList;
m_pPerfCounterTitles = pPerfCounterTitles;
m_pObjectList = pObjectList;
m_pPerfCounterTitles = pPerfCounterTitles;
}
CPerfObjectInstance *
CPerfObject::GetFirstObjectInstance( void )
{
m_currentObjectInstance = 0;
if ( m_currentObjectInstance >= GetObjectInstanceCount() )
return 0;
m_currentObjectInstance = 0;
if ( m_currentObjectInstance >= GetObjectInstanceCount() )
return 0;
// Point at the first PERF_INSTANCE_DEFINITION
m_pCurrentObjectInstanceDefinition =
MakePtr( PPERF_INSTANCE_DEFINITION, m_pObjectList,
m_pObjectList->DefinitionLength );
// Point at the first PERF_INSTANCE_DEFINITION
m_pCurrentObjectInstanceDefinition =
MakePtr( PPERF_INSTANCE_DEFINITION, m_pObjectList,
m_pObjectList->DefinitionLength );
return new CPerfObjectInstance(
m_pCurrentObjectInstanceDefinition,
MakePtr(PPERF_COUNTER_DEFINITION,
m_pObjectList, m_pObjectList->HeaderLength),
m_pObjectList->NumCounters,
m_pPerfCounterTitles,
m_pObjectList->NumInstances ==
PERF_NO_INSTANCES ? TRUE : FALSE );
return new CPerfObjectInstance(
m_pCurrentObjectInstanceDefinition,
MakePtr(PPERF_COUNTER_DEFINITION,
m_pObjectList, m_pObjectList->HeaderLength),
m_pObjectList->NumCounters,
m_pPerfCounterTitles,
m_pObjectList->NumInstances ==
PERF_NO_INSTANCES ? TRUE : FALSE );
}
CPerfObjectInstance *
CPerfObject::GetNextObjectInstance( void )
{
if ( m_pObjectList->NumInstances == PERF_NO_INSTANCES )
return 0;
if ( m_pObjectList->NumInstances == PERF_NO_INSTANCES )
return 0;
if ( ++m_currentObjectInstance >= GetObjectInstanceCount() )
return 0;
if ( ++m_currentObjectInstance >= GetObjectInstanceCount() )
return 0;
// Advance to the next PERF_INSTANCE_DEFINITION in the list. However,
// following the current PERF_INSTANCE_DEFINITION is the counter data,
// which is also of variable length. So, we gotta take that into
// account when finding the next PERF_INSTANCE_DEFINITION
// First, get a pointer to the counter data size field
PDWORD pCounterDataSize
= MakePtr(PDWORD, m_pCurrentObjectInstanceDefinition,
m_pCurrentObjectInstanceDefinition->ByteLength);
// Advance to the next PERF_INSTANCE_DEFINITION in the list. However,
// following the current PERF_INSTANCE_DEFINITION is the counter data,
// which is also of variable length. So, we gotta take that into
// account when finding the next PERF_INSTANCE_DEFINITION
// Now we can point at the next PPERF_INSTANCE_DEFINITION
m_pCurrentObjectInstanceDefinition = MakePtr(PPERF_INSTANCE_DEFINITION,
m_pCurrentObjectInstanceDefinition,
m_pCurrentObjectInstanceDefinition->ByteLength
+ *pCounterDataSize);
// Create a CPerfObjectInstance based around the PPERF_INSTANCE_DEFINITION
return new CPerfObjectInstance(m_pCurrentObjectInstanceDefinition,
MakePtr(PPERF_COUNTER_DEFINITION,
m_pObjectList,
m_pObjectList->HeaderLength),
m_pObjectList->NumCounters,
m_pPerfCounterTitles,
FALSE );
// First, get a pointer to the counter data size field
PDWORD pCounterDataSize
= MakePtr(PDWORD, m_pCurrentObjectInstanceDefinition,
m_pCurrentObjectInstanceDefinition->ByteLength);
// Now we can point at the next PPERF_INSTANCE_DEFINITION
m_pCurrentObjectInstanceDefinition = MakePtr(PPERF_INSTANCE_DEFINITION,
m_pCurrentObjectInstanceDefinition,
m_pCurrentObjectInstanceDefinition->ByteLength
+ *pCounterDataSize);
// Create a CPerfObjectInstance based around the PPERF_INSTANCE_DEFINITION
return new CPerfObjectInstance(m_pCurrentObjectInstanceDefinition,
MakePtr(PPERF_COUNTER_DEFINITION,
m_pObjectList,
m_pObjectList->HeaderLength),
m_pObjectList->NumCounters,
m_pPerfCounterTitles,
FALSE );
}
BOOL
CPerfObject::GetObjectTypeName( PTSTR pszObjTypeName, DWORD nSize )
{
PTSTR pszName = m_pPerfCounterTitles->GetTitleStringFromIndex(
m_pObjectList->ObjectNameTitleIndex );
if ( !pszName )
return FALSE;
lstrcpyn( pszObjTypeName, pszName, nSize );
return TRUE;
PTSTR pszName = m_pPerfCounterTitles->GetTitleStringFromIndex(
m_pObjectList->ObjectNameTitleIndex );
if ( !pszName )
return FALSE;
lstrcpyn( pszObjTypeName, pszName, nSize );
return TRUE;
}

View File

@ -21,145 +21,145 @@ PBYTE CPerfSnapshot::c_pBuffer = NULL;
DWORD CPerfSnapshot::c_cbBufferSize = 0;
CPerfSnapshot::CPerfSnapshot(
CPerfTitleDatabase * pCounterTitles )
CPerfTitleDatabase * pCounterTitles )
{
m_pPerfDataHeader = 0;
m_pCounterTitles = pCounterTitles;
m_pPerfDataHeader = 0;
m_pCounterTitles = pCounterTitles;
}
CPerfSnapshot::~CPerfSnapshot( void )
{
DisposeSnapshot();
DisposeSnapshot();
}
BOOL
CPerfSnapshot::TakeSnapshot( PCTSTR pszSnapshotItems )
{
DisposeSnapshot(); // Clear out any current snapshot
DisposeSnapshot(); // Clear out any current snapshot
// Convert the input string (e.g., "Process") into the form required
// by the HKEY_PERFORMANCE_DATA key (e.g., "232")
// Convert the input string (e.g., "Process") into the form required
// by the HKEY_PERFORMANCE_DATA key (e.g., "232")
TCHAR szConvertedItemNames[ 256 ];
if ( !ConvertSnapshotItemName( pszSnapshotItems, szConvertedItemNames,
sizeof(szConvertedItemNames) ) )
return FALSE;
TCHAR szConvertedItemNames[ 256 ];
if ( !ConvertSnapshotItemName( pszSnapshotItems, szConvertedItemNames,
sizeof(szConvertedItemNames) ) )
return FALSE;
DWORD cbAllocSize = 0;
LONG retValue;
while ( 1 ) // Loop until we get the data, or we fail unexpectedly
{
retValue = RegQueryValueEx( HKEY_PERFORMANCE_DATA,
szConvertedItemNames, 0, 0,
c_pBuffer, &c_cbBufferSize );
DWORD cbAllocSize = 0;
LONG retValue;
if ( retValue == ERROR_SUCCESS ) // We apparently got the snapshot
{
while ( 1 ) // Loop until we get the data, or we fail unexpectedly
{
retValue = RegQueryValueEx( HKEY_PERFORMANCE_DATA,
szConvertedItemNames, 0, 0,
c_pBuffer, &c_cbBufferSize );
if ( retValue == ERROR_SUCCESS ) // We apparently got the snapshot
{
m_pPerfDataHeader = (PPERF_DATA_BLOCK)c_pBuffer;
// Verify that the signature is a unicode "PERF"
if ( memcmp( m_pPerfDataHeader->Signature, L"PERF", 8 ) )
break;
// Verify that the signature is a unicode "PERF"
if ( memcmp( m_pPerfDataHeader->Signature, L"PERF", 8 ) )
break;
return TRUE;
}
else if ( retValue != ERROR_MORE_DATA ) // Anything other failure
break; // code means something
// bad happened, so bail out.
return TRUE;
}
else if ( retValue != ERROR_MORE_DATA ) // Anything other failure
break; // code means something
// bad happened, so bail out.
// If we get here, our buffer wasn't big enough. Delete it and
// try again with a bigger buffer.
delete [] c_pBuffer;
// The new buffer size will be 4096 bytes bigger than the larger
// of: 1) The previous allocation size, or 2) The size that the
// RegQueryValueEx call said was necessary.
if ( c_cbBufferSize > cbAllocSize )
cbAllocSize = c_cbBufferSize + 4096;
else
cbAllocSize += 4096;
// If we get here, our buffer wasn't big enough. Delete it and
// try again with a bigger buffer.
delete [] c_pBuffer;
// Allocate a new, larger buffer in preparation to try again.
c_pBuffer = new BYTE[ cbAllocSize ];
if ( !c_pBuffer )
break;
// The new buffer size will be 4096 bytes bigger than the larger
// of: 1) The previous allocation size, or 2) The size that the
// RegQueryValueEx call said was necessary.
if ( c_cbBufferSize > cbAllocSize )
cbAllocSize = c_cbBufferSize + 4096;
else
cbAllocSize += 4096;
c_cbBufferSize = cbAllocSize;
}
// Allocate a new, larger buffer in preparation to try again.
c_pBuffer = new BYTE[ cbAllocSize ];
if ( !c_pBuffer )
break;
// If we get here, the RegQueryValueEx failed unexpectedly.
return FALSE;
c_cbBufferSize = cbAllocSize;
}
// If we get here, the RegQueryValueEx failed unexpectedly.
return FALSE;
}
void
CPerfSnapshot::DisposeSnapshot( void )
{
m_pPerfDataHeader = 0;
m_pPerfDataHeader = 0;
}
void
CPerfSnapshot::CleanUp( void )
{
delete [] c_pBuffer;
c_pBuffer = 0;
delete [] c_pBuffer;
c_pBuffer = 0;
c_cbBufferSize = 0;
}
DWORD
CPerfSnapshot::GetNumObjectTypes( void )
{
return m_pPerfDataHeader ? m_pPerfDataHeader->NumObjectTypes: 0;
return m_pPerfDataHeader ? m_pPerfDataHeader->NumObjectTypes: 0;
}
BOOL
CPerfSnapshot::GetSystemName( PTSTR pszSystemName, DWORD nSize )
{
if ( !m_pPerfDataHeader ) // If no snapshot data, bail out.
return FALSE;
if ( !m_pPerfDataHeader ) // If no snapshot data, bail out.
return FALSE;
// Make sure the input buffer size is big enough
if ( nSize < m_pPerfDataHeader->SystemNameLength )
return FALSE;
// Make sure the input buffer size is big enough
if ( nSize < m_pPerfDataHeader->SystemNameLength )
return FALSE;
// Make a unicode string point to the system name string
// that's stored in the PERF_DATA_BLOCK
LPWSTR lpwszName = MakePtr( LPWSTR, m_pPerfDataHeader,
m_pPerfDataHeader->SystemNameOffset );
// Make a unicode string point to the system name string
// that's stored in the PERF_DATA_BLOCK
LPWSTR lpwszName = MakePtr( LPWSTR, m_pPerfDataHeader,
m_pPerfDataHeader->SystemNameOffset );
#ifdef UNICODE // Copy the PERF_DATA_BLOCK string to the input buffer
lstrcpy( pszSystemName, lpwszName );
#else
wcstombs( pszSystemName, lpwszName, nSize );
#endif
return TRUE;
#ifdef UNICODE // Copy the PERF_DATA_BLOCK string to the input buffer
lstrcpy( pszSystemName, lpwszName );
#else
wcstombs( pszSystemName, lpwszName, nSize );
#endif
return TRUE;
}
PVOID
CPerfSnapshot::GetPostHeaderPointer( void )
{
// Returns a header to the first byte following the PERF_DATA_BLOCK
// (including the variable length system name string at the end)
return m_pPerfDataHeader ?
MakePtr(PVOID, m_pPerfDataHeader,m_pPerfDataHeader->HeaderLength)
: 0;
// Returns a header to the first byte following the PERF_DATA_BLOCK
// (including the variable length system name string at the end)
return m_pPerfDataHeader ?
MakePtr(PVOID, m_pPerfDataHeader,m_pPerfDataHeader->HeaderLength)
: 0;
}
BOOL
CPerfSnapshot::ConvertSnapshotItemName( PCTSTR pszIn,
PTSTR pszOut, DWORD nSize )
PTSTR pszOut, DWORD nSize )
{
if ( IsBadStringPtr( pszIn, 0xFFFFFFFF ) )
return FALSE;
if ( IsBadStringPtr( pszIn, 0xFFFFFFFF ) )
return FALSE;
DWORD objectID = m_pCounterTitles->GetIndexFromTitleString(pszIn);
if ( objectID )
pszOut += wsprintf( pszOut, TEXT("%u "), objectID );
else
pszOut += wsprintf( pszOut, TEXT("%s "), pszIn );
if ( objectID )
pszOut += wsprintf( pszOut, TEXT("%u "), objectID );
else
pszOut += wsprintf( pszOut, TEXT("%s "), pszIn );
return TRUE;
return TRUE;
}

View File

@ -16,143 +16,143 @@
#include "Titledb.h"
CPerfTitleDatabase::CPerfTitleDatabase(
PERFORMANCE_TITLE_TYPE titleType )
PERFORMANCE_TITLE_TYPE titleType )
{
m_nLastIndex = 0;
m_TitleStrings = 0;
m_pszRawStrings = 0;
m_nLastIndex = 0;
m_TitleStrings = 0;
m_pszRawStrings = 0;
// Determine the appropriate strings to pass to RegOpenKeyEx
PTSTR psz009RegValue, pszLastIndexRegValue;
if ( PERF_TITLE_COUNTER == titleType )
{
psz009RegValue = TEXT("Counter 009");
pszLastIndexRegValue = TEXT("Last Counter");
}
else if ( PERF_TITLE_EXPLAIN == titleType )
{
psz009RegValue = TEXT("Explain 009");
pszLastIndexRegValue = TEXT("Last Help");
}
else
return;
// Determine the appropriate strings to pass to RegOpenKeyEx
PTSTR psz009RegValue, pszLastIndexRegValue;
if ( PERF_TITLE_COUNTER == titleType )
{
psz009RegValue = TEXT("Counter 009");
pszLastIndexRegValue = TEXT("Last Counter");
}
else if ( PERF_TITLE_EXPLAIN == titleType )
{
psz009RegValue = TEXT("Explain 009");
pszLastIndexRegValue = TEXT("Last Help");
}
else
return;
// Find out the max number of entries
HKEY hKeyPerflib = 0;
DWORD cbLastIndex;
// Open the registry key that has the values for the maximum number
// of title strings
if ( ERROR_SUCCESS != RegOpenKeyEx(
HKEY_LOCAL_MACHINE,
TEXT("software\\microsoft\\windows nt\\currentversion\\perflib"),
0, KEY_READ, &hKeyPerflib ) )
return;
// Find out the max number of entries
HKEY hKeyPerflib = 0;
DWORD cbLastIndex;
// Read in the number of title strings
if ( ERROR_SUCCESS != RegQueryValueEx(
hKeyPerflib, pszLastIndexRegValue, 0, 0,
(PBYTE)&m_nLastIndex, &cbLastIndex ) )
{
RegCloseKey( hKeyPerflib );
return;
}
RegCloseKey( hKeyPerflib );
//
// Now go find and process the raw string data
//
// Open the registry key that has the values for the maximum number
// of title strings
if ( ERROR_SUCCESS != RegOpenKeyEx(
HKEY_LOCAL_MACHINE,
TEXT("software\\microsoft\\windows nt\\currentversion\\perflib"),
0, KEY_READ, &hKeyPerflib ) )
return;
// Determine how big the raw data in the REG_MULTI_SZ value is
DWORD cbTitleStrings;
if ( ERROR_SUCCESS != RegQueryValueEx( HKEY_PERFORMANCE_DATA, psz009RegValue, 0,0,0, &cbTitleStrings))
return;
// Read in the number of title strings
if ( ERROR_SUCCESS != RegQueryValueEx(
hKeyPerflib, pszLastIndexRegValue, 0, 0,
(PBYTE)&m_nLastIndex, &cbLastIndex ) )
{
RegCloseKey( hKeyPerflib );
return;
}
// Allocate memory for the raw registry title string data
m_pszRawStrings = new TCHAR[cbTitleStrings];
// Read in the raw title strings
if ( ERROR_SUCCESS != RegQueryValueEx( HKEY_PERFORMANCE_DATA,
psz009RegValue, 0, 0, (PBYTE)m_pszRawStrings,
&cbTitleStrings ) )
{
delete []m_pszRawStrings;
return;
}
RegCloseKey( hKeyPerflib );
// allocate memory for an array of string pointers.
m_TitleStrings = new PTSTR[ m_nLastIndex+1 ];
if ( !m_TitleStrings )
{
delete []m_pszRawStrings;
return;
}
//
// Now go find and process the raw string data
//
// Initialize the m_TitleStrings to all NULLs, since there may
// be some array slots that aren't used.
memset( m_TitleStrings, 0, sizeof(PTSTR) * (m_nLastIndex+1) );
// Determine how big the raw data in the REG_MULTI_SZ value is
DWORD cbTitleStrings;
if ( ERROR_SUCCESS != RegQueryValueEx( HKEY_PERFORMANCE_DATA, psz009RegValue, 0,0,0, &cbTitleStrings))
return;
// The raw data entries are an ASCII string index (e.g., "242"), followed
// by the corresponding string. Fill in the appropriate slot in the
// m_TitleStrings array with the pointer to the string name. The end
// of the list is indicated by a double NULL.
// Allocate memory for the raw registry title string data
m_pszRawStrings = new TCHAR[cbTitleStrings];
PTSTR pszWorkStr = (PTSTR)m_pszRawStrings;
unsigned cbCurrStr;
// Read in the raw title strings
if ( ERROR_SUCCESS != RegQueryValueEx( HKEY_PERFORMANCE_DATA,
psz009RegValue, 0, 0, (PBYTE)m_pszRawStrings,
&cbTitleStrings ) )
{
delete []m_pszRawStrings;
return;
}
// While the length of the current string isn't 0...
while ( 0 != (cbCurrStr = lstrlen( pszWorkStr)) )
{
// Convert the first string to a binary representation
unsigned index = _ttoi( pszWorkStr ); // _ttoi -> atoi()
// allocate memory for an array of string pointers.
m_TitleStrings = new PTSTR[ m_nLastIndex+1 ];
if ( !m_TitleStrings )
{
delete []m_pszRawStrings;
return;
}
if ( index > m_nLastIndex )
break;
// Now point to the string following it. This is the title string
pszWorkStr += cbCurrStr + 1;
// Initialize the m_TitleStrings to all NULLs, since there may
// be some array slots that aren't used.
memset( m_TitleStrings, 0, sizeof(PTSTR) * (m_nLastIndex+1) );
// Fill in the appropriate slot in the title strings array.
m_TitleStrings[index] = pszWorkStr;
// Advance to the next index/title pair
pszWorkStr += lstrlen(pszWorkStr) + 1;
}
// The raw data entries are an ASCII string index (e.g., "242"), followed
// by the corresponding string. Fill in the appropriate slot in the
// m_TitleStrings array with the pointer to the string name. The end
// of the list is indicated by a double NULL.
PTSTR pszWorkStr = (PTSTR)m_pszRawStrings;
unsigned cbCurrStr;
// While the length of the current string isn't 0...
while ( 0 != (cbCurrStr = lstrlen( pszWorkStr)) )
{
// Convert the first string to a binary representation
unsigned index = _ttoi( pszWorkStr ); // _ttoi -> atoi()
if ( index > m_nLastIndex )
break;
// Now point to the string following it. This is the title string
pszWorkStr += cbCurrStr + 1;
// Fill in the appropriate slot in the title strings array.
m_TitleStrings[index] = pszWorkStr;
// Advance to the next index/title pair
pszWorkStr += lstrlen(pszWorkStr) + 1;
}
}
CPerfTitleDatabase::~CPerfTitleDatabase( )
{
delete []m_TitleStrings;
m_TitleStrings = 0;
delete []m_pszRawStrings;
m_pszRawStrings = 0;
m_nLastIndex = 0;
delete []m_TitleStrings;
m_TitleStrings = 0;
delete []m_pszRawStrings;
m_pszRawStrings = 0;
m_nLastIndex = 0;
}
PTSTR
CPerfTitleDatabase::GetTitleStringFromIndex( unsigned index )
{
if ( index > m_nLastIndex ) // Is index within range?
return 0;
return m_TitleStrings[ index ];
if ( index > m_nLastIndex ) // Is index within range?
return 0;
return m_TitleStrings[ index ];
}
unsigned
CPerfTitleDatabase::GetIndexFromTitleString( PCTSTR pszTitleString )
{
if ( IsBadStringPtr(pszTitleString, 0xFFFFFFFF) )
return 0;
if ( IsBadStringPtr(pszTitleString, 0xFFFFFFFF) )
return 0;
// Loop through all the non-null string array entries, doing a case-
// insensitive comparison. If found, return the correpsonding index
for ( unsigned i = 1; i <= m_nLastIndex; i++ )
{
if ( m_TitleStrings[i] )
if ( 0 == _tcsicmp( pszTitleString, m_TitleStrings[i] ) )
return i;
}
return 0;
// Loop through all the non-null string array entries, doing a case-
// insensitive comparison. If found, return the correpsonding index
for ( unsigned i = 1; i <= m_nLastIndex; i++ )
{
if ( m_TitleStrings[i] )
if ( 0 == _tcsicmp( pszTitleString, m_TitleStrings[i] ) )
return i;
}
return 0;
}