VBA 用于监控 windows 进程的代码显示在任务管理器中

VBA code to monitor windows process displayed in task manager

VBA 用于监视任务管理器中显示的 windows 进程的代码。

我正在尝试获取应用程序的 RAM 使用情况和 CPU 使用情况,并将这些值添加到 excel。

我尝试使用 WMI class 如下,但是我正在获取进程 ID.I 无法检索 RAM 使用情况和 CPU 使用情况。 谁能帮我解决这个问题?

Sub test2()
    Set objWMIService = GetObject("winmgmts:\.\root\CIMV2")
    Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Process where caption='excel.exe'", , 48)
    For Each objItem In colItems
        Sheet1.Range("d2").Value = objItem.ProcessId
    Next

    Set colItems = objWMIService.ExecQuery( _"SELECT * FROM Win32_PerfFormattedData_PerfProc_Process where IDProcess=" & Sheet1.Range("d2").Value, , 48)

    For Each objItem In colItems
        Sheet1.Range("A1").Value = "PercentProcessorTime: " & objItem.PercentProcessorTime
    Next

End Sub

如果您选择走 WMI 路线,那么您可能会在 Win32_Process class 的 WorkingSetSize 属性 之后:

WorkingSetSize Data type: uint64 Access type: Read-only Qualifiers: DisplayName ("Working Set Size"), Units ("bytes") Amount of memory in bytes that a process needs to execute efficiently—for an operating system that uses page-based memory management. If the system does not have enough memory (less than the working set size), thrashing occurs. If the size of the working set is not known, use NULL or 0 (zero). If working set data is provided, you can monitor the information to understand the changing memory requirements of a process.

和Win32_PerfFormattedData_PerfProc_Processclass的PercentProcessorTime

PercentProcessorTime Data type: uint64 Access type: Read-only Qualifiers: CookingType ("PERF_100NSEC_TIMER") , Counter ("PercentProcessorTime") , PerfTimeStamp ("TimeStamp_Sys100NS") , PerfTimeFreq ("Frequency_Sys100NS") Percentage of time that the processor is executing a non-idle thread. This property was designed as a primary indicator of processor activity. It is calculated by measuring the time that the processor spends executing the thread of the idle process in each sample interval, and subtracting that value from 100%. (Each processor has an idle thread which consumes cycles when no other threads are ready to run.) It can be viewed as the percentage of the sample interval spent doing useful work. This property displays the average percentage of busy time observed during the sample interval. It is calculated by monitoring the time the service was inactive, and then subtracting that value from 100%.

但您可能还想查看 SWbemRefresher 对象 (https://msdn.microsoft.com/en-us/library/aa393838(v=vs.85).aspx)。

骨架 VBA 供您使用的代码:

Dim srvEx As SWbemServicesEx
Dim xlProcSet As SWbemObjectSet
Dim xlPerfSet As SWbemObjectSet
Dim objEx As SWbemObjectEx

Set srvEx = GetObject("winmgmts:\.\root\CIMV2")
Set xlProcSet = srvEx.ExecQuery("SELECT * FROM Win32_Process WHERE name = 'EXCEL.EXE'")
Set xlPerfSet = srvEx.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfProc_Process WHERE name = 'EXCEL'")

For Each objEx In xlProcSet
    Debug.Print objEx.Name & " RAM: " & objEx.WorkingSetSize / 1024 & "kb"
Next

For Each objEx In xlPerfSet
    Debug.Print objEx.Name & " CPU: " & objEx.PercentProcessorTime & "%"
Next

这些功能允许根据要求监控 RAM 和 CPU 使用情况。请注意,一个中的应用程序名称区分大小写并包含扩展名,另一个中的应用程序名称不区分大小写且不包含扩展名。

Debug.Print ProcessCpuUsage("Excel")
Debug.Print ProcessMemoryUsage("EXCEL.EXE") / 1024

Function ProcessCpuUsage(ProcessName As String) As Double
  Dim Wmi As Object, Processes As Variant, Process As Object
  Set Wmi = GetObject("winmgmts:")
  Set Processes = Wmi.ExecQuery("SELECT PercentProcessorTime FROM win32_PerfFormattedData_PerfProc_Process WHERE Name='" & ProcessName & "'")
  For Each Process In Processes
    Exit For
  Next Process
  ProcessCpuUsage = Process.PercentProcessorTime
End Function

Function ProcessMemoryUsage(ProcessName As String) As Double
  Dim Wmi As Object, Processes As Variant, Process As Object
  Set Wmi = GetObject("winmgmts:")
  Set Processes = Wmi.ExecQuery("SELECT WorkingSetSize FROM Win32_Process WHERE Name='" & ProcessName & "'")
  For Each Process In Processes
    Exit For
  Next Process
  ProcessMemoryUsage = Process.WorkingSetSize
End Function