如何獲取VB 6代碼中的CPU和內存使用情況?謝謝!如何在VB 6中獲取當前的CPU和RAM使用情況?
0
A
回答
1
3
確定CPU使用率上,目前該機
聲明
Option Explicit
' Note that if you declare the lpData parameter as String, you
' must pass it By Value.
Private Declare Function RegQueryValueEx Lib "advapi32.dll" _
Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName _
As String, ByVal lpReserved As Long, lpType As Long, lpData As _
Any, lpcbData As Long) As Long
Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias _
"RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, _
ByVal ulOptions As Long, ByVal samDesired As Long, _
phkResult As Long) As Long
Private Declare Function RegCloseKey Lib "advapi32.dll" _
(ByVal hKey As Long) As Long
Private Const KEY_QUERY_VALUE = &H1
Private Const HKEY_DYN_DATA As Long = &H80000006
Private Const STAT_DATA = "PerfStats\StatData"
Private Const CPU_USE = "KERNEL\CPUUsage"
代碼
Public Function CPU_Usage_Percent() As Long
On Error GoTo errorhandler:
Dim lResult As Long
Dim lData As Long
Dim lType As Long
Dim hKey As Long
lResult = RegOpenKeyEx(HKEY_DYN_DATA, STAT_DATA, _
0, KEY_QUERY_VALUE, hKey)
If lResult = 0 Then
lResult = RegQueryValueEx(hKey, CPU_USE, 0, _
lType, lData, 4)
If lResult = 0 Then
CPU_Usage_Percent = lData
lResult = RegCloseKey(hKey)
End If
End If
Exit Function
errorhandler:
On Error Resume Next
RegCloseKey hKey
Exit Function
End Function
1
這裏是如何讓你的RAM信息:
你可以把下面的代碼,只是將其放到一個形式。我將把錯誤處理留給你,因爲我希望你會僅僅以此爲例。還有一個函數可以將一個64位整數轉換成一個VB雙精度值,還可以用一個函數來格式化KB,MB和GB的數字。
Private Declare Sub GlobalMemoryStatusEx Lib "kernel32" (lpBuffer As MEMORYSTATUSEX) Private Type INT64 LoPart As Long HiPart As Long End Type Private Type MEMORYSTATUSEX dwLength As Long dwMemoryLoad As Long ulTotalPhys As INT64 ulAvailPhys As INT64 ulTotalPageFile As INT64 ulAvailPageFile As INT64 ulTotalVirtual As INT64 ulAvailVirtual As INT64 ulAvailExtendedVirtual As INT64 End Type Private Sub Form_Load() Me.AutoRedraw = True Call PrintRamInformation End Sub Private Sub PrintRamInformation() Dim udtMemStatEx As MEMORYSTATUSEX Me.Cls udtMemStatEx.dwLength = Len(udtMemStatEx) Call GlobalMemoryStatusEx(udtMemStatEx) Me.Print "Total physical memory" + vbTab + NumberInKB(CLargeInt(udtMemStatEx.ulTotalPhys.LoPart, udtMemStatEx.ulTotalPhys.HiPart)) & " (" & Round(CLargeInt(udtMemStatEx.ulAvailPhys.LoPart, udtMemStatEx.ulAvailPhys.HiPart)/(CLargeInt(udtMemStatEx.ulTotalPhys.LoPart, udtMemStatEx.ulTotalPhys.HiPart)) * 100) & "% Free)" Me.Print "Available physical memory" + vbTab + NumberInKB(CLargeInt(udtMemStatEx.ulAvailPhys.LoPart, udtMemStatEx.ulAvailPhys.HiPart)) Me.Print "Total virtual memory" + vbTab + NumberInKB(CLargeInt(udtMemStatEx.ulTotalVirtual.LoPart, udtMemStatEx.ulTotalVirtual.HiPart)) Me.Print "Available virtual memory" + vbTab + NumberInKB(CLargeInt(udtMemStatEx.ulAvailVirtual.LoPart, udtMemStatEx.ulAvailVirtual.HiPart)) Me.Print "Total page file" + vbTab + NumberInKB(CLargeInt(udtMemStatEx.ulTotalPageFile.LoPart, udtMemStatEx.ulTotalPageFile.HiPart)) Me.Print "Available page file" + vbTab + NumberInKB(CLargeInt(udtMemStatEx.ulAvailPageFile.LoPart, udtMemStatEx.ulAvailPageFile.HiPart)) Me.Print "Available extended page file" + vbTab + NumberInKB(CLargeInt(udtMemStatEx.ulAvailExtendedVirtual.LoPart, udtMemStatEx.ulAvailExtendedVirtual.HiPart)) Me.Print "Memory Load" + vbTab + CStr(udtMemStatEx.dwMemoryLoad) + "%" End Sub 'This function converts the LARGE_INTEGER data type to a double Private Function CLargeInt(Lo As Long, Hi As Long) As Double Dim dblLo As Double Dim dblHi As Double If Lo < 0 Then dblLo = 2^32 + Lo Else dblLo = Lo End If If Hi < 0 Then dblHi = 2^32 + Hi Else dblHi = Hi End If CLargeInt = dblLo + dblHi * 2^32 End Function Public Function NumberInKB(ByVal vNumber As Currency) As String Dim strReturn As String Select Case vNumber Case Is < 1024^1 strReturn = CStr(vNumber) & " bytes" Case Is < 1024^2 strReturn = CStr(Round(vNumber/1024, 1)) & " KB" Case Is < 1024^3 strReturn = CStr(Round(vNumber/1024^2, 2)) & " MB" Case Is < 1024^4 strReturn = CStr(Round(vNumber/1024^3, 2)) & " GB" End Select NumberInKB = strReturn End Function
相關問題
- 1. 獲取CPU和RAM的使用情況
- 2. 如何在Python中獲取當前的CPU和RAM使用情況?
- 3. 如何在C++中獲取當前的CPU和RAM使用情況?
- 4. 獲取C#中當前CPU,RAM和磁盤驅動器的使用情況
- 5. VB 6.0中遠程系統CPU和RAM的使用情況
- 6. 如何從QNX中獲取CPU負載/ RAM使用情況?
- 7. 使用ruby或cli獲取當前的cpu使用情況
- 8. 使用WMI獲取C#中每個進程的CPU和RAM使用情況?
- 9. 如何使用.NET CORE獲取C#Web應用程序當前的CPU/RAM /磁盤使用情況?
- 10. 如何使用PowerShell查找CPU和RAM使用情況?
- 11. 如何處理函數的RAM和CPU使用情況?
- 12. 如何記錄Linux進程的RAM和CPU使用情況
- 13. 獲取當前pthread cpu使用情況Mac OS X
- 14. 通過Java獲得當前JVM的真實RAM使用情況
- 15. 在asp.net框架中使用c#獲取當前cpu使用情況
- 16. AS400 jt400獲取RAM使用情況
- 17. 在Android上獲取CPU,RAM和網絡使用情況統計信息
- 18. Amazon EC2 - 如何通過AWS API獲得RAM和CPU的使用情況?
- 19. 在C++/windows中獲取當前cpu使用情況的具體過程
- 20. 如何獲取C#(託管代碼)中的* THREAD *的CPU使用率和/或RAM使用情況?
- 21. 如何在批處理文件中獲取當前的CPU使用情況和可用內存?
- 22. 如何獲取我的程序使用的RAM和CPU?
- 23. 如何在容器上的特定進程上獲取RAM,CPU和網絡使用情況
- 24. Python:如何獲取當前的帶寬使用情況?
- 25. 如何獲得Apache Cordova中的CPU和內存使用情況?
- 26. 度量以計算和比較Android中的RAM和CPU使用情況
- 27. 如何獲取CPU溫度以及CPU和內存使用情況?
- 28. 如何在Android上獲取CPU使用情況統計信息?
- 29. 如何從aws服務器詳細信息(如cpu ram和硬盤使用情況)獲取實時數據
- 30. 如何獲取作爲taskmgr的CPU使用情況? C#
這不適用於Windows 7 – 2015-08-10 22:47:43