Results 1 to 9 of 9

Thread: Get Windows Version

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2014
    Posts
    354

    Get Windows Version

    y question about VB.Net and VB6.
    I have in VB6 application this code in module:
    Code:
    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
    
    Public sngWindowsVersion As Single
    Public strWindowsVersion As String
    
    Public Sub GetWindowsVersion()
    Dim osinfo As OSVERSIONINFO
    Dim retvalue As Integer
        osinfo.dwOSVersionInfoSize = 148
        osinfo.szCSDVersion = Space$(128)
        retvalue = GetVersionExA(osinfo)
        sngWindowsVersion = CSng((CStr(osinfo.dwMajorVersion) & "." & CStr(osinfo.dwMinorVersion)))
        strWindowsVersion = CStr(osinfo.dwMajorVersion) & "." & CStr(osinfo.dwMinorVersion) & "." & CStr(osinfo.dwBuildNumber)
    End Sub
    And this code in Form:
    Code:
        GetWindowsVersion
        Text2.Text = sngWindowsVersion
        Text1.Text = strWindowsVersion
    For Windows 7 I get 6.1.7601 and 6.1
    It is OK
    But for Windows 8.1 I get 6.2.9200 and 6.2
    For Windows 10 Enterprise RTM I get the same 6.2.9200 and 6.2

    As MS site says 6.1 it is Win 7, 6.2 is Win 8, 6.3 is Win 8.1

    I tried several other methods including VB.net with this code:
    Code:
            MessageBox.Show(System.Environment.OSVersion.ToString)
    I get the same wrong result.

    I tried to add SysInfo1 control to VB6 form and use this code:
    Code:
        Dim HancyRockz As String
        HancyRockz = "OsVersion :- " & SysInfo1.OSVersion & " / Built " & SysInfo1.OSBuild
        Text1.Text = HancyRockz
    I see via Control Panel in Win 7 correct information, I see the version 10.0.10240 via System Information in Win 10. Unfortunately I do not know how to check the version number with build in Win8.1, but as I said before MS says that win 8.1 is 6.3 version.

    I just found out that using command ver at command prompt I can see OS version. I checked Win 8.1 and saw 8.3.9200, I checked Win 10 and saw 10.0.10240

    Can anybody help me with getting OS version using VB 6 or VB.Net?

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,588

    Re: Get Windows Version

    There is no need for an API, use the OperatingSystem class.

  3. #3
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Get Windows Version

    Your issue is described in "Operating system version changes in Windows 8.1 and Windows Server 2012 R2"

    For VB.Net, go to the Project Menu->ProjName Properties->Application Tab->View Window Settings Button to open the "app.Manifest" file. Then un-comment the "<supportedOS" tags. This will allow System.Environment.OSVersion to get the proper values.

    Here is an alternate way to get the information using the API function RtlGetVersion. This is VB.Net code, but hopefully it should get you on the right track for VB6 as well.

    Code:
    Dim ver As New RTL_OSVERSIONINFOEXW
    
    ver.dwOSVersionInfoSize = CUInt(Marshal.SizeOf(ver))
    RtlGetVersion(ver)
    Code:
    <StructLayout(LayoutKind.Sequential)> _
    Public Class RTL_OSVERSIONINFOEXW
        Public dwOSVersionInfoSize As UInt32
        Public dwMajorVersion As UInt32
        Public dwMinorVersion As UInt32
        Public dwBuildNumber As UInt32
        Public dwPlatformId As UInt32
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=&H80)> _
        Public szCSDVersion As String
        Public wServicePackMajor As UInt16
        Public wServicePackMinor As UInt16
        Public wSuiteMask As UInt16
        Public bProductType As Byte
        Public bReserved As Byte
    End Class
    
    <Runtime.InteropServices.DllImport("ntdll.dll", ExactSpelling:=True)> _
    Private Shared Function RtlGetVersion(<[In](), Out()> ByVal PRTL_OSVERSIONINFOW As RTL_OSVERSIONINFOEXW) As Int32
    End Function
    Last edited by TnTinMN; Jul 22nd, 2015 at 10:26 PM. Reason: Corrected name of RTL_OSVERSIONINFOEXW

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2014
    Posts
    354

    Re: Get Windows Version

    Thank you very much. Unfortunately due to my weak knowledge of VB.Net I cannot understand how to use the code you provided. And of cource I have no idea how to convert (translate) it to VB6 code. My current goal is to rewrite the existing VB6 code checking Windows version.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2014
    Posts
    354

    Re: Get Windows Version

    So far I found this way to get the version:
    Code:
    Private Sub Command1_Click()
    Dim strCommand As String
    Dim intFile As Integer
    Dim strData As String
        strCommand = "ver > C:\Users\Vlad\Desktop\Result.txt"
        Shell "cmd.exe /c " & strCommand, vbHide
        
        intFile = FreeFile()
        Open "C:\Users\Vlad\Desktop\Result.txt" For Input As #intFile
        strData = Input(LOF(intFile), #intFile)
        Close #intFile
        
        MsgBox (strData)
        Kill "C:\Users\chapran\Desktop\Result.txt"
    End Sub
    I just do not like it because it works with file.

  6. #6
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Get Windows Version

    So what you are saying that given the the name of the API function (RtlGetVersion) to use instead of GetVersionEx function, that you are unable to look up the documentation of that new function and write the VB6 "Declare Function" syntax and that what you really want is for some one to give you a cut & paste solution for VB6. It is not that hard. I don't have VB6 but the syntax of Office VB6 is the same as VB6 and it took a few seconds for me to type it out. Give it a try.

    Also, I'm asking that this thread be moved to the VB6 forum since that is your real interest.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2014
    Posts
    354

    Re: Get Windows Version

    Thanks to all who helped me. The final code in form (VB6)
    Code:
    Private Sub Command1_Click()
        MsgBox NativeGetVersion
    End Sub
    :

    And in module:
    Code:
    Declare Function RtlGetVersion Lib "NTDLL" (ByRef lpVersionInformation As Long) As Long
    
    Public Function NativeGetVersion() As String
    Dim tOSVw(&H54) As Long
        tOSVw(0) = &H54 * &H4
        Call RtlGetVersion(tOSVw(0))
        NativeGetVersion = Join(Array(tOSVw(1), tOSVw(2), tOSVw(3)), ".")
        MsgBox VersionToName(Join(Array(tOSVw(1), tOSVw(2)), "."))
    End Function
    
    Public Function VersionToName(ByRef sVersion As String) As String
        Select Case sVersion
            Case "5.1": VersionToName = "Windows XP"
            Case "5.3": VersionToName = "Windows 2003 (SERVER)"
            Case "6.0": VersionToName = "Windows Vista"
            Case "6.1": VersionToName = "Windows 7"
            Case "6.2": VersionToName = "Windows 8"
            Case "6.3": VersionToName = "Windows 8.1"
            Case "10.0": VersionToName = "Windows 10"
            Case Else: VersionToName = "Unknown"
        End Select
    End Function

  8. #8
    PowerPoster
    Join Date
    Feb 2006
    Posts
    21,036

    Re: Get Windows Version

    If you want to use GetVersionEx() calls, and you care about the Windows 8.1+ version lie appcompat shim... then you bypass it by using appropriate supportedOS nodes in your application manifest. See Application Manifest and related articles.

    For many programs this doesn't matter, and such programs may be far better off living with the lie. This wasn't implemented in Win 8.1 just for the heck of it.

    When it does matter you use a manifest or some alternative call. But just trying to gratuitously display the name of the OS in an application is silly.

    You see similar issues from people who fell for the notion that VB6.exe should have the Windows XP version-lie shim manually applied. That one is actually bad because of changes between XP and all current versions of Windows that a program does need to care about.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2014
    Posts
    354

    Re: Get Windows Version

    I just found out that all codes I used around July 23 doesn't work anymore. All give Windows XP on Windows 10 Enterprise.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width