Attribute VB_Name = "WinVersion" ' WinVers.bas for Visual Basic 6.0 ' Module for retrieving the currently running Windows version ' ©2005-2012 Aivosto Oy www.aivosto.com ' ' Originally distributed with Aivosto Visustin flow chart generator. ' Permission is granted to use, modify and compile this code, but not to publish or sell it. Option Explicit ' Windows versions enumerated Public Enum EWinVersion wvError = 0 ' Error/Unknown Windows version wvWin95 ' 4.0 Windows 95 wvWin98 ' 4.10 Windows 98 wvWinME ' 4.90 Windows ME wvWinNT351 ' 3.51 Windows NT 3.51 - this should never occur as VB6 doesn't run on this OS wvWinNT4 ' 4.0 Windows NT 4.0 wvWin2000 ' 5.0 Windows 2000 wvWinXP ' 5.1 Windows XP wvWinServer2003 ' 5.2 Windows Server 2003 (& R2) - also Windows Home Server, WinXP Pro x64 Edition wvWinVista ' 6.0 Windows Vista wvWinServer2008 ' 6.0 Windows Server 2008 wvWin7 ' 6.1 Windows 7 wvWinServer2008R2 ' 6.1 Windows Server 2008 R2 wvWin8 ' 6.2 Windows 8 wvWinServer2012 ' 6.2 Windows Server 2012 wvNewer ' A newer Windows version End Enum ' API structures Private Type OSVERSIONINFO dwOSVersionInfoSize As Long dwMajorVersion As Long dwMinorVersion As Long dwBuildNumber As Long dwPlatformId As Long szCSDVersion As String * 128 End Type Private Type OSVERSIONINFOEX dwOSVersionInfoSize As Long dwMajorVersion As Long dwMinorVersion As Long dwBuildNumber As Long dwPlatformId As Long szCSDVersion As String * 128 wServicePackMajor As Integer wServicePackMinor As Integer wSuiteMask As Integer wProductType As Byte wReserved As Byte End Type ' API Declares ' Two declarations: same function but different parameter Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (lpVersionInformation As OSVERSIONINFO) As Long Private Declare Function GetVersionEx2 Lib "kernel32" Alias "GetVersionExA" (lpVersionInformation As OSVERSIONINFOEX) As Long ' API constants Private Const VER_PLATFORM_WIN32_WINDOWS = 1 ' Windows 95/98/ME Private Const VER_PLATFORM_WIN32_NT = 2 ' Windows NT Private Const VER_NT_WORKSTATION = 1 ' Workstation Private Const VER_NT_SERVER = 3 ' Windows Server 2000, 2003, 2008, 2012 Private Const VER_NT_DOMAIN_CONTROLLER = 2 ' Windows Server 2000, 2003, 2008, 2012 (domain controller) Public Function GetWinVersionEx() As EWinVersion ' Retrieve the current Windows version as an enumerated constant value ' In the case of an error, return wvError Dim OSVI As OSVERSIONINFO Dim OSVIEX As OSVERSIONINFOEX Dim IsServer As Boolean On Error Resume Next ' Assume error/unknown until Windows version detected GetWinVersionEx = wvError ' Retrieve version information into OSVI OSVI.dwOSVersionInfoSize = Len(OSVI) If GetVersionEx(OSVI) <> 0 Then ' Version retrieved If OSVI.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then ' Win95/98/ME detected If OSVI.dwMajorVersion = 4 Then ' Windows 4.x detected Select Case OSVI.dwMinorVersion Case 0 ' 4.0 Windows 95 GetWinVersionEx = wvWin95 Case 10 ' 4.10 Windows 98 GetWinVersionEx = wvWin98 Case 90 ' 4.90 Windows ME GetWinVersionEx = wvWinME End Select End If ElseIf OSVI.dwPlatformId = VER_PLATFORM_WIN32_NT Then ' WinNT/2000/XP/2003/2008/7/8/Server detected Select Case OSVI.dwMajorVersion Case 3 ' Windows NT 3.51 GetWinVersionEx = wvWinNT351 Case 4 ' Windows NT 4.0 GetWinVersionEx = wvWinNT4 Case 5 ' Windows NT 5.x Select Case OSVI.dwMinorVersion Case 0 ' 5.0 Windows 2000 GetWinVersionEx = wvWin2000 Case 1 ' 5.1 Windows XP GetWinVersionEx = wvWinXP Case 2 ' 5.2 Windows Server 2003 (or R2) ' (also Windows Home Server or WinXP Pro x64 Edition) GetWinVersionEx = wvWinServer2003 Case Else ' Unknown Windows version GetWinVersionEx = wvNewer End Select Case 6 ' Windows 6.x ' Determine Server vs. Workstation OSVIEX.dwOSVersionInfoSize = Len(OSVIEX) If GetVersionEx2(OSVIEX) <> 0 Then Select Case OSVIEX.wProductType Case VER_NT_WORKSTATION ' Workstation IsServer = False Case Else ' Server or Domain controller IsServer = True End Select Else IsServer = False ' Call failed, assume Workstation End If If IsServer Then ' Server or Domain controller Select Case OSVI.dwMinorVersion Case 0 ' 6.0 Windows Server 2008 GetWinVersionEx = wvWinServer2008 Case 1 ' 6.1 Windows Server 2008 R2 GetWinVersionEx = wvWinServer2008R2 Case 2 ' 6.2 Windows Server 2012 GetWinVersionEx = wvWinServer2012 Case Else ' Unknown, newer Windows version GetWinVersionEx = wvNewer End Select Else ' Workstation Select Case OSVI.dwMinorVersion Case 0 ' 6.0 Windows Vista GetWinVersionEx = wvWinVista Case 1 ' 6.1 Windows 7 GetWinVersionEx = wvWin7 Case 2 ' 6.2 Windows 8 GetWinVersionEx = wvWin8 Case Else ' Unknown, newer Windows version GetWinVersionEx = wvNewer End Select End If Case Is > 6 ' Unknown, newer Windows version GetWinVersionEx = wvNewer End Select End If End If End Function