}
// next string
p += (strlen(p) + 1);
}
// free the counter names buffer
free( buf );
// allocate the initial buffer for the performance data
dwSize = INITIAL_SIZE;
buf = (LPSTR) malloc( dwSize );
while (TRUE)
{
if (buf == NULL)
__leave;
memset( buf, 0, dwSize );
rc=RegQueryValueEx(ghPerfKey,szSubKey,NULL,&dwType,(LPBYTE) buf,&dwSize);
pPerf = (PPERF_DATA_BLOCK) buf;
// check for success and valid perf data block signature
if ((rc == ERROR_SUCCESS) &&
(dwSize > 0) &&
(pPerf)->Signature[0] == (WCHAR)''P'' &&
(pPerf)->Signature == (WCHAR)''E'' &&
(pPerf)->Signature == (WCHAR)''R'' &&
(pPerf)->Signature == (WCHAR)''F'' )
break;
// if buffer is not big enough, reallocate and try again
if (rc == ERROR_MORE_DATA)
{
dwSize += EXTEND_SIZE;
buf = (LPSTR) realloc( buf, dwSize );
}
else __leave;
}
// set the perf_object_type pointer
pObj = (PPERF_OBJECT_TYPE) ((DWORD)pPerf + pPerf->HeaderLength);
//loop thru the performance counter definition records looking
//for the process id counter and then save its offset
pCounterDef = (PPERF_COUNTER_DEFINITION) ((DWORD)pObj + pObj->HeaderLength);
for (i=0; i<(DWORD)pObj->NumCounters; i++)
{
if (pCounterDef->CounterNameTitleIndex == dwProcessIdTitle)
{
dwProcessIdCounter = pCounterDef->CounterOffset;
break;
}
pCounterDef++;
}
pInst = (PPERF_INSTANCE_DEFINITION) ((DWORD)pObj + pObj->DefinitionLength);
// loop thru the performance instance data extracting each process name
// and process id
for (i=0; i < (DWORD)pObj->NumInstances-1 && i<MaxProcessNum; i++)
{
// pointer to the process name
p = (LPSTR) ((DWORD)pInst + pInst->NameOffset);
// convert it to ascii