Searching...
12:26 AM

Get Total RAM and FREE RAM VB6 Code

Code:
Public Function GetRamTotalnFree() As String
    Dim TotalRam As String
    Dim FreeRam As String
    GetRAMInfo TotalRam, FreeRam
    GetRamTotalnFree = TotalRam & " / " & FreeRam

End Function

Public Sub GetRAMInfo(ByRef GetMemory As String, ByRef sFree As String)
  On Error GoTo ErrHandler

  Dim oWMI As Object
  Dim oResult As Object
  Dim oItem As Object
  Dim sSQL As String
  Dim RAMinMB
  Set oWMI = GetObject("winmgmts:")

  sSQL = "Select TotalPhysicalMemory from Win32_ComputerSystem"
  Set oResult = oWMI.ExecQuery(sSQL)
  For Each oItem In oResult
    RAMinMB = oItem.TotalPhysicalMemory / 1024 / 1024
    GetMemory = "(" & Round(RAMinMB, 0) & " MB)"
    Exit For
  Next

  sSQL = "SELECT FreePhysicalMemory from Win32_OperatingSystem"
  Set oResult = oWMI.ExecQuery(sSQL)
  For Each oItem In oResult
    sFree = FormatRam(oItem.FreePhysicalMemory * 1024)
    Exit For
  Next
  On Error GoTo 0
  
  Exit Sub

ErrHandler:
End Sub

Public Function FormatRam(ByVal oRam As Variant) As String
  If (((oRam / 1024) / 1024) / 1024) >= 1 Then
    FormatRam = Format(((oRam / 1024) / 1024) / 1024, "###,##0.0#") & " GB"
  ElseIf ((oRam / 1024) / 1024) >= 1 Then
    FormatRam = Format((oRam / 1024) / 1024, "###,##0.0#") & " MB"
  Else
    FormatRam = Format(oRam / 1024, "###,##0.0#") & " KB"
  End If
End Function

0 comments:

Post a Comment

 
Back to top!