OSの情報取得             <TOP>


OSの情報を取得します。

GetVersionEx オペレーティングシステムの種類やバージョンに関する情報を取得
GetWindowsDirectory Windowsディレクトリのパス名を取得
GetSystemDirectory Windows のシステムディレクトリのパスを取得
 

Windows9xとWindows2000以降では分けて取得します。

ついでにWindowsフォル ダ、およびSystemフォルダも取得します。

ビルド番号のデータの格納方法
 全体は、4バイトのLong型変数で、Windows 95 の場合、下位2バイトがビルド番号になっています。
 下位2バイトは、4バイトの値を2の16乗で割った余り、もしくは2の16乗との論理積(AND) の計算から得られます。
WindowsXP

Windows2000

Windows98

 

'================================================================
'= OSの情報取得
'=    (GetVersionEx.bas)
'================================================================
#include"Windows.bi"

'------------------------------
'OSのバージョン取得(Windows9x)
'------------------------------
Type OSVERSIONINFO
    dwOSVersionInfoSize As Long    '構造体のバイト数
    dwMajorVersion      As Long    'メジャーバージョン番号
    dwMinorVersion      As Long    'マイナーバージョン番号
    dwBuildNumber       As Long    'ビルド番号
    dwPlatformID        As Long    'プラットフォームのID
    szCSDVersion        As String * 128 'OSに関する付加情報
End Type

' オペレーティングシステムの種類やバージョンに関する情報を取得
Declare Function Api_GetVersionEx& Lib "Kernel32" Alias "GetVersionExA" (lpVersionInformation As OSVERSIONINFO)

'------------------------------
'OSのバージョン取得(WindowsNT、2000、XP)
'------------------------------
Type OSVERSIONINFOEX
    dwOSVersionInfoSize As Long
    dwMajorVersion      As Long
    dwMinorVersion      As Long
    dwBuildNumber       As Long
    dwPlatformID        As Long
    szCSDVersion        As String * 128 'Maintenance string for PSS usage
    wSPMajor            As Integer      'Service Pack Major Version
    wSPMinor            As Integer      'Service Pack Minor Version
    wSuiteMask          As Integer      'Suite Identifier
    bProductType        As Byte         'Server / Workstation / Domain Controller ?
    bReserved           As Byte         'Reserved
End Type


' オペレーティングシステムの種類やバージョンに関する情報を取得
Declare Function Api_GetVersionEx2& Lib "Kernel32" Alias "GetVersionExA" (lpVersionInformation As OSVERSIONINFOEX)

#define VER_NT_SERVER &H3               '
#define VER_NT_WORKSTATION &H1          '
#define VER_PLATFORM_WIN32_NT 2         'WindowsNT、2000、XP
#define VER_PLATFORM_WIN32_WINDOWS 1    'Windows9x
#define VER_PLATFORM_WIN32s 0           'Windows3.1
#define VER_SERVER_NT &H80000000        '
#define VER_SUITE_BACKOFFICE &H4        '
#define VER_SUITE_BLADE &H400           '
#define VER_SUITE_COMMUNICATIONS &H8    '
#define VER_SUITE_DATACENTER &H80       '
#define VER_SUITE_EMBEDDEDNT &H40       '
#define VER_SUITE_ENTERPRISE &H2        '
#define VER_SUITE_PERSONAL &H200        '
#define VER_SUITE_SINGLEUSERTS &H100    '
#define VER_SUITE_SMALLBUSINESS &H1     '
#define VER_SUITE_SMALLBUSINESS_RESTRICTED &H20 '
#define VER_SUITE_TERMINAL &H10         '
#define VER_WORKSTATION_NT &H40000000   '

'------------------------------
'Windows・Systemフォルダ取得
'------------------------------
' Windowsディレクトリのパス名を取得
Declare Function Api_GetWindowsDirectory& Lib "Kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer$, ByVal nSize&)

' Windows のシステムディレクトリのパスを取得。システムディレクトリには、Windows ライブラリ、ドライバなどのファイルが置かれている
Declare Function Api_GetSystemDirectory& Lib "Kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer$, ByVal nSize&)

Var Shared Text1 As Object

Text1.Attach GetDlgItem("Text1") : Text1.SetFontSize 12

'================================================================
'=
'================================================================
Declare Sub MainForm_Start edecl ()
Sub MainForm_Start()
    Var ovi As OSVERSIONINFO
    Var OSType As String
    Var MajorVer As Long
    Var MinorVer As Long
    Var PlatformID As Long
    Var CsdVer As String
    Var WorkStr As String
    Var WinPath As String * 255
    Var SysPath As String * 255
    Var nWinPath As String
    Var nSysPath As String
    Var WinPathLen As Long
    Var SysPathLen As Long
    Var CrLf As String
    Var txt As String
    Var Ret As Long

    CrLf = Chr$(13, 10)
    Text1.SetWindowtext ""

    ovi.dwOSVersionInfoSize = Len(ovi)
    Ret = Api_GetVersionEx(ovi)
    If Ret = 0 Then Exit Sub

    '------------------------------
    'Windows・Systemフォルダを取得
    '------------------------------
    WinPathLen = Api_GetWindowsDirectory(WinPath, Len(WinPath))
    SysPathLen = Api_GetSystemDirectory(SysPath, Len(SysPath))

    nWinPath = Left$(WinPath, InStr(WinPath, Chr$(0)) - 1)
    nSysPath = Left$(SysPath, InStr(SysPath, Chr$(0)) - 1)

    '------------------------------
    'ovi で Version を取得
    '------------------------------
    MajorVer = ovi.dwMajorVersion
    MinorVer = ovi.dwMinorVersion
    PlatformID = ovi.dwPlatformID

    '------------------------------
  'Windows2000 以上と以下で処理を分岐
    '------------------------------
    Select Case MajorVer
        Case 3
            OSType = "Windows NT 3.51"
        Case 4
            Select Case MinorVer
                Case 0
                    If CsdVer = "C" Then
                        OSType = "Windows 95 OSR2"
                    Else If PlatformID = VER_PLATFORM_WIN32_NT Then
                        OSType = "Windows NT 4.0"
                    Else
                        OSType = "Windows 95"
                    End If
                Case 10
                    If CsdVer = "A" Then
                        OSType = "Windows 98 SE"
                    Else
                        OSType = "Windows 98"
                    End If
                Case 90
                    OSType = "Windows Me"
                Case Else
                    Text1.SetWindowtext " Windows ??"
            End Select

            txt =        " OSType      : " & OSType & CrLf
            txt = txt & " Major Version    : " & Trim$(Str$(ovi.dwMajorVersion)) & CrLf
            txt = txt & " Minor Version    : " & Trim$(Str$(ovi.dwMinorVersion)) & CrLf
            txt = txt & " Build Number    : " & Trim$(Str$(ovi.dwBuildNumber And &HFFFF)) & CrLf
            txt = txt & " Platform ID     : " & Trim$(Str$(ovi.dwPlatformID)) & CrLf
            txt = txt & " CSD Version   : " & Left$(ovi.szCSDVersion, InStr(ovi.szCSDVersion, Che$(0)) - 1) & CrLf
            txt = txt & " Windows Folder   : " & nWinPath & CrLf
            txt = txt & " System Folder    : " & nSysPath

            Text1.SetWindowtext txt

            '------------------------------
            'Windows2000 以上の場合 oviEx で再取得
            '------------------------------
        Case 5
            Var oviEx As OSVERSIONINFOEX
            Var ProductType As byte
            Var SuiteMask As Integer

            oviEx.dwOSVersionInfoSize = Len(oviEx)

            Ret = Api_GetVersionEx2(oviEx)
            If Ret = 0 Then Exit Sub

            MajorVer = oviEx.dwMajorVersion
            MinorVer = oviEx.dwMinorVersion
            ProductType = oviEx.bProductType
            SuiteMask = oviEx.wSuiteMask

            Select Case MinorVer
                Case 0
                    OSType = "Windows 2000"
                Case 1
                    '------------------------------
                    'Workstation かどうかを取得
                    '------------------------------
                    If ProductType = VER_NT_WORKSTATION Then
                        WorkStr = "Workstation"
                    Else If ProductType = VER_NT_SERVER Then
                        WorkStr = "Server"
                    End If

                    If VER_SUITE_PERSONAL <> 0 Then
                        OSType = "Windows XP Home Edition"
                    Else
                        OSType = "Windows XP Professional Edition"
                    End If
                Case Else
                    Text1.SetWindowtext " Windows ??"
            End Select

            txt =        " OSType      : " & OSType & CrLf
            txt = txt & " Major Version  : " & Trim$(Str$(oviEx.dwMajorVersion)) & CrLf
            txt = txt & " Minor Version  : " & Trim$(Str$(oviEx.dwMinorVersion)) & CrLf
            txt = txt & " Build Number   : " & Trim$(Str$(oviEx.dwBuildNumber And &HFFFF)) & CrLf
            txt = txt & " Platform ID   : " & Trim$(Str$(oviEx.dwPlatformID)) & CrLf
            txt = txt & " CSD Version   : " & Left$(oviEx.szCSDVersion, InStr(oviEx.szCSDVersion, Chr$(0)) - 1) & CrLf
            txt = txt & " ServicePackMajor : " & Trim$(Str$(oviEx.wSPMajor)) & CrLf
            txt = txt & " ServicePackMinor : " & Trim$(Str$(oviEx.wSPMinor)) & CrLf
            txt = txt & " SuiteMask    : " & Trim$(Str$(oviEx.wSuiteMask)) & CrLf
            txt = txt & " ProductType   : " & Trim$(Str$(oviEx.bProductType)) & CrLf
            txt = txt & " Workstation or  : " & WorkStr & CrLf
            txt = txt & " Reserved     : " & Trim$(Str$(oviEx.bReserved)) & CrLf
            txt = txt & " Windows Folder   : " & nWinPath & CrLf
            txt = txt & " System Folder    : " & nSysPath

            Text1.SetWindowtext txt
        Case Else
            Text1.SetWindowtext " Windows ??"
    End Select
End Sub

'================================================================
'=
'================================================================
While 1
    WaitEvent
Wend
Stop
End