VB6対応

 

Visual Basic 中学校 > VB6 サンプル >

Windowsのバージョンを取得する

 

以下のWindowsVersion関数を使うとWindowsのバージョンを「Windows XP」のような文字列で取得することができる。目下のところWindows3.1より前のバージョンと、Windows Vista以降のバージョンは正しく判別できない。

VB6対応

Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (VersionInfo As OSVERSIONINFO) As Boolean

Private Type OSVERSIONINFO
    dwOSVersionInfoSize As Long 'この構造体のサイズ
    dwMajorVersion      As Long 'メジャーバージョン
    dwMinorVersion      As Long 'マイナーバージョン
    dwBuildNumber       As Long 'ビルド番号
    dwPlatformId        As Long '3.1系、95系、NT系の区別
    szCSDVersion(127)   As Byte 'ServicePack情報など(Unicode文字列の配列)
End Type
'■WindowsVersion
'■機能:Windowsのバージョンを返す。
'■戻り値:Winodwsのバージョン名を表す文字列。例:"Window 98"

Public Function WindowsVersion() As String

    Dim VersionInfo As OSVERSIONINFO
    Dim ret As Boolean
    Dim WindowsID As Long

    '●API関数の呼び出し
    VersionInfo.dwOSVersionInfoSize = Len(VersionInfo)
    ret = GetVersionEx(VersionInfo)
    If ret = False Then
        Exit Function
    End If

    '●どのWindowsか判定
    With VersionInfo

        WindowsID = .dwPlatformId & Format(.dwMajorVersion, "00") & Format(.dwMinorVersion, "0000")

        Select Case WindowsID
            Case Is < 1040000
                WindowsVersion = "Windows 3.1"
            Case 1040000
                WindowsVersion = "Windows 95"
            Case 1040010
                WindowsVersion = "Windows 98"
            Case 1040090
                WindowsVersion = "Windows Me"
            Case 2030051
                WindowsVersion = "Windows NT 3.51"
            Case 2040000
                WindowsVersion = "Windows NT 4.0"
            Case 2050000
                WindowsVersion = "Windows 2000"
            Case 2050001
                WindowsVersion = "Windows XP"
            Case 2050002
                WindowsVersion = "Windows Server 2003"
            Case Is > 2050002
                WindowsVersion = "Windows Vista or Later"
            Case Is > 1040090
                WindowsVersion = "Windows Me Later"
            Case Else
                WindowsVersion = "Windows Unknown"
        End Select

    End With

End Function

 


VB.NET2002対応 VB.NET2003対応 VB2005対応 →Environment.OSVersionを使用します。簡単に判別するにはEnvironment.OSVersion.ToStringとします。