VB Notebook Main Page VB Notebook Files VB Notebook Function Library VB Notebook Articles VB Notebook Links VB Notebook

VBNotebook Home | Functions | Get Windows Version

This routine returns the 32-bit version of Windows that's in use.

UPDATE 5/15/07: Thanks to Vero44 on VBForums who pointed out a bug in this routine. I also added Vista support although I haven't tested it to see if it works or not.

First, in General|Declarations, include the following...
__________________________________________________________________________________________

'
' Types for API Calls
'

Private Type
OSVERSIONINFO
    dwOSVersionInfoSize As Long
    dwMajorVersion As Long
    dwMinorVersion As Long
    dwBuildNumber As Long
    dwPlatformId As Long
    szCSDVersion As String * 128
End Type
'
' Constants
'

Public Enum
WindowsVersionConstants
    wvc_Unknown
    wvc_Win95
    wvc_Win98
    wvc_WinMe
    wvc_NT351
    wvc_NT4
    wvc_2000
    wvc_XP
    wvc_2003
    wvc_Vista

End Enum
'
' API Calls
'

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

__________________________________________________________________________________________

...and now the routine... __________________________________________________________________________________________

Public Function GetOSVersion() As WindowsVersionConstants
'
'                Win95 Win98 WinME NT 3.51 NT 4.0 Win2000 WinXP Win2003 Vista
' ------------------------------------------------------------------------------
'dwPlatFormID      1     1     1      2      2      2       2     2     2
'dwMajorVersion    4     4     4      3      4      5       5     5     6
'dwMinorVersion    0    10    90     51      0      0       1     2     _
'
    Const VER_PLATFORM_WIN32s = 0
    Const VER_PLATFORM_WIN32_WINDOWS = 1
    Const VER_PLATFORM_WIN32_NT = 2

    Dim tOS As OSVERSIONINFO
    Dim retval As Long
    Dim nResult As WindowsVersionConstants

    tOS.dwOSVersionInfoSize = Len(tOS)
    retval = GetVersionEx(tOS)

    With tOS
        If .dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then
            Select Case .dwMajorVersion
                Case 4
                    Select Case .dwMinorVersion
                        Case 0
                            nResult = wvc_Win95
                        Case 10
                            nResult = wvc_Win98
                        Case 90
                            nResult = wvc_WinMe
                        Case Else
                            nResult = wvc_Unknown
                    End Select
                Case Else
                    nResult = wvc_Unknown
            End Select
        ElseIf .dwPlatformId = VER_PLATFORM_WIN32_NT Then
            Select Case .dwMajorVersion
                Case 3
                    nResult = wvc_NT351
                Case 4
                    nResult = wvc_NT4
                Case 5
                    Select Case .dwMinorVersion
                        Case 0
                            nResult = wvc_2000
                        Case 1
                            nResult = wvc_XP
                        Case 2
                            nResult = wvc_2003
                        Case Else
                            nResult = wvc_Unknown
                    End Select
                Case 6
                    nResult = wvc_Vista
                Case Else
                    nResult = wvc_Unknown
            End Select
        Else
            nResult = wvc_Unknown
        End If
    End With

    GetOSVersion = nResult
End Function

__________________________________________________________________________________________

Copyright 2000-2005, J. Frank Carr