PDHGetCounterValue
Function Prototype
PDHGetCounterValue(anCounterID : number, anCounterValue : number):number;
Parameters
anCounterID (in) - Identifier of the counter for which a value is to be returned. This counter ID is returned when PDHCreateCounter is called.
anCounterValue (out) - The value of the counter requested
Return Value
number : the return value will be <0 if an error occurred.
Description
Before using PDHGetCounterValue it is necessary to first create a query with PDHCreateQuery, create counters in the query with PDHCreateCounter, and populate the counters with data using PDHCollectData.
Having laid the groundwork using these functions and supplied with a valid counter ID, PDHGetCounter will return the value of the requested counter in the argument anCounterValue.
Example
The following Sentinel example uses PDHCreateQuery, PDHCreateCounter, PDHCollectData, PDHGetCounterValue and PDHDeleteQuery to retrieve the value of the Disk Time performance counters for physical disk 0 on the local machine and Processor 0 User Time on a workstation called "roma".
You might define a function like this within CustomSerioScriptFunctions
function pdhExample():number
begin
//declarations
lnQueryID:number;
lnaCounterIDs:number array;
lsaPaths: string array;
lnIndex: number;
lnCounterValue:number;
//set up arrays
setArrayLength(lnaCounterIDs, 2);
setArrayLength(lsaPaths, 2);
lsaPaths[1]:= "\\roma\Processor(0)\% User Time";
lsaPaths[2]:= "\PhysicalDisk(0)\% Disk Time";
//create query
lnQueryID := PDHCreateQuery();
if (lnQueryID < 0) then
begin
//error
print("Can't create query ID");
return(-1);
end;
//create counters in query
for lnIndex := 1 to getArrayLength(lnaCounterIDs)
begin
lnaCounterIDs[lnIndex] := PDHCreateCounter(lnQueryID, lsaPaths[lnIndex]);
if (lnaCounterIDs[lnIndex] = -1) then
begin
print("The following path is invalid: " + lsaPaths[lnIndex]);
return (-1);
end;
end;
//collect the data for the query
if (PDHCollectData(lnQueryID) < 0) then
begin
print("The call to PDHCollectData failed");
return (-1);
end;
//print the results
for lnIndex := 1 to getArrayLength(lnaCounterIDs)
begin
if (PDHGetCounterValue(lnaCounterIDs[lnIndex], lnCounterValue) < 0) then
begin
print ("PDHGetCounterValue failed");
return(-1);
end
else
begin
print(lsaPaths[lnIndex] + " = " + numToString(lnCounterValue, 2));
end;
end;
//close the query
if (PDHDeleteQuery(lnQueryID) < 0) then
begin
print("Failed call to PDHDeleteQuery");
end;
end;