Conoscere il Sistema Operativo in uso

Per sapere su quale S.O. si sta lavorando, bisogna utilizzare le API come la GetVersionEx.

Inserire in un modulo bas:

Option Explicit

'Costanti per determinare il Sistema Operativo
Private Const VER_PLATFORM_WIN32s = 0
Private Const VER_PLATFORM_WIN32_WINDOWS = 1
Private Const VER_PLATFORM_WIN32_NT = 2
Private Const MAX_PATH = 260
Private Const UNKNOWN_OS = 0
Private Const WINDOWS_NT_3_51 = 1
Private Const WINDOWS_95 = 2
Private Const WINDOWS_NT_4 = 3
Private Const WINDOWS_98 = 4
Private Const WINDOWS_2000 = 5
Private Const WINDOWS_ME = 6
Private Const WINDOWS_XP = 7
'UDT per determinare il Sistema Operativo
Private Type OSVERSIONINFO
    dwOSVersionInfoSize As Long
    dwMajorVersion As Long
    dwMinorVersion As Long
    dwBuildNumber As Long
    dwPlatformId As Long
    szCSDVersion As String * 128
End Type
Private Type FILETIME
    dwLowDateTime As Long
    dwHighDateTime As Long
End Type
Private Type WIN32_FIND_DATA
    dwFileAttributes As Long
    ftCreationTime As FILETIME
    ftLastAccessTime As FILETIME
    ftLastWriteTime As FILETIME
    nFileSizeHigh As Long
    nFileSizeLow As Long
    dwReserved0 As Long
    dwReserved1 As Long
    cFileName As String * MAX_PATH
    cAlternate As String * 14
End Type
Private Declare Function GetVersionEx Lib "kernel32.dll" _
Alias "GetVersionExA" _
(lpVersionInformation As OSVERSIONINFO) As Long

'Piattaforma Sistema Operativo
Public IdPlatform As Integer
'Identificazione numerica del Sistema Operativo
Public WinVer As Long
'Identificazione letterale del Sistema Operativo
Public OpSys As String

'====================================================
' FUNZIONE
'====================================================

Public Function GetWindowsVersion() As Long
Dim osinfo As OSVERSIONINFO
Dim retvalue As Integer
osinfo.dwOSVersionInfoSize = 148
osinfo.szCSDVersion = Space$(128)
retvalue = GetVersionEx(osinfo)
With osinfo
    IdPlatform = .dwPlatformId
    Select Case .dwPlatformId
        Case VER_PLATFORM_WIN32_WINDOWS
            If .dwMinorVersion = 0 Then
                GetWindowsVersion = WINDOWS_95
            ElseIf .dwMinorVersion = 10 Then
                GetWindowsVersion = WINDOWS_98
            ElseIf .dwMinorVersion = 90 Then
                GetWindowsVersion = WINDOWS_ME
        End If
    Case VER_PLATFORM_WIN32_NT
        If .dwMajorVersion = 3 Then
            GetWindowsVersion = WINDOWS_NT_3_51
        ElseIf .dwMajorVersion = 4 Then
            GetWindowsVersion = WINDOWS_NT_4
        ElseIf .dwMajorVersion = 5 Then
            If .dwMinorVersion = 0 Then
                GetWindowsVersion = WINDOWS_2000
            ElseIf .dwMinorVersion = 1 Then
                GetWindowsVersion = WINDOWS_XP
            Else
                GetWindowsVersion = UNKNOWN_OS
            End If
        End If
    Case Else
        GetWindowsVersion = UNKNOWN_OS
    End Select
End With
End Function

Public Sub LeggiOpSys()
WinVer = GetWindowsVersion
Select Case WinVer
    Case 0
' Sistema Operativo Sconosciuto
        OpSys = "Sconosciuto"
    Case 1
'Windows NT 3.51
        OpSys = "Windows NT 3.51"
    Case 2
'Windows 95
        OpSys = "Windows 95"
    Case 3
'Windows NT 4.0
        OpSys = "Windows NT 4.0"
    Case 4
'Windows 98
        OpSys = "Windows 98"
    Case 5
'Windows 2000
        OpSys = "Windows 2000"
    Case 6
'Windows ME
        OpSys = "Windows ME"
    Case 7
'Windows XP
        OpSys = "Windows XP"
End Select
End Sub


In un Form, inserire un CommandButton ed il seguente codice:

Option Explicit

Private Sub Command1_Click()
LeggiOpSys
If IdPlatform = 1 Then
    MsgBox "Piattaforma: Windows 9x/Me" & vbCrLf & "Sistema Operativo: " & OpSys
ElseIf IdPlatform = 2 Then
    MsgBox "Piattaforma: Windows Nt/2000/Xp" & vbCrLf & "Sistema Operativo: " & OpSys
Else
    MsgBox "Piattaforma: Sconosciuta" & vbCrLf & "Sistema Operativo: " & OpSys
End If
End Sub

Insieme al S.O.ho visualizzato anche il tipo di piattaforma (Windows 9x/Me o Windows NT/2000) perchč in molti casi č pių importante il tipo di piattaforma che il Sistema Operativo vero e proprio.

Testato su: Tutti i Sistemi Operativi