|
There are many times while writing programs that I want to
execute different code depending on what version of Windows the user is
running. I find myself running into this problem a lot when I am using
certain API calls that only work on specific versions of Windows. That is
why I decided to write this quick howto. So lets begin with the API you
will need to use, GetVersionEx. This API will fill a OSVERSIONINFO
structure, which you can then examine to determine the exact version of
Windows running. This will not work for Windows 3.1 but all my programs
are 32 bit so I really don't care :-). The OSVERSIONINFO structure will
contain the following data depending on what version of Windows you are
running:
| |
Win95 |
Win98 |
WinNT 3.51 |
WinNT 4.0 |
Win 2000 |
| dwPlatformID |
1 |
1 |
2 |
2 |
2 |
| dwMajorVersion |
4 |
4 |
3 |
4 |
5 |
| dwMinorVersion |
0 |
10 |
51 |
0 |
0 |
I am going to write a public function called GetOSVersion.
This function will return an integer representing the windows version it
has. So to do this lets first declare a long integer to store our return
value. Next, lets declare a variable called osVersion as OSVERSIONINFO.
This variable will be passed into our GetVersionEx function, and will then
be filled according to the table above. However, the GetVersionEx function
requires the OSVERSIONINFO variable to be declared and have memory
reserved. We can do this by setting the dwOSVersionInfoSize equal to 148
and filling the szCSDVersion with 128 spaces. Then we can pass the
osVersion variable into our function. If GetVersionEx is greater than 0
then we know it worked correctly and we can trust that the osVersion
variable was filled. By looking at the
dwPlatformID, dwMajorVersion, and dwMinorVersion, of the osVersion variable
we can determine what
version of Windows is running. Below is the code for my modGetOSVers
module. If you want to see a complete working example of it download the
source.
Option Explicit
Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" _
(lpVersionInformation As OSVERSIONINFO) As Long
Private Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128 ' Maintenance string for PSS usage
End Type
Public Const OSVerInvalid As Integer = 0
Public Const OSVerWin95 As Integer = 1
Public Const OSVerWin98 As Integer = 2
Public Const OSVerWinNT35 As Integer = 3
Public Const OSVerWinNT4 As Integer = 4
Public Const OSVerWin2000 As Integer = 5
Public Function GetOSVersion() As Integer
Dim osVersion As OSVERSIONINFO
Dim iRet As Integer
' Make sure the memory is set aside
osVersion.dwOSVersionInfoSize = 148
osVersion.szCSDVersion = Space$(128)
iRet = GetVersionEx(osVersion)
If iRet > 0 Then
With osVersion
Select Case .dwPlatformId
Case 1 ' Win95 or Win98
If .dwMinorVersion = 0 Then
GetOSVersion = OSVerWin95
ElseIf .dwMinorVersion = 10 Then
GetOSVersion = OSVerWin98
Else
GetOSVersion = OSVerInvalid
End If
Case 2 ' WinNT or 2000
If .dwMajorVersion = 3 Then
GetOSVersion = OSVerWinNT35
ElseIf .dwMajorVersion = 4 Then
GetOSVersion = OSVerWinNT4
ElseIf .dwMajorVersion = 5 Then
GetOSVersion = OSVerWin2000
Else
GetOSVersion = OSVerInvalid
End If
Case Else
GetOSVersion = OSVerInvalid
End Select
End With
Else
GetOSVersion = OSVerInvalid
End If
End Function
|
Download The Sample Code
Return to the HOWTOs
|