Can I determine if I am on Win7 OS from VB6?

I have an old program written in VB6 that should run on three different platforms, including my laptop with Win7. I googled how to determine the OS from VB6 and found some code that I slightly modified as follows:

Declare Function GetVersionExA Lib "kernel32" (lpVersionInformation As OSVERSIONINFO) As Integer

Public Type OSVERSIONINFO
    dwOSVersionInfoSize As Long
    dwMajorVersion As Long
    dwMinorVersion As Long
    dwBuildNumber As Long
    dwPlatformId As Long
    szCSDVersion As String * 128
End Type


Private Const VER_PLATFORM_WIN32s = 0
Private Const VER_PLATFORM_WIN32_WINDOWS = 1
Private Const VER_PLATFORM_WIN32_NT As Long = 2


Private Function GetOS() As String
    Dim osinfo As OSVERSIONINFO
    Dim retvalue As Integer
    Dim sOS as String

    osinfo.dwOSVersionInfoSize = 148
    osinfo.szCSDVersion = Space$(128)
    retvalue = GetVersionExA(osinfo)

    Select Case osinfo.dwMajorVersion
        Case 7
            sOS = "?"  'Win7?
        Case 6
            sOS = "Vista"
         Case 5
            sOS = "XP"
         Case 4
            sOS = "Win2000"
     End Select

     MsgBox (sOS)
     return sOS     

End Function

When I run this from my WIN7 laptop, osinfo.dwMajorVersion = 5, which assumes it is on an XP machine.

What's going on here? Can I tell if I am starting Win7 with this method? What is the best way to get the information you need?

+3
source share
7 answers

Windows 7 6.1, 7. . , , , , . return VB 6. GetOS GetOS = sOS. , .

, . Windows . , , Windows . , , , .

, Windows 7:

     

, , . , . , , " Windows XP" - .

+8

Windows 7 6.1.7600, majorversion 6, minorversion 1, build 7600 . , MajorVersion 5, , . .exe, "".

+4

API, Windows XP . "", , .

Private Function GetMyWindowsVersion() As String    
Dim r As Long, bFile As Integer, verString As String, fResult As String, bracketStart As Integer, verInfo As String, bracketEnd As Integer, versionLength As Integer

fResult = "Windows OS"

bFile = FreeFile
Open App.Path & "\checkos.bat" For Output As #bFile    
    Print #bFile, "@echo off"    
    Print #bFile, "ver > version.txt"    
    Print #bFile, "exit"    
Close #bFile

r = Shell(App.Path & "\checkos.bat", vbMinimizedNoFocus)

bFile = FreeFile    
Open App.Path & "\version.txt" For Input As #bFile    
    Do Until EOF(bFile)    
        Line Input #bFile, verString    
        If Trim(verString) <> "" Then    
            bracketStart = InStr(verString, "[")    
            bracketEnd = InStr(verString, "]")    
            If bracketStart And bracketEnd > 0 Then    
                versionLength = bracketEnd - bracketStart    
                verInfo = Mid(verString, bracketStart + 1, versionLength - 1)    
                If InStr(verString, "6.2") Then    
                    fResult = "Windows 8 " & verInfo    
                End If    
                If InStr(verString, "6.1") Then    
                    fResult = "Windows 7 " & verInfo    
                End If    
                If InStr(verString, "5.") Then    
                    fResult = "Windows XP " & verInfo    
                End If    
                Exit Do    
                Else    
                fResult = verString    
                Exit Do    
            End If    
        End If    
    Loop    
Close #bFile    
GetMyWindowsVersion = fResult    
End Function
+3

. Vista Windows 2008 , Windows 7.

0

; dwMajorVersion 5 - win2k XP, 6 - 2k8 R2 Win 7 - dwMinorVersion, . ( )

0

, ....

Option Explicit



Private Const VER_PLATFORM_WIN32s = 0
Private Const VER_PLATFORM_WIN32_WINDOWS = 1
Private Const VER_PLATFORM_WIN32_NT = 2

Private Const VER_NT_WORKSTATION = 1
Private Const VER_NT_DOMAIN_CONTROLLER = 2
Private Const VER_NT_SERVER = 3

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

Private Declare Function GetVersionExA Lib "kernel32" (ByRef lpVersionInformation As OSVERSIONINFOEX) As Long




Public Function GetWindowsVersion() As String
    Dim osinfo As OSVERSIONINFOEX
    Dim retvalue As Integer

    osinfo.dwOSVersionInfoSize = 148
    osinfo.szCSDVersion = Space$(128)
    retvalue = GetVersionExA(osinfo)

    With osinfo
        Select Case .dwPlatformId
            Case 1
                Select Case .dwMinorVersion
                    Case 0:         GetWindowsVersion = "Windows 95"
                    Case 10:        GetWindowsVersion = "Windows 98"
                    Case 90:        GetWindowsVersion = "Windows Millenium"
                End Select

            Case 2
                Select Case .dwMajorVersion
                    Case 3:         GetWindowsVersion = "Windows NT 3.51"
                    Case 4:         GetWindowsVersion = "Windows NT 4.0"

                    Case 5
                        Select Case .dwMinorVersion
                            Case 0: GetWindowsVersion = "Windows 2000"
                            Case 1: GetWindowsVersion = "Windows XP"
                            Case 2: GetWindowsVersion = "Windows 2003"
                        End Select

                    Case 6
                        Select Case .dwMinorVersion
                            Case 0: GetWindowsVersion = "Windows Vista/2008"
                            Case 1: GetWindowsVersion = "Windows 7/2008 R2"
                            Case 2: GetWindowsVersion = "Windows 8/2012"
                            Case 3: GetWindowsVersion = "Windows 8.1/2012 R2"
                        End Select
                End Select

            Case Else
                GetWindowsVersion = "Failed"
        End Select
    End With
End Function
0

. Windows 7 Ultimate, "XP" " 5.1"?

, , , . MS SysInfo.

Private Sub Command2_Click()

    Dim MsgEnd As String
    Select Case SysDetectOS.OSPlatform
        Case 0
            MsgEnd = "Unidentified"
        Case 1
            MsgEnd = "Windows 95, ver. " & _
                     CStr(SysDetectOS.OSVersion) & "(" & _
                     CStr(SysDetectOS.OSBuild) & ")"
        Case 2
            MsgEnd = "Windows NT, ver. " & _
                     CStr(SysDetectOS.OSVersion) & "(" & _
                     CStr(SysDetectOS.OSBuild) & ")"

            If SysDetectOS.OSVersion >= 6.01 Then
                MsgEnd = MsgEnd + " Win7"
            End If        
    End Select
    MsgBox "System: " & MsgEnd    
End Sub
-1
source

All Articles