Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1448

Getting OS, Ram, CPU information

$
0
0
Over the years I have used many methods for getting the above information. None have been entirely satisfactory or involved long code.

Recently I came across the WMI classes that are part of all Windows machines (certainly since XP) and make the job quite simple.

Here is a quick demo with a very simple presentation via a Message box. I urge you to investigate for yourselves all the classes available.

All my tests have been on Win10, so I would be grateful if others would report their findings.

Code:

Option Explicit

Private Sub Form_Load()
    Dim Results As Object, Info As Object, PCInfo As String, Ram As String, TotMem As Long

    ' Get the Memory information. For more information from this query, see: https://msdn.microsoft.com/en-us/library/aa394347(v=vs.85).aspx
    Set Results = GetObject("Winmgmts:").ExecQuery("SELECT Capacity FROM Win32_PhysicalMemory")
    For Each Info In Results
        TotMem = TotMem + (Info.Capacity / 1073741824) 'Capacity returns the size separately for each stick in bytes. Therefore we loop and add whilst dividing by 1GB.
    Next Info

    ' Get the O.S. information. For more information from this query, see: https://msdn.microsoft.com/en-us/library/aa394239(v=vs.85).aspx
    Set Results = GetObject("Winmgmts:").ExecQuery("SELECT Caption,Version,ServicePackMajorVersion,ServicePackMinorVersion,OSArchitecture,TotalVisibleMemorySize FROM Win32_OperatingSystem")
    For Each Info In Results 'Info.Version can be used to calculate Windows version. E.G. If Val(Left$(Info.Version,3)>=6.1 then it is at least Windows 7.
        PCInfo = Info.Caption & " - " & Info.Version & "  SP " & _
            Info.ServicePackMajorVersion & "." & _
            Info.ServicePackMinorVersion & "  " & Info.OSArchitecture & "  " & vbNewLine
        Ram = "Installed RAM: " & Format$(TotMem, "0.00 GB (") & Format$(Info.TotalVisibleMemorySize / 1048576, "0.00 GB usable)") 'Divide by 1MB to get GB
    Next Info

    ' Get the C.P.U. information. For more information from this query, see: https://msdn.microsoft.com/en-us/library/aa394373(v=vs.85).aspx
    Set Results = GetObject("Winmgmts:").ExecQuery("SELECT Name,AddressWidth,NumberOfLogicalProcessors,CurrentClockSpeed FROM Win32_Processor")
    For Each Info In Results
        PCInfo = PCInfo & Info.Name & "  " & Info.AddressWidth & _
            "-bit." & vbNewLine & Info.NumberOfLogicalProcessors & _
            " Cores " & Info.CurrentClockSpeed & "MHz.  " & Ram
    Next Info

    Set Results = Nothing
    MsgBox PCInfo
End Sub

Attached Images
 

Viewing all articles
Browse latest Browse all 1448

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>