|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Visual Basic System Services GetVersionEx: Windows Version, Service Pack and Platform Info |
||
Posted: | Monday May 24, 1999 | |
Updated: | Monday December 26, 2011 | |
Applies to: | VB4-32, VB5, VB6 | |
Developed with: | VB6, Windows NT4 | |
OS restrictions: | None | |
Author: | VBnet - Randy Birch | |
Related: |
GetVersionEx: Windows Version Info (Wrapper Routines) GetFileVersionInfo: Handy Routines for Identifying Shell32 Versions GetSystemInfo: System Processor Information NetServerGetInfo: Configuration Info for Domain/Workgroup Servers and Machines |
|
Prerequisites | ||||||||
None. | ||||||||
|
||||||||
This
page provides several wrapper routines for obtaining general Windows operation system information, all using
a single call to the GetVersionEx API.
One of the two main routines populates a developer-defined type with the OS information allowing creation of the information shown in the illustration's first six text boxes. The other routine returns just the version number in a string (as shown in the "Together" text box). A small set of wrapper routines for basic OS identification are also presented; the page at GetVersionEx: Windows Version Info (Wrapper Routines) contains a much more exhaustive set of IsWinXXX functions that including additional tests returning information such as whether the OS is Terminal Server, Small Business Server, a domain controller, BackOffice server, etc. MS recommends that the use of GetVersionEx be used in appropriate situations. The API is perfectly safe to call as often as needed, but as MS points out identifying the current operating system is usually not the best way to determine whether a particular operating system *feature* is present. Their recommendation is to test for the presence of the feature itself.
|
||||||||
BAS Module Code | ||||||||
None. | ||||||||
|
||||||||
Form Code | ||||||||
On a form, add a command button, a textbox as Text1(0) creating a control array, and a single label (Label1(0) as a control array). Add the following code to the form: | ||||||||
|
||||||||
Option Explicit '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved. ' Some pages may also contain other copyrights by the author. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Distribution: You can freely use this code in your own ' applications, but you may not reproduce ' or publish this code on any web site, ' online service, or distribute as source ' on any media without express permission. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Private Const VER_PLATFORM_WIN32s = 0 Private Const VER_PLATFORM_WIN32_WINDOWS = 1 Private Const VER_PLATFORM_WIN32_NT = 2 'windows-defined type OSVERSIONINFO Private Type OSVERSIONINFO OSVSize As Long 'size, in bytes, of this data structure dwVerMajor As Long 'ie NT 3.51, dwVerMajor = 3; NT 4.0, dwVerMajor = 4. dwVerMinor As Long 'ie NT 3.51, dwVerMinor = 51; NT 4.0, dwVerMinor= 0. dwBuildNumber As Long 'NT: build number of the OS 'Win9x: build number of the OS in low-order word. ' High-order word contains major & minor ver nos. PlatformID As Long 'Identifies the operating system platform. szCSDVersion As String * 128 'NT: string, such as "Service Pack 3" 'Win9x: 'arbitrary additional information' End Type 'my type for holding the retrieved info Private Type RGB_WINVER PlatformID As Long VersionName As String VersionNo As String ServicePack As String BuildNo As String End Type Private Declare Function GetVersionEx Lib "kernel32" _ Alias "GetVersionExA" _ (lpVersionInformation As OSVERSIONINFO) As Long Private Sub Form_Load() Dim cnt As Long Dim TotalRequired As Long Me.Move (Screen.Width - Me.Width) \ 2, (Screen.Height - Me.Height) \ 2 'the array is 0-based, so the first control is 'index 0, and the last is TotalRequired -1 TotalRequired = 13 For cnt = 0 To TotalRequired - 1 'since Text1(0) already exists, we don't load it If cnt > 0 Then Load Label1(cnt) Load Text1(cnt) End If 'position the newly-created control Label1(cnt).Move 120, 230 + (cnt * 300) Text1(cnt).Move 1600, 200 + (cnt * 300), 3900, 285 'and show Label1(cnt).Visible = True Text1(cnt).Visible = True Next With Label1 .Item(0).Caption = "Windows version" .Item(1).Caption = "Version number" .Item(2).Caption = "Build number" .Item(3).Caption = "Service Pack/misc" .Item(4).Caption = "Platform identifier" .Item(5).Caption = "Together" .Item(6).Caption = "IsWin95 returns" .Item(7).Caption = "IsWin98 returns" .Item(8).Caption = "IsWinME returns" .Item(9).Caption = "IsWinNT4 returns" .Item(10).Caption = "IsWin2000 returns" .Item(11).Caption = "IsWinXP returns" .Item(12).Caption = "GetWinVer returns" End With End Sub Private Sub Command1_Click() Dim win As RGB_WINVER Call GetWinVersion(win) Text1(0).Text = win.VersionName Text1(1).Text = win.VersionNo Text1(2).Text = win.BuildNo Text1(3).Text = win.ServicePack Text1(4).Text = win.PlatformID 'as a version string Text1(5).Text = win.VersionName & " " & _ win.VersionNo & " build " & _ win.BuildNo & " (" & _ win.ServicePack & ")" Text1(6).Text = IsWin95() Text1(7).Text = IsWin98() Text1(8).Text = IsWinME() Text1(9).Text = IsWinNT4() Text1(10).Text = IsWin2000() Text1(11).Text = IsWinXP() Text1(12).Text = GetWinVer() End Sub Private Function GetWinVersion(win As RGB_WINVER) As String 'returns a structure (RGB_WINVER) 'filled with OS information #If Win32 Then Dim osv As OSVERSIONINFO Dim pos As Integer Dim sVer As String Dim sBuild As String osv.OSVSize = Len(osv) If GetVersionEx(osv) = 1 Then 'PlatformId contains a value representing the OS win.PlatformID = osv.PlatformID Select Case osv.PlatformID Case VER_PLATFORM_WIN32s: win.VersionName = "Win32s" Case VER_PLATFORM_WIN32_NT: win.VersionName = "Windows NT" Select Case osv.dwVerMajor Case 4: win.VersionName = "Windows NT" Case 5: Select Case osv.dwVerMinor Case 0: win.VersionName = "Windows 2000" Case 1: win.VersionName = "Windows XP" End Select End Select Case VER_PLATFORM_WIN32_WINDOWS: 'The dwVerMinor bit tells if its 95 or 98. Select Case osv.dwVerMinor Case 0: win.VersionName = "Windows 95" Case 90: win.VersionName = "Windows ME" Case Else: win.VersionName = "Windows 98" End Select End Select 'Get the version number win.VersionNo = osv.dwVerMajor & "." & osv.dwVerMinor 'Get the build win.BuildNo = (osv.dwBuildNumber And &HFFFF&) 'Any additional info. In Win9x, this can be '"any arbitrary string" provided by the 'manufacturer. In NT, this is the service pack. pos = InStr(osv.szCSDVersion, Chr$(0)) If pos Then win.ServicePack = Left$(osv.szCSDVersion, pos - 1) End If End If #Else 'can only return that this does not 'support the 32 bit call, so must be Win3x win.VersionName = "Windows 3.x" #End If End Function Private Function IsWin95() As Boolean 'returns True if running Win95 #If Win32 Then Dim osv As OSVERSIONINFO osv.OSVSize = Len(osv) If GetVersionEx(osv) = 1 Then IsWin95 = (osv.PlatformID = VER_PLATFORM_WIN32_WINDOWS) And _ (osv.dwVerMajor = 4 And osv.dwVerMinor = 0) End If #End If End Function Private Function IsWin98() As Boolean 'returns True if running Win98 Dim osv As OSVERSIONINFO osv.OSVSize = Len(osv) If GetVersionEx(osv) = 1 Then IsWin98 = (osv.PlatformID = VER_PLATFORM_WIN32_WINDOWS) And _ (osv.dwVerMajor = 4 And osv.dwVerMinor = 10) And _ (osv.dwBuildNumber >= 2222) End If End Function Private Function IsWinME() As Boolean 'returns True if running Windows ME #If Win32 Then Dim osv As OSVERSIONINFO osv.OSVSize = Len(osv) If GetVersionEx(osv) = 1 Then IsWinME = (osv.PlatformID = VER_PLATFORM_WIN32_WINDOWS) And _ (osv.dwVerMajor = 4 And osv.dwVerMinor = 90) End If #End If End Function Private Function IsWinNT4() As Boolean 'returns True if running WinNT4 #If Win32 Then Dim osv As OSVERSIONINFO osv.OSVSize = Len(osv) If GetVersionEx(osv) = 1 Then 'PlatformId contains a value representing the OS. 'If VER_PLATFORM_WIN32_NT and dwVerMajor is 4, return true IsWinNT4 = (osv.PlatformID = VER_PLATFORM_WIN32_NT) And _ (osv.dwVerMajor = 4) End If #End If End Function Private Function IsWin2000() As Boolean 'returns True if running Windows 2000 (NT5) #If Win32 Then Dim osv As OSVERSIONINFO osv.OSVSize = Len(osv) If GetVersionEx(osv) = 1 Then IsWin2000 = (osv.PlatformID = VER_PLATFORM_WIN32_NT) And _ (osv.dwVerMajor = 5 And osv.dwVerMinor = 0) End If #End If End Function Private Function IsWinXP() As Boolean 'returns True if running WinXP (NT5.1) #If Win32 Then Dim osv As OSVERSIONINFO osv.OSVSize = Len(osv) If GetVersionEx(osv) = 1 Then IsWinXP = (osv.PlatformID = VER_PLATFORM_WIN32_NT) And _ (osv.dwVerMajor = 5 And osv.dwVerMinor = 1) End If #End If End Function Private Function GetWinVer() As String 'returns a string representing the version, 'ie "95", "98", "NT4", "WinXP" Dim osv As OSVERSIONINFO Dim r As Long Dim pos As Integer Dim sVer As String Dim sBuild As String osv.OSVSize = Len(osv) If GetVersionEx(osv) = 1 Then 'PlatformId contains a value representing the OS Select Case osv.PlatformID Case VER_PLATFORM_WIN32s: GetWinVer = "32s" Case VER_PLATFORM_WIN32_NT: 'dwVerMajor = NT version. 'dwVerMinor = minor version Select Case osv.dwVerMajor Case 3: Select Case osv.dwVerMinor Case 0: GetWinVer = "NT3" Case 1: GetWinVer = "NT3.1" Case 5: GetWinVer = "NT3.5" Case 51: GetWinVer = "NT3.51" End Select Case 4: GetWinVer = "NT 4" Case 5: Select Case osv.dwVerMinor Case 0: GetWinVer = "Win2000" Case 1: GetWinVer = "WinXP" End Select End Select Case VER_PLATFORM_WIN32_WINDOWS: 'dwVerMinor bit tells if its 95 or 98. Select Case osv.dwVerMinor Case 0: GetWinVer = "95" Case 90: GetWinVer = "ME" Case Else: GetWinVer = "98" End Select End Select End If End Function |
||||||||
Comments | ||||||||
Running the project will populate the fields with the info returned by the operating system. IsWinNT4 / IsWin2000 / IsWin95 / IsWin98 / IsWinME will return True or False as expected. GetVersionEx: Windows Version Info (Wrapper Routines) has additional wrappers providing even more specific information (where supported) such as major and minor version numbers, a build number, a platform identifier (server, workstation etc), and information about product suites (business, enterprise, terminal etc) and the latest Service Pack installed on the system. | ||||||||
|
|
|
|||||
|
|||||
|
|||||
Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved. |