-
Process Memory Usage
Hi All,
Just wondering if there is a way to find the CPU and Memory usage of a particular process from the code below?
(Win2K Taskmanager style.)
Thanks a million in advance,
Scott
Code:
Private Sub ListProcesses()
Dim dl&
Dim varray As Variant
Dim FinStr As String
List1.Clear
'dl = GetVersion()
Select Case WinVersion
Case 1, 2, 3 'Windows 95/98/ME
Dim f As Long, sname As String
Dim hSnap As Long, proc As PROCESSENTRY32
hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
'hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD,0)
If hSnap = hNull Then Exit Sub
proc.dwSize = Len(proc)
' Iterate through the processes
f = Process32First(hSnap, proc)
Do While f
sname = Str(proc.szExeFile)
'Debug.Print proc.th32ModuleID
With List1
.AddItem LCase$(sname)
.ItemData(.NewIndex) = proc.th32ProcessID
End With
f = Process32Next(hSnap, proc)
Loop
Case Else 'Windows NT4/2000/XP
Dim cb&, cbNeeded&, NumElements&
Dim ProcessIDs&()
Dim cbNeeded2&, NumElements2&
Dim Modules&(1 To 200), lRet&
Dim ModuleName$
Dim nSize&, hProcess&, i&
'Get the array containing the process id's for each process object
cb = 8
cbNeeded = 96
Do While cb <= cbNeeded
cb = cb * 2
ReDim ProcessIDs(cb / 4) As Long
lRet = EnumProcesses(ProcessIDs(1), cb, cbNeeded)
Loop
NumElements = cbNeeded / 4
For i = 1 To NumElements
'Get a handle to the Process
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, ProcessIDs(i))
'Got a Process handle
If hProcess <> 0 Then
'Get an array of the module handles for the specified process
lRet = EnumProcessModules(hProcess, Modules(1), 200, cbNeeded2)
'If the Module Array is retrieved, get the ModuleFileName
If lRet <> 0 Then
ModuleName = Space(MAX_PATH)
nSize = 500
lRet = GetModuleFileNameExA(hProcess, Modules(1), ModuleName, nSize)
With List1
varray = Split(ModuleName, "\")
If Len(Str(ProcessIDs(i))) = 4 Then
FinStr = " " + Str(ProcessIDs(i)) + " " + varray(UBound(varray))
.AddItem FinStr
.ItemData(.NewIndex) = ProcessIDs(i)
ElseIf Len(Str(ProcessIDs(i))) > 4 Then
FinStr = " " + Str(ProcessIDs(i)) + " " + varray(UBound(varray))
.AddItem FinStr
.ItemData(.NewIndex) = ProcessIDs(i)
End If
End With
End If
End If
'Close the handle to the process
lRet = CloseHandle(hProcess)
Next
End Select
With List1
If Plc = .ListCount Then
If .ListCount > 0 Then .ListIndex = Plc - 1
Else
If .ListCount > 0 Then .ListIndex = Plc
End If
End With
End Sub