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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I monitor CPU Usage by PID

Status
Not open for further replies.
May 14, 2003
3
US
Hello Everyone,

I would like to know if anyone knows how to monitor cpu usage by PID. I already know how to monitor it by the process name using the PDH.dll, but I REALLY need to get results for specific PIDs.

More Dazed and Confused than ever,
Chris
 
I know this is a little off topic, but you metioned it. How do you determine the cpu usage of a process?
 
Code:
Option Explicit
Private Declare Function GetProcessTimes Lib "kernel32" (ByVal hProcess As Long, lpCreationTime As FILETIME, lpExitTime As FILETIME, lpKernelTime As FILETIME, lpUserTime As FILETIME) As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function FileTimeToLocalFileTime Lib "kernel32" (lpFileTime As FILETIME, lpLocalFileTime As FILETIME) As Long
Private Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long

Private Type FILETIME
        dwLowDateTime As Long
        dwHighDateTime As Long
End Type

Private Type SYSTEMTIME
    wYear As Integer
    wMonth As Integer
    wDayOfWeek As Integer
    wDay As Integer
    wHour As Integer
    wMinute As Integer
    wSecond As Integer
    wMilliseconds As Integer
End Type

' Sneaky type that will allow us to neatly convert the double words into a nicely scaled currency value
' representing milliseconds
Private Type DWORDPROXIE
    Value As Currency
End Type

Private Type ProcessTimes
    CreationTime As SYSTEMTIME ' Time process started
    ExitTime As SYSTEMTIME ' Generally not useful
    KernelTime As Currency ' elapsed time in milliseconds
    UserTime As Currency ' elapsed time in milliseconds
End Type


Private Function vbGetProcessTimes(hProcess As Long) As ProcessTimes
    Dim CreationTime As FILETIME
    Dim ExitTime As FILETIME
    Dim KernelTime As FILETIME
    Dim UserTime As FILETIME
    Dim result As Long
    Dim UsableTime As SYSTEMTIME
    Dim TempTime As DWORDPROXIE

    GetProcessTimes hProcess, CreationTime, ExitTime, KernelTime, UserTime

    ' Some time conversions
    FileTimeToLocalFileTime CreationTime, CreationTime
    FileTimeToLocalFileTime ExitTime, ExitTime ' Don't really care about this one generally
    FileTimeToSystemTime CreationTime, vbGetProcessTimes.CreationTime
    FileTimeToSystemTime ExitTime, vbGetProcessTimes.ExitTime
    
    LSet TempTime = KernelTime
    vbGetProcessTimes.KernelTime = TempTime.Value
    LSet TempTime = UserTime
    vbGetProcessTimes.UserTime = TempTime.Value

End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top