Code: Select all
VBSTART
'Two Subs which output data in message boxes to demonstrate how to
'get service and process information.
'displays each service and it's status
Sub Services
strComputer = "."
Set wbemServices = GetObject("winmgmts:\\" & strComputer)
Set wbemObjectSet = wbemServices.InstancesOf("Win32_Service")
For Each wbemObject In wbemObjectSet
MsgBox "Display Name: " & wbemObject.DisplayName & vbCrLf & _
" State: " & wbemObject.State & vbCrLf & _
" Start Mode: " & wbemObject.StartMode
Next
End Sub
Sub Processes
'displays each process, it's size
'lists each process and the cpu time it has used.
sComputer = "."
Set oWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & sComputer & "\root\cimv2")
Set colProcessList = oWMIService.ExecQuery _
("Select Name, WorkingSetSize from Win32_Process")
For Each oProcess in colProcessList
MsgBox oProcess.Name & "," & oProcess.WorkingSetSize / 1024
Next
'The current CPU percentage usage vs. other processes is not available with WMI,
'only the total CPU time each process has used (kernelmodetime + usermodetime):
Set oWMI = GetObject("winmgmts:")
sWQL = "select name, kernelmodetime, usermodetime " _
& " from win32_process"
Set oResults = oWMI.ExecQuery(sWQL)
For Each oProcess In oResults
MsgBox "name: " & oProcess.name & _
",kernelmodetime: " & oProcess.kernelmodetime & _
",usermodetime: " & oProcess.usermodetime
Next
End Sub
VBEND
VBRun>Services
VBRun>Processes
Code: Select all
VBSTART
'returns the number of copies of ProcessName that are running
'will return 0 if ProcessName is not running
Function IsProcessRunning(ProcessName)
Set oWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colProcessList = oWMIService.ExecQuery ("Select Name from Win32_Process where Name='" & ProcessName & "'")
IsProcessRunning = colProcessList.count
End Function
VBEND
VBEval>IsProcessRunning("opera.exe"),res
MessageModal>res