Vb6 question - GetVersionExA

SIMIN

Well-known member
Joined
Mar 10, 2008
Messages
92
Hi,
I want to detect the version using GetVersionExA.
With the following module I can do this, but there is a problem in detecting Windows Vista against Windows Server 2008 because they have the same minor and major versions.
According to Microsoft I have to use use OSVERSIONINFOEX Structure. It contains operating system version information. The information includes major and minor version numbers, a build number, a platform identifier, and information about product suites and the latest Service Pack installed on the system. This structure is used with the GetVersionEx and VerifyVersionInfo functions.

The version numbers for Windows Server 2008 and Windows Vista are identical, both version numbers are 6, you must also test whether the wProductType member of OSVERSIONINFOEX Structure is VER_NT_WORKSTATION. If wProductType is VER_NT_WORKSTATION, the operating system is Windows Vista; otherwise, it is Windows Server 2008.

The problem is that .wProductType always returns 0 rather than VER_NT_WORKSTATION...

Here is my module, which I dont know where its wrong:

Code:
Public Declare Function GetVersionExA Lib "kernel32" (lpVersionInformation As OSVERSIONINFOEX) As Integer
Public 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
Public Function GetVersion() As String
   On Error Resume Next
   Dim osinfo As OSVERSIONINFOEX
   Dim retvalue As Integer
   osinfo.dwOSVersionInfoSize = 148
   osinfo.szCSDVersion = Space$(128)
   retvalue = GetVersionExA(osinfo)
   With osinfo
   MsgBox .wProductType
    Select Case .dwPlatformId
     Case 1
      Select Case .dwMinorVersion
       Case 0
        GetVersion = "Windows 95"
       Case 10
        GetVersion = "Windows 98"
       Case 90
        GetVersion = "Windows Millennium"
      End Select
     Case 2
      Select Case .dwMajorVersion
       Case 3
        GetVersion = "Windows NT 3.51"
       Case 4
        GetVersion = "Windows NT 4.0"
       Case 5
        Select Case .dwMinorVersion
         Case 0
          GetVersion = "Windows 2000"
         Case 1
          GetVersion = "Windows XP"
         Case 2
          GetVersion = "Windows Server 2003"
        End Select
       Case 6
        If .wProductType = "VER_NT_WORKSTATION" Then
         GetVersion = "Windows Vista"
        Else
         GetVersion = "Windows Server 2008"
        End If
      End Select
     Case Else
      GetVersion = "Unknown Windows"
    End Select
   End With
End Function
 
Problems with your code

A few things.

1. VER_NT_WORKSTATION is the name of a constant value. You need to declare it as such and not perform a string comparison:

Code:
Const VER_NT_WORKSTATION = &H01
Const VER_NT_DOMAIN_CONTROLLER = &H02
Const VER_NT_SERVER = &H03

If .wProductType = VER_NT_WORKSTATION Then
    Vista, XP, or 2000 Professional
End If

2. You are setting dwOSVersionInfoSize to a value smaller than the size of the OSVERSIONINFOEX structure:

Code:
osinfo.dwOSVersionInfoSize = Len(OSVERSIONINFOEX)

3. My VB6 is a little rusty, but will the szCSDVersion member use single or double byte characters? If as I suspect it is a double byte character field, you should be using GetVersionExW. If you do not actually use this field and want to be certain it is the correct size, declare it as an array of bytes if using GetVersionExA:

Code:
szCSDVersion(127) As Byte

Good luck :cool:
 
Hello,

I also faced same problem..but found a solution. While declaring osversionInfoSize,

use

osinfo.dwOSVersionInfoSize = 156

instead of

osinfo.dwOSVersionInfoSize = 148
 
Back
Top