项目中用到的代码,分享出来。 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
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
呵呵,谢谢
VaTG790i.最好的<a href=http://www.kyfei.com>网站推广软件</a>,
非常好
....................
;ui;普i;uighur;ui;ui;个
在unix网络编程中看到了关于TCP/IP的一些内容,我感觉还是写的不够。正在下载中,一定
下载地址呢