当前位置: 主页 > 日志 > VB >

VB WMI 根据进程名获取进程ID(第一个),获取指定进行的内存使用量,获取指定进程的CPU使用率

项目中用到的代码,分享出来。  by redice  
 
'获取进程ID
Public Function getProId(proName As String) As Long
   On Error Resume Next
   Dim objWMIService As Object
   Dim colProcessList
   Dim objProcess As Object
  
   Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\. ootcimv2")
   Set colProcessList = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name=" & proName & "")
   
   For Each objProcess In colProcessList
       getProId = objProcess.ProcessId
       Set objWMIService = Nothing
       Set objProcess = Nothing
       Exit Function
   Next
End Function


'获取内存使用量
Public Function RamUsage(Optional strProcess As String = "") As String
    On Error Resume Next
   
    If strProcess = "" Then strProcess = UCase(App.EXEName) & ".EXE" '获取当前进程
   
    Dim objWMIService   As Object
    Dim colProcessList
    Dim objProcess      As Object
    Dim tUsage As Double
   
    Set objWMIService = GetObject("winmgmts:\. ootcimv2")
    Set colProcessList = objWMIService.ExecQuery("SELECT * FROM Win32_Process WHERE Name=" & strProcess & "")
   
    For Each objProcess In colProcessList
        tUsage = objProcess.workingSetSize / 1024
    Next
   
    '格式化输出
    If Int(tUsage) = tUsage Then
        If tUsage = 0 Then
           RamUsage = "0 K"
        Else
           RamUsage = Format(tUsage, "###,###") & " K"
        End If
    Else
        RamUsage = Format(tUsage, "###,###.#") & " K"
    End If
    Set objWMIService = Nothing
    Set objProcess = Nothing
End Function



' CPU使用率是通过计算得出来的
' 放在定时器中

Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long

Public Declare Function GetProcessTimes Lib "kernel32" (ByVal hProcess As Long, lpCreationTime As FILETIME, lpExitTime As FILETIME, lpKernelTime As FILETIME, lpUserTime As FILETIME) As Long

Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Public Const PROCESS_QUERY_INFORMATION = &H400


'定时刷新CPU使用率
Private Sub usageTimer_Timer()
   usageTimer.Enabled = False
  
   'CPU使用率
   Dim hId As Long
   hId = getProId(serviceExe)
  
   Dim hPro As Long
   hPro = OpenProcess(PROCESS_QUERY_INFORMATION, 0, hId)
   
   Static ftKernelStart As FILETIME
   Static ftUserStart As FILETIME
   
   Dim ftCreat As FILETIME
   Dim ftExit As FILETIME
   Dim ftKernel As FILETIME
   Dim ftUser As FILETIME
   
   bRet = GetProcessTimes(hPro, ftCreat, ftExit, ftKernel, ftUser)
   Dim PU As Double
   PU = ((ftKernel.dwLowDateTime - ftKernelStart.dwLowDateTime) + (ftUser.dwLowDateTime - ftUserStart.dwLowDateTime) / 1000) * 100
   PU = PU / 10000000
   
   If PU > 100 Then PU = 0
   
   ftKernelStart = ftKernel
   ftUserStart = ftUser
  
   cpuperclabel.Caption = CInt(PU) & "%"
  
   CloseHandle hPro '关闭进程句柄
   usageTimer.Enabled = True
End Sub

[日志信息]

该日志于 2010-03-03 16:54 由 redice 发表在 redice's Blog ,你除了可以发表评论外,还可以转载 “VB WMI 根据进程名获取进程ID(第一个),获取指定进行的内存使用量,获取指定进程的CPU使用率” 日志到你的网站或博客,但是请保留源地址及作者信息,谢谢!!    (尊重他人劳动,你我共同努力)
   
验证(必填):   点击我更换验证码

redice's Blog  is powered by DedeCms |  Theme by Monkeii.Lee |  网站地图 |  本服务器由西安鲲之鹏网络信息技术有限公司友情提供

返回顶部