Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding CPU Utilization 1

Status
Not open for further replies.

KerryG

MIS
Apr 23, 1999
2
0
0
US
I am trying to find some code to retrieve the CPU utilization like perfmon does. Any tips would be appreciated.<br>
-Kerry
 
The following code works for me:<br>
<br>
Put this code in .bas Module:<br>
<br>
Private Declare Function PdhConnectMachine Lib "Pdh.Dll" Alias "PdhConnectMachineA" (ByVal MachName As String) As Long<br>
<br>
<br>
'<br>
' Visual Basic 4.0 definitions file for use with<br>
' PDH.DLL the Peformance Data Helper DLL<br>
'<br>
' Copyright (c) 1996 by Microsoft Corporation<br>
'<br>
' 19-Apr-1996 Bob Watson<br>
'<br>
' = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =<br>
'<br>
' Pdh Error status return values<br>
'<br>
Private Const ERROR_SUCCESS = 0<br>
Private Const PDH_CSTATUS_VALID_DATA = &H20000000<br>
Private Const PDH_CSTATUS_NEW_DATA = &H20000001<br>
Private Const PDH_CSTATUS_NO_MACHINE = &HA00007D0<br>
Private Const PDH_CSTATUS_NO_INSTANCE = &HA00007D1<br>
Private Const PDH_MORE_DATA = &HA00007D2<br>
Private Const PDH_CSTATUS_ITEM_NOT_VALIDATED = &HA00007D3<br>
Private Const PDH_RETRY = &HA00007D4<br>
Private Const PDH_NO_DATA = &HA00007D5<br>
Private Const PDH_CALC_NEGATIVE_DENOMINATOR = &HA00007D6<br>
Private Const PDH_CALC_NEGATIVE_TIMEBASE = &HA00007D7<br>
Private Const PDH_CALC_NEGATIVE_VALUE = &HA00007D8<br>
Private Const PDH_DIALOG_CANCELLED = &HA00007D9<br>
Private Const PDH_CSTATUS_NO_OBJECT = &HE0000BB8<br>
Private Const PDH_CSTATUS_NO_COUNTER = &HE0000BB9<br>
Private Const PDH_CSTATUS_INVALID_DATA = &HE0000BBA<br>
Private Const PDH_MEMORY_ALLOCATION_FAILURE = &HE0000BBB<br>
Private Const PDH_INVALID_HANDLE = &HE0000BBC<br>
Private Const PDH_INVALID_ARGUMENT = &HE0000BBD<br>
Private Const PDH_FUNCTION_NOT_FOUND = &HE0000BBE<br>
Private Const PDH_CSTATUS_NO_COUNTERNAME = &HE0000BBF<br>
Private Const PDH_CSTATUS_BAD_COUNTERNAME = &HE0000BC0<br>
Private Const PDH_INVALID_BUFFER = &HE0000BC1<br>
Private Const PDH_INSUFFICIENT_BUFFER = &HE0000BC2<br>
Private Const PDH_CANNOT_CONNECT_MACHINE = &HE0000BC3<br>
Private Const PDH_INVALID_PATH = &HE0000BC4<br>
Private Const PDH_INVALID_INSTANCE = &HE0000BC5<br>
Private Const PDH_INVALID_DATA = &HE0000BC6<br>
Private Const PDH_NO_DIALOG_DATA = &HE0000BC7<br>
Private Const PDH_CANNOT_READ_NAME_STRINGS = &HE0000BC8<br>
'<br>
' Counter Browser Detail Level Definitions<br>
'<br>
Private Const PERF_DETAIL_NOVICE = 100<br>
Private Const PERF_DETAIL_ADVANCED = 200<br>
Private Const PERF_DETAIL_EXPERT = 300<br>
Private Const PERF_DETAIL_WIZARD = 400<br>
'<br>
' Standard PDH Function prototypes<br>
'<br>
Private Declare Function PdhOpenQuery Lib "Pdh.Dll" Alias "PdhVbOpenQuery" (ByRef QueryHandle As Long) As Long<br>
' PdhOpenQuery Creates an empty performance data query structure<br>
' QueryHandle The Id of the query created is returned in this buffer<br>
' Return Value an error code from the list above. ERROR_SUCCESS indicates a valid query was created and the value returned in QueryHandle is valid<br>
Private Declare Function PdhAddCounter Lib "Pdh.Dll" Alias "PdhVbAddCounter" (ByVal QueryHandle As Long, ByVal CounterPath As String, ByRef CounterHandle As Long) As Long<br>
' PdhAddCounter Adds a counter to the specified query<br>
' QueryHandle the QueryHandle of the query to add the counter to. This is the value initialized by the PdhOpenQuery function<br>
' CounterPath the counter path string of the counter to add. This value can be obtained by either the PdhGetOneCounterPath or the PdhCreateCounterPathList functions<br>
' CounterHandle The handle to the counter that is added to the query<br>
' Return Value an error code from the list above. ERROR_SUCCESS indicates a valid counter was created<br>
Private Declare Function PdhRemoveCounter Lib "Pdh.Dll" (ByVal CounterHandle As Long) As Long<br>
' PdhRemoveCounter Removes the specified counter from the query to which it belongs<br>
' CounterHandle Handle of the counter to remove<br>
' Return Value an error code from the list above. ERROR_SUCCESS indicates the counter was removed<br>
Private Declare Function PdhCollectQueryData Lib "Pdh.Dll" (ByVal QueryHandle As Long) As Long<br>
' PdhCollectQueryData Obtains the current values of each counter in the query (the value of each counter must be retrieved individually using the PdhGetDoubleCounterValue, after the data has been updated)<br>
' QueryHandle Handle of the query to refresh<br>
' Return Value an error code from the list above. ERROR_SUCCESS indicates the function completed successfully<br>
Private Declare Function PdhCloseQuery Lib "Pdh.Dll" (ByVal QueryHandle As Long) As Long<br>
' PdhCloseQuery Terminates data collection and frees the resources associated with the query<br>
' QueryHandle Handle of query to close and free<br>
' Return Value an error code from the list above. ERROR_SUCCESS indicates the query was freed<br>
'<br>
' DPH VB Helper Functions<br>
'<br>
Private Declare Function PdhGetDoubleCounterValue Lib "Pdh.Dll" Alias "PdhVbGetDoubleCounterValue" (ByVal CounterHandle As Long, ByRef CounterStatus As Long) As Double<br>
' PdhGetDoubleCounterValue retrieves the value of the specified counter as a double precision real number<br>
' CounterHandle The handle of the counter to retrive<br>
' CounterStatus returns the current status of the counter as a PDH_ status value defined above<br>
' Return Value The double precision real value of the counter<br>
Private Declare Function PdhGetOneCounterPath Lib "Pdh.Dll" Alias "PdhVbGetOneCounterPath" (ByVal PathString As String, ByVal PathLength As Long, ByVal DetailLevel As Long, ByVal CaptionString As String) As Long<br>
' PdhGetOneCounterPath Displays a dialog box prompting the user to select one counter path<br>
' PathString Preinitialized buffer to retrieve the path string<br>
' PathLength Size of initialized buffer<br>
' DetailLevel Level of detail to filter counter display. 0 shows all counters and hides detail combo box.<br>
' CaptionString String to display in caption bar of browser dialog box<br>
' Return Value returns the number of characters written to the PathString buffer.<br>
Private Declare Function PdhCreateCounterPathList Lib "Pdh.Dll" Alias "PdhVbCreateCounterPathList" (ByVal DetailLevel As Long, ByVal CaptionString As String) As Long<br>
' PdhCreateCounterPathList Displays a dialog box prompting the user to select multiple counters Use the next function to read the selected path strings<br>
' DetailLevel Level of detail to filter counter display. 0 shows all counters and hides detail combo box.<br>
' CaptionString String to display in caption bar of browser dialog box<br>
' Return Value returns the number of path strings selected<br>
Private Declare Function PdhGetCounterPathFromList Lib "Pdh.Dll" Alias "PdhVbGetCounterPathFromList" (ByVal Index As Long, ByVal Buffer As String, ByVal BufferLength As Long) As Long<br>
' PdhGetCounterPathFromList Retrieves an individual path string from the list created by the PdhCreateCounterPathList function call<br>
' Index Index of the path string to retrieve starting at 1 and going to the value returned by the previous call to PdhCreateCounterPathList<br>
' Buffer An initialized string buffer to retrieve the selected string<br>
' BufferLength The maximum size of the buffer<br>
' Return Value The number of characters copied to Buffer<br>
Private Declare Function PdhGetCounterPathElements Lib "Pdh.Dll" Alias "PdhVbGetCounterPathElements" (ByVal PathString As String, ByVal MachineName As String, ByVal ObjectName As String, ByVal InstanceName As String, ByVal ParentInstance As String, ByVal CounterName As String, ByVal BufferSize As Long) As Long<br>
' PdhGetCounterPathElements Reads a performance counter path string and returns the components as individual strings<br>
' PathString The full path string to read<br>
' MachineName The buffer to receive the machine name<br>
' ObjectName The buffer to receive the object name<br>
' InstanceName The buffer to receive the instance name<br>
' ParentInstance The buffer to receive the parent instance<br>
' CounterName The buffer to receive the counter name<br>
' BufferSize The size of the string buffers<br>
' ReturnValue DPH status code. 0 means the function succeeded.<br>
Private Declare Function PdhIsGoodStatus Lib "Pdh.Dll" Alias "PdhVbIsGoodStatus" (ByVal StatusValue As Long) As Long<br>
' PdhIsGoodStatus tests the status value returned by a PDH function to determine if the function or value returned is good/success (nozero) or bad/error (zero)<br>
' StatusValue the value returned by a call to a PDH function<br>
' Return Value 0 if the status is not good non-zero if the status is good<br>
<br>
<br>
Public Function PerfData(ByVal MachName As String, ByVal Object As String, ByVal Instance As String, ByVal Counter As String, ByRef QHndl As Long, ByRef CHndl As Long) As Long<br>
Static lbBeenHere As Boolean<br>
Dim llReturn As Long<br>
Dim ldReturn As Double<br>
Dim lsPath As String<br>
'Dim qHndl As Long<br>
'Dim cHndl As Long<br>
Dim cStat As Long<br>
<br>
On Error GoTo ProcError<br>
<br>
If QHndl &gt; 0 Then<br>
llReturn = PdhCollectQueryData(QHndl)<br>
ldReturn = PdhGetDoubleCounterValue(CHndl, cStat)<br>
PerfData = CLng(ldReturn)<br>
Else<br>
llReturn = PdhConnectMachine(MachName)<br>
llReturn = PdhOpenQuery(QHndl)<br>
llReturn = PdhAddCounter(QHndl, "\\" & MachName & "\" & Object & "(" & Instance & ")\" & Counter, CHndl)<br>
llReturn = PdhCollectQueryData(QHndl)<br>
ldReturn = PdhGetDoubleCounterValue(CHndl, cStat)<br>
PerfData = CLng(ldReturn)<br>
lbBeenHere = True<br>
End If<br>
<br>
Exit Function<br>
ProcError:<br>
MsgBox "PerfData Error " & Err.Description<br>
End Function<br>
<br>
I would have listed the code to use this function but it is specialized for MSMQ. Let me know if you need it.<br>

 
I am just a little lost as the the parameters to pass to the PerfData function. MachName is pretty obvious, other than that, Im a bit confused. Do you have a single example of reference that function?<br>
Thanks.<br>
-Kerry<br>
kerryg@deltanet.com
 
refer to Performance monitor for the values in the parameters.<br>
<br>
Data = PerfData("MachName", "Object", "Instance", "Counter", QHndl, CHndl)<br>
<br>
You have to reuse the QHndl and CHndl or you will run out of resources.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top