×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Finding CPU Utilization

Finding CPU Utilization

Finding CPU Utilization

(OP)
I am trying to find some code to retrieve the CPU utilization like perfmon does. Any tips would be appreciated.
-Kerry

RE: Finding CPU Utilization

The following code works for me:

Put this code in .bas Module:

Private Declare Function PdhConnectMachine Lib "Pdh.Dll" Alias "PdhConnectMachineA" (ByVal MachName As String) As Long


'
' Visual Basic 4.0 definitions file for use with
' PDH.DLL the Peformance Data Helper DLL
'
' Copyright (c) 1996 by Microsoft Corporation
'
' 19-Apr-1996 Bob Watson
'
' = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
'
' Pdh Error status return values
'
Private Const ERROR_SUCCESS = 0
Private Const PDH_CSTATUS_VALID_DATA = &H20000000
Private Const PDH_CSTATUS_NEW_DATA = &H20000001
Private Const PDH_CSTATUS_NO_MACHINE = &HA00007D0
Private Const PDH_CSTATUS_NO_INSTANCE = &HA00007D1
Private Const PDH_MORE_DATA = &HA00007D2
Private Const PDH_CSTATUS_ITEM_NOT_VALIDATED = &HA00007D3
Private Const PDH_RETRY = &HA00007D4
Private Const PDH_NO_DATA = &HA00007D5
Private Const PDH_CALC_NEGATIVE_DENOMINATOR = &HA00007D6
Private Const PDH_CALC_NEGATIVE_TIMEBASE = &HA00007D7
Private Const PDH_CALC_NEGATIVE_VALUE = &HA00007D8
Private Const PDH_DIALOG_CANCELLED = &HA00007D9
Private Const PDH_CSTATUS_NO_OBJECT = &HE0000BB8
Private Const PDH_CSTATUS_NO_COUNTER = &HE0000BB9
Private Const PDH_CSTATUS_INVALID_DATA = &HE0000BBA
Private Const PDH_MEMORY_ALLOCATION_FAILURE = &HE0000BBB
Private Const PDH_INVALID_HANDLE = &HE0000BBC
Private Const PDH_INVALID_ARGUMENT = &HE0000BBD
Private Const PDH_FUNCTION_NOT_FOUND = &HE0000BBE
Private Const PDH_CSTATUS_NO_COUNTERNAME = &HE0000BBF
Private Const PDH_CSTATUS_BAD_COUNTERNAME = &HE0000BC0
Private Const PDH_INVALID_BUFFER = &HE0000BC1
Private Const PDH_INSUFFICIENT_BUFFER = &HE0000BC2
Private Const PDH_CANNOT_CONNECT_MACHINE = &HE0000BC3
Private Const PDH_INVALID_PATH = &HE0000BC4
Private Const PDH_INVALID_INSTANCE = &HE0000BC5
Private Const PDH_INVALID_DATA = &HE0000BC6
Private Const PDH_NO_DIALOG_DATA = &HE0000BC7
Private Const PDH_CANNOT_READ_NAME_STRINGS = &HE0000BC8
'
' Counter Browser Detail Level Definitions
'
Private Const PERF_DETAIL_NOVICE = 100
Private Const PERF_DETAIL_ADVANCED = 200
Private Const PERF_DETAIL_EXPERT = 300
Private Const PERF_DETAIL_WIZARD = 400
'
' Standard PDH Function prototypes
'
Private Declare Function PdhOpenQuery Lib "Pdh.Dll" Alias "PdhVbOpenQuery" (ByRef QueryHandle As Long) As Long
' PdhOpenQuery Creates an empty performance data query structure
' QueryHandle The Id of the query created is returned in this buffer
' 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
Private Declare Function PdhAddCounter Lib "Pdh.Dll" Alias "PdhVbAddCounter" (ByVal QueryHandle As Long, ByVal CounterPath As String, ByRef CounterHandle As Long) As Long
' PdhAddCounter Adds a counter to the specified query
' QueryHandle the QueryHandle of the query to add the counter to. This is the value initialized by the PdhOpenQuery function
' CounterPath the counter path string of the counter to add. This value can be obtained by either the PdhGetOneCounterPath or the PdhCreateCounterPathList functions
' CounterHandle The handle to the counter that is added to the query
' Return Value an error code from the list above. ERROR_SUCCESS indicates a valid counter was created
Private Declare Function PdhRemoveCounter Lib "Pdh.Dll" (ByVal CounterHandle As Long) As Long
' PdhRemoveCounter Removes the specified counter from the query to which it belongs
' CounterHandle Handle of the counter to remove
' Return Value an error code from the list above. ERROR_SUCCESS indicates the counter was removed
Private Declare Function PdhCollectQueryData Lib "Pdh.Dll" (ByVal QueryHandle As Long) As Long
' 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)
' QueryHandle Handle of the query to refresh
' Return Value an error code from the list above. ERROR_SUCCESS indicates the function completed successfully
Private Declare Function PdhCloseQuery Lib "Pdh.Dll" (ByVal QueryHandle As Long) As Long
' PdhCloseQuery Terminates data collection and frees the resources associated with the query
' QueryHandle Handle of query to close and free
' Return Value an error code from the list above. ERROR_SUCCESS indicates the query was freed
'
' DPH VB Helper Functions
'
Private Declare Function PdhGetDoubleCounterValue Lib "Pdh.Dll" Alias "PdhVbGetDoubleCounterValue" (ByVal CounterHandle As Long, ByRef CounterStatus As Long) As Double
' PdhGetDoubleCounterValue retrieves the value of the specified counter as a double precision real number
' CounterHandle The handle of the counter to retrive
' CounterStatus returns the current status of the counter as a PDH_ status value defined above
' Return Value The double precision real value of the counter
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
' PdhGetOneCounterPath Displays a dialog box prompting the user to select one counter path
' PathString Preinitialized buffer to retrieve the path string
' PathLength Size of initialized buffer
' DetailLevel Level of detail to filter counter display. 0 shows all counters and hides detail combo box.
' CaptionString String to display in caption bar of browser dialog box
' Return Value returns the number of characters written to the PathString buffer.
Private Declare Function PdhCreateCounterPathList Lib "Pdh.Dll" Alias "PdhVbCreateCounterPathList" (ByVal DetailLevel As Long, ByVal CaptionString As String) As Long
' PdhCreateCounterPathList Displays a dialog box prompting the user to select multiple counters Use the next function to read the selected path strings
' DetailLevel Level of detail to filter counter display. 0 shows all counters and hides detail combo box.
' CaptionString String to display in caption bar of browser dialog box
' Return Value returns the number of path strings selected
Private Declare Function PdhGetCounterPathFromList Lib "Pdh.Dll" Alias "PdhVbGetCounterPathFromList" (ByVal Index As Long, ByVal Buffer As String, ByVal BufferLength As Long) As Long
' PdhGetCounterPathFromList Retrieves an individual path string from the list created by the PdhCreateCounterPathList function call
' Index Index of the path string to retrieve starting at 1 and going to the value returned by the previous call to PdhCreateCounterPathList
' Buffer An initialized string buffer to retrieve the selected string
' BufferLength The maximum size of the buffer
' Return Value The number of characters copied to Buffer
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
' PdhGetCounterPathElements Reads a performance counter path string and returns the components as individual strings
' PathString The full path string to read
' MachineName The buffer to receive the machine name
' ObjectName The buffer to receive the object name
' InstanceName The buffer to receive the instance name
' ParentInstance The buffer to receive the parent instance
' CounterName The buffer to receive the counter name
' BufferSize The size of the string buffers
' ReturnValue DPH status code. 0 means the function succeeded.
Private Declare Function PdhIsGoodStatus Lib "Pdh.Dll" Alias "PdhVbIsGoodStatus" (ByVal StatusValue As Long) As Long
' 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)
' StatusValue the value returned by a call to a PDH function
' Return Value 0 if the status is not good non-zero if the status is good


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
Static lbBeenHere As Boolean
Dim llReturn As Long
Dim ldReturn As Double
Dim lsPath As String
'Dim qHndl As Long
'Dim cHndl As Long
Dim cStat As Long

On Error GoTo ProcError

If QHndl > 0 Then
llReturn = PdhCollectQueryData(QHndl)
ldReturn = PdhGetDoubleCounterValue(CHndl, cStat)
PerfData = CLng(ldReturn)
Else
llReturn = PdhConnectMachine(MachName)
llReturn = PdhOpenQuery(QHndl)
llReturn = PdhAddCounter(QHndl, "\\" & MachName & "\" & Object & "(" & Instance & ")\" & Counter, CHndl)
llReturn = PdhCollectQueryData(QHndl)
ldReturn = PdhGetDoubleCounterValue(CHndl, cStat)
PerfData = CLng(ldReturn)
lbBeenHere = True
End If

Exit Function
ProcError:
MsgBox "PerfData Error " & Err.Description
End Function

I would have listed the code to use this function but it is specialized for MSMQ. Let me know if you need it.

RE: Finding CPU Utilization

(OP)
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?
Thanks.
-Kerry
kerryg@deltanet.com

RE: Finding CPU Utilization

refer to Performance monitor for the values in the parameters.

Data = PerfData("MachName", "Object", "Instance", "Counter", QHndl, CHndl)

You have to reuse the QHndl and CHndl or you will run out of resources.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close