Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleUse WMI to get the operating system name including its edition, plus some other information in Visual Basic 6
DescriptionThis example shows how to use WMI to get the operating system name including its edition, plus some other information in Visual Basic 6.
KeywordsWMI, operating system, version, 32-bit, 64-bit, Visual Basic 6, VB 6
Categories, Windows
 
WMI (Windows Management Instrumentation) lets you use SQL-like statements to ask the computer about itself. This example uses it to get:

  • The operating system name including its edition (Home, Ultimate, etc.), version, and Service Pack number
  • The number of logical processors
  • The number of bits the system uses (32 or 64)

When the program loads, the following code displays these values in labels.

 
' Display the operating system's name.
Private Sub Form_Load()
    ' Get the OS information.
    ' For more information from this query, see:
    '      http:'msdn.microsoft.com/library/aa394239.aspx
    Dim os_query As String
    Dim os_results As Object
    Dim info As Object
    Dim cpus_query As String
    Dim cpus_results As Object
    Dim proc_query As String
    Dim proc_results As Object

    os_query = "SELECT * FROM Win32_OperatingSystem"
    Set os_results = _
        GetObject("Winmgmts:").ExecQuery(os_query)
    For Each info In os_results
        lblCaption.Caption = info.Caption
        lblVersion.Caption = "Version " & _
            info.Version & _
            " SP " & _
            info.ServicePackMajorVersion & "." & _
            info.ServicePackMinorVersion
    Next info

    ' Get number of processors.
    ' For more information from this query, see:
    '      http:'msdn.microsoft.com/library/aa394373.aspx
    cpus_query = "SELECT * FROM Win32_ComputerSystem"
    Set cpus_results = _
        GetObject("Winmgmts:").ExecQuery(cpus_query)
    For Each info In cpus_results
        lblCpus.Caption = info.NumberOfLogicalProcessors & _
            " processors"
    Next info

    ' Get 32- versus 64-bit.
    ' For more information from this query, see:
    '      http:'msdn.microsoft.com/library/aa394373.aspx
    proc_query = "SELECT * FROM Win32_Processor"
    Set proc_results = _
        GetObject("Winmgmts:").ExecQuery(proc_query)
    For Each info In proc_results
        lblBits.Caption = info.AddressWidth & "-bit"
    Next info
End Sub
 
The code uses the WMI query "SELECT * FROM Win32_OperatingSystem" to get information about the operating system. The result is a collection holding one Win32_OperatingSystem object. The code uses a loop to get that object and reads its properties. The Caption property gives the full operating system name including trademark symbols and the edition. Version returns the operating system's version. The ServicePackMajorVersion and ServicePackMinorVersion properties give the service pack information.

Next the program executes the query "SELECT * FROM Win32_ComputerSystem" in a similar fashion to get the number of logical processors.

It finishes by executing the query "SELECT * FROM Win32_Processor" to determine whether this is a 32-bit or 64-bit operating system.

 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated