Microsoft KB Archive/189249: Difference between revisions

From BetaArchive Wiki
(importing KB archive)
 
m (Text replacement - """ to """)
 
Line 101: Line 101:
<li>From the '''Project''' menu, add a '''Standard Module''' to the project.</li>
<li>From the '''Project''' menu, add a '''Standard Module''' to the project.</li>
<li><p>Insert the following code into '''Module1''':</p>
<li><p>Insert the following code into '''Module1''':</p>
<pre class="codesample">      Public Declare Function GetVersionExA Lib &quot;kernel32&quot; _
<pre class="codesample">      Public Declare Function GetVersionExA Lib "kernel32" _
               (lpVersionInformation As OSVERSIONINFO) As Integer
               (lpVersionInformation As OSVERSIONINFO) As Integer
   
   
Line 128: Line 128:
                     Select Case .dwMinorVersion
                     Select Case .dwMinorVersion
                         Case 0
                         Case 0
                             getVersion = &quot;Windows 95&quot;
                             getVersion = "Windows 95"
                         Case 10
                         Case 10
                             getVersion = &quot;Windows 98&quot;
                             getVersion = "Windows 98"
                         Case 90
                         Case 90
                             getVersion = &quot;Windows Millennium&quot;
                             getVersion = "Windows Millennium"
                     End Select
                     End Select
      
      
Line 138: Line 138:
                     Select Case .dwMajorVersion
                     Select Case .dwMajorVersion
                         Case 3
                         Case 3
                             getVersion = &quot;Windows NT 3.51&quot;
                             getVersion = "Windows NT 3.51"
                         Case 4
                         Case 4
                             getVersion = &quot;Windows NT 4.0&quot;
                             getVersion = "Windows NT 4.0"
                         Case 5
                         Case 5
                             If .dwMinorVersion = 0 Then
                             If .dwMinorVersion = 0 Then
                                 getVersion = &quot;Windows 2000&quot;
                                 getVersion = "Windows 2000"
                             Else
                             Else
                                 getVersion = &quot;Windows XP&quot;
                                 getVersion = "Windows XP"
                             End If
                             End If
                     End Select
                     End Select
      
      
                 Case Else
                 Case Else
                   getVersion = &quot;Failed&quot;
                   getVersion = "Failed"
             End Select
             End Select
   
   

Latest revision as of 12:40, 21 July 2020

Knowledge Base


Article ID: 189249

Article Last Modified on 9/3/2004



APPLIES TO

  • Microsoft Visual Basic 5.0 Learning Edition
  • Microsoft Visual Basic 6.0 Learning Edition
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic 6.0 Professional Edition
  • Microsoft Visual Basic 5.0 Enterprise Edition
  • Microsoft Visual Basic 6.0 Enterprise Edition
  • Microsoft Visual Basic 4.0 Standard Edition
  • Microsoft Visual Basic 4.0 Professional Edition
  • Microsoft Visual Basic for Applications 5.0



This article was previously published under Q189249

SUMMARY

An application may need to perform tasks differently depending on which operating system is running on the computer. This article shows, by example, how to differentiate between Microsoft Windows 95, Microsoft Windows 98, Microsoft Window NT 3.51, Microsoft Windows NT 4.0, Microsoft Windows 2000, and Microsoft Windows XP.

The Win32 GetVersionEx function returns information that a program can use to identify the operating system. Among those values are the major and minor revision numbers and a platform identifier. With the introduction of Windows 98, it now takes a more involved logical evaluation to determine which version of Windows is in use. The listing below provides the data needed to evaluate the OSVERSIONINFO structure populated by the GetVersionEx function:

Windows 95 Windows 98 Windows Me Windows NT 4.0 Windows 2000 Windows XP
PlatformID 1 1 1 2 2 2
Major Version 4 4 4 4 5 5
Minor Version 0 10 90 0 0 1


MORE INFORMATION

Step-by-step example

  1. Start a new Standard EXE project in Visual Basic. Form1 is created by default.
  2. From the Project menu, add a Standard Module to the project.
  3. Insert the following code into Module1:

          Public 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 Function getVersion() As String
                   Dim osinfo As OSVERSIONINFO
                   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
                                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
                                If .dwMinorVersion = 0 Then
                                    getVersion = "Windows 2000"
                                Else
                                    getVersion = "Windows XP"
                                End If
                        End Select
        
                    Case Else
                       getVersion = "Failed"
                End Select
     
                   End With
                End Function
    
                        
  4. Add the following line of code to the Load event of Form1:

    MsgBox GetVersion()
                        
  5. Run the project, and note that a message box displays the correct Windows version.


REFERENCES

For additional information, click the following article number to view the article in the Microsoft Knowledge Base:

92936 How to get Windows 3.1 version number in VB with GetVersion


Keywords: kbhowto KB189249