Windows Version Information

A common question is how to retreive the windows version using Windows API. This example shows you how to get almost every bit of information you could ever need: Windows version, product type (server/workstation), service pack and build number! Simply add the following code to a form or module:

Option Explicit

Private Const VER_PLATFORM_WIN32s = 0
Private Const VER_PLATFORM_WIN32_WINDOWS = 1
Private Const VER_PLATFORM_WIN32_NT = 2

Private Const VER_NT_WORKSTATION = 1
Private Const VER_NT_DOMAIN_CONTROLLER = 2
Private Const VER_NT_SERVER = 3

Private Type OSVERSIONINFOEX
    dwOSVersionInfoSize As Long
    dwMajorVersion As Long
    dwMinorVersion As Long
    dwBuildNumber As Long
    dwPlatformId As Long
    szCSDVersion As String * 128      '  Maintenance string for PSS usage
    wServicePackMajor As Integer 'win2000 only
    wServicePackMinor As Integer 'win2000 only
    wSuiteMask As Integer 'win2000 only
    wProductType As Byte 'win2000 only
    wReserved As Byte
End Type

Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (ByRef lpVersionInformation As OSVERSIONINFOEX) As Long

Private Function GetVersionInfo() As String
    Dim myOS As OSVERSIONINFOEX
    Dim bExInfo As Boolean
    Dim sOS As String

    myOS.dwOSVersionInfoSize = Len(myOS) 'should be 148/156
    'try win2000 version
    If GetVersionEx(myOS) = 0 Then
        'if fails
        myOS.dwOSVersionInfoSize = 148 'ignore reserved data
        If GetVersionEx(myOS) = 0 Then
            GetVersionInfo = "Microsoft Windows (Unknown)"
            Exit Function
        End If
    Else
        bExInfo = True
    End If
   
    With myOS
        'is version 4
        If .dwPlatformId = VER_PLATFORM_WIN32_NT Then
            'nt platform
            Select Case .dwMajorVersion
            Case 3, 4
                sOS = "Microsoft Windows NT"
            Case 5
                sOS = "Microsoft Windows 2000"
            End Select
            If bExInfo Then
                'workstation/server?
                If .wProductType = VER_NT_SERVER Then
                    sOS = sOS & " Server"
                ElseIf .wProductType = VER_NT_DOMAIN_CONTROLLER Then
                    sOS = sOS & " Domain Controller"
                ElseIf .wProductType = VER_NT_WORKSTATION Then
                    sOS = sOS & " Workstation"
                End If
            End If
           
            'get version/build no
            sOS = sOS & " Version " & .dwMajorVersion & "." & .dwMinorVersion & " " & StripTerminator(.szCSDVersion) & " (Build " & .dwBuildNumber & ")"
           
        ElseIf .dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then
            'get minor version info
            If .dwMinorVersion = 0 Then
                sOS = "Microsoft Windows 95"
            ElseIf .dwMinorVersion = 10 Then
                sOS = "Microsoft Windows 98"
            ElseIf .dwMinorVersion = 90 Then
                sOS = "Microsoft Windows Millenium"
            Else
                sOS = "Microsoft Windows 9?"
            End If
            'get version/build no
            sOS = sOS & "Version " & .dwMajorVersion & "." & .dwMinorVersion & " " & StripTerminator(.szCSDVersion) & " (Build " & .dwBuildNumber & ")"
        End If
    End With
    GetVersionInfo = sOS
End Function
Private Function StripTerminator(sString As String) As String
    StripTerminator = Left$(sString, InStr(sString, Chr$(0)) - 1)
End Function

To get the version information, do

MsgBox GetVersionInfo

or whatever else your code requires.

Msgbox

You might also like...

Comments

James Crowley James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audience ...

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“My definition of an expert in any field is a person who knows enough about what's really going on to be scared.” - P. J. Plauger