Déterminer la version de windows (fonction api getversion)

Soyez le premier à donner votre avis sur cette source.

Snippet vu 12 655 fois - Téléchargée 33 fois

Contenu du snippet

Ce petit programme permet de déterminer la version de Windows en utilisant la fonction API GetVersionExA.

Source / Exemple :


Placez le code suivant dans un module :

Option Explicit

Private Declare Function GetVersionExA Lib "kernel32" _
                 (lpVersionInformation As OSVERSIONINFO) As Integer
    
Private Type OSVERSIONINFO
    dwOSVersionInfoSize As Long
    dwMajorVersion As Long
    dwMinorVersion As Long
    dwBuildNumber As Long
    dwPlatformId As Long
    szCSDVersion As String * 128
    wServicePackMajor As Integer  ' Version Majeur du Service Pack
    wServicePackMinor As Integer  ' Version Mineur du Service Pack
End Type

Private Const VER_PLATFORM_WIN32s = 0       ' Win32s / Windows 3.1
Private Const VER_PLATFORM_WIN32_WINDOWS = 1 ' Windows 95, Windows 98, ou Windows Me
Private Const VER_PLATFORM_WIN32_NT = 2     ' Windows NT, Windows 2000, Windows XP, ou Windows Server 2003 familiale.

'         Version   Version
' Système   Platforme Majeur  Mineur   Build
' Windows 95  1   4    0
' Windows 98  1   4   10   1998
' Windows 98SE  1   4   10   2222
' Windows Me  1   4   90   3000
' NT 3.51   2   3   51
' NT    2   4    0   1381
' 2000    2   5    0
' XP    2   5    1   2600
' Server 2003   2   5    2

Public Function VBWinVer() As String
Dim OSInfo As OSVERSIONINFO
Dim retvalue As Integer
    
OSInfo.dwOSVersionInfoSize = 156
OSInfo.szCSDVersion = Space$(128)
retvalue = GetVersionExA(OSInfo)
With OSInfo
     Select Case .dwPlatformId
         Case VER_PLATFORM_WIN32s           ' Win32s / Windows 3.1
                VBWinVer = "Windows 3.1"
                 
         Case VER_PLATFORM_WIN32_WINDOWS    ' Windows 95, Windows 98,
                Select Case .dwMinorVersion ' ou Windows Me
                    Case 0
                        VBWinVer = "Windows 95"
                    Case 10
                        If (OSInfo.dwBuildNumber And &HFFFF&) = 2222 Then
                            VBWinVer = "Windows 98SE"
                        Else
                            VBWinVer = "Windows 98"
                        End If
                    Case 90
                        VBWinVer = "Windows Me"
                End Select
     
         Case VER_PLATFORM_WIN32_NT     ' Windows NT, Windows 2000, Windows XP,
                Select Case .dwMajorVersion ' ou Windows Server 2003 family.
                    Case 3
                        VBWinVer = "Windows NT 3.51"
                    Case 4
                        VBWinVer = "Windows NT 4.0"
                    Case 5
                        Select Case .dwMinorVersion
                            Case 0
                                VBWinVer = "Windows 2000"
                            Case 1
                                VBWinVer = "Windows XP"
                            Case 2
                                VBWinVer = "Windows Server 2003"
                        End Select
                End Select
                     
         Case Else
                VBWinVer = "Failed"
     End Select
     If VBWinVer <> "Failed" And .wServicePackMajor <> 0 Then
       'Ajouter la version du Service Pack Installé
        VBWinVer = VBWinVer & " Service Pack " & Trim(Str(.wServicePackMajor)) & "." & Trim(Str(.wServicePackMinor))
     End If
End With
End Function

'**************************************************************

Ensuite, placez le code suivant dans l'évenement Click d'un bouton dans un Form :

  MsgBox VBWinVer

Et voilà.

Conclusion :


Je me permet de mettre ce code source après avoir rechercher dans vbfrance un code similaire et que je n'ai pas trouvé d'exemples (Enfin je ne suis pas sûr).

Ce code est tiré de site suivant :

http://www.tek-tips.com/faqs.cfm?fid=4599

A voir également

Ajouter un commentaire

Commentaires

Messages postés
17286
Date d'inscription
mercredi 2 janvier 2002
Statut
Modérateur
Dernière intervention
23 décembre 2019
65
pour les versions plus récentes de Windows : (2000 et supérieure)

Option Explicit

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 OSVERSIONINFOEX
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128
wServicePackMajor As Integer
wServicePackMinor As Integer
wSuiteMask As Integer
wProductType As Byte
wReserved As Byte
End Type

Private Type SYSTEM_INFO
wProcessorArchitecture As Integer
wReserved As Integer
dwPageSize As Long
lpMinimumApplicationAddress As Long
lpMaximumApplicationAddress As Long
dwActiveProcessorMask As Long
dwNumberOrfProcessors As Long
dwProcessorType As Long
dwAllocationGranularity As Long
dwReserved As Long
End Type

Private Declare Function GetVersionEx Lib "kernel32.dll" Alias "GetVersionExA" (ByRef lpVersionInformation As Any) As Long
Private Declare Sub GetSystemInfo Lib "kernel32.dll" (ByRef lpSystemInfo As SYSTEM_INFO)
Private Declare Sub GetNativeSystemInfo Lib "kernel32.dll" (ByRef lpSystemInfo As SYSTEM_INFO)
Private Declare Function GetModuleHandle Lib "kernel32.dll" Alias "GetModuleHandleA" (ByVal lpModuleName As String) As Long
Private Declare Function GetProcAddress Lib "kernel32.dll" (ByVal hModule As Long, ByVal lpProcName As String) As Long
Private Declare Function GetProductInfo Lib "kernel32.dll" (ByVal dwOSMajorVersion As Long, ByVal dwOSMinorVersion As Long, ByVal dwSpMajorVersion As Long, ByVal dwSpMinorVersion As Long, ByRef pdwReturnedProductType As Long) As Long
Private Declare Function GetSystemMetrics Lib "user32.dll" (ByVal nIndex As Long) As Long

Private Const SM_SERVERR2 As Long = 89
Private Const VER_PLATFORM_WIN32_NT As Long = 2
Private Const VER_PLATFORM_WIN32_WINDOWS As Long = 1
Private Const VER_PLATFORM_WIN32s As Long = 0
Private Const VER_PLATFORMID As Long = &H8
Private Const VER_PRERELEASE As Long = 0
Private Const VS_FF_PRIVATEBUILD As Long = &H8&
Private Const VER_PRIVATEBUILD As Long = VS_FF_PRIVATEBUILD
Private Const VER_PRODUCT_TYPE As Long = &H80
Private Const VER_PRODUCTBETA_STR As String = ""
Private Const VER_PRODUCTBUILD As Long = 1000
Private Const VER_PRODUCTBUILD_QFE As Long = 1
Private Const VER_PRODUCTNAME_STR As String = "Platform SDK Sample Code"
Private Const VER_PRODUCTVERSION_DW As Long = (&H1000000 Or VER_PRODUCTBUILD)
Private Const VER_PRODUCTVERSION_STRING As String = "1.00"
Private Const VER_PRODUCTVERSION_W As Long = &H100
Private Const VER_SERVER_NT As Long = &H80000000
Private Const VER_SERVICEPACKMAJOR As Long = &H20
Private Const VER_SERVICEPACKMINOR As Long = &H10
Private Const PROCESSOR_ARCHITECTURE_AMD64 As Long = 9
Private Const PROCESSOR_ARCHITECTURE_ALPHA As Long = 2
Private Const PROCESSOR_ARCHITECTURE_ALPHA64 As Long = 7
Private Const PROCESSOR_ARCHITECTURE_ARM As Long = 5
Private Const PROCESSOR_ARCHITECTURE_IA64 As Long = 6
Private Const PROCESSOR_ARCHITECTURE_INTEL As Long = 0
Private Const PROCESSOR_ARCHITECTURE_MIPS As Long = 1
Private Const PROCESSOR_ARCHITECTURE_MSIL As Long = 8
Private Const PROCESSOR_ARCHITECTURE_PPC As Long = 3
Private Const PROCESSOR_ARCHITECTURE_UNKNOWN As Long = &HFFFF&
Private Const VER_SUITE_BACKOFFICE As Long = &H4
Private Const VER_SUITE_COMMUNICATIONS As Long = &H8
Private Const VER_SUITE_DATACENTER As Long = &H80
Private Const VER_SUITE_EMBEDDEDNT As Long = &H40
Private Const VER_SUITE_ENTERPRISE As Long = &H2
Private Const VER_SUITE_SINGLEUSERTS As Long = &H100
Private Const VER_SUITE_PERSONAL As Long = &H200
Private Const VER_SUITE_BLADE As Long = &H400
Private Const VER_SUITE_COMPUTE_SERVER As Long = &H4000
Private Const VER_SUITE_STORAGE_SERVER As Long = &H2000
Private Const VER_SUITE_SMALLBUSINESS As Long = &H1
Private Const VER_SUITE_SMALLBUSINESS_RESTRICTED As Long = &H20
Private Const VER_SUITE_TERMINAL As Long = &H10
Private Const VER_SUITENAME As Long = &H40
Private Const VER_NT_DOMAIN_CONTROLLER As Long = &H2
Private Const VER_NT_SERVER As Long = &H3
Private Const VER_NT_WORKSTATION As Long = &H1
Private Const PRODUCT_BUSINESS_N As Long = &H10
Private Const PRODUCT_BUSINESS As Long = &H6
Private Const PRODUCT_CLUSTER_SERVER As Long = &H12
Private Const PRODUCT_DATACENTER_SERVER As Long = &H8
Private Const PRODUCT_DATACENTER_SERVER_CORE As Long = &HC
Private Const PRODUCT_DATACENTER_SERVER_CORE_V As Long = &H27
Private Const PRODUCT_DATACENTER_SERVER_V As Long = &H25
Private Const PRODUCT_ENTERPRISE As Long = &H4
Private Const PRODUCT_ENTERPRISE_N As Long = &H1B
Private Const PRODUCT_ENTERPRISE_SERVER As Long = &HA
Private Const PRODUCT_ENTERPRISE_SERVER_CORE As Long = &HE
Private Const PRODUCT_ENTERPRISE_SERVER_CORE_V As Long = &H29
Private Const PRODUCT_ENTERPRISE_SERVER_IA64 As Long = &HF
Private Const PRODUCT_ENTERPRISE_SERVER_V As Long = &H26
Private Const PRODUCT_HOME_BASIC As Long = &H2
Private Const PRODUCT_HOME_BASIC_N As Long = &H5
Private Const PRODUCT_HOME_PREMIUM As Long = &H3
Private Const PRODUCT_HOME_PREMIUM_N As Long = &H1A
Private Const PRODUCT_HOME_SERVER As Long = &H13
Private Const PRODUCT_MEDIUMBUSINESS_SERVER_MANAGEMENT As Long = &H1E
Private Const PRODUCT_MEDIUMBUSINESS_SERVER_MESSAGING As Long = &H20
Private Const PRODUCT_MEDIUMBUSINESS_SERVER_SECURITY As Long = &H1F
Private Const PRODUCT_SERVER_FOR_SMALLBUSINESS As Long = &H18
Private Const PRODUCT_SMALLBUSINESS_SERVER As Long = &H9
Private Const PRODUCT_SMALLBUSINESS_SERVER_PREMIUM As Long = &H19
Private Const PRODUCT_STANDARD_SERVER As Long = &H7
Private Const PRODUCT_STANDARD_SERVER_CORE As Long = &HD
Private Const PRODUCT_STANDARD_SERVER_CORE_V As Long = &H28
Private Const PRODUCT_STANDARD_SERVER_V As Long = &H24
Private Const PRODUCT_STARTER As Long = &HB
Private Const PRODUCT_STORAGE_ENTERPRISE_SERVER As Long = &H17
Private Const PRODUCT_STORAGE_EXPRESS_SERVER As Long = &H14
Private Const PRODUCT_STORAGE_STANDARD_SERVER As Long = &H15
Private Const PRODUCT_STORAGE_WORKGROUP_SERVER As Long = &H16
Private Const PRODUCT_ULTIMATE As Long = &H1
Private Const PRODUCT_ULTIMATE_N As Long = &H1C
Private Const PRODUCT_UNDEFINED As Long = &H0
Private Const PRODUCT_WEB_SERVER As Long = &H11
Private Const PRODUCT_WEB_SERVER_CORE As Long = &H1D

Public Function GetOSDisplayString() As String
Dim osvi As OSVERSIONINFOEX
Dim si As SYSTEM_INFO
Dim bOsVersionInfoEx As Boolean
Dim dwType As Long
Dim sBuffer As String
osvi.dwOSVersionInfoSize = Len(osvi)
If GetVersionEx(osvi) Then
'# Call GetNativeSystemInfo if supported or GetSystemInfo otherwise.
If GetProcAddress(GetModuleHandle("kernel32.dll"), "GetNativeSystemInfo") <> 0 Then
GetNativeSystemInfo si
Else
GetSystemInfo si
End If

If (VER_PLATFORM_WIN32_NT = osvi.dwPlatformId And osvi.dwMajorVersion > 4) Then
sBuffer = "Microsoft "

'# Test for the specific product.
If osvi.dwMajorVersion 6 And osvi.dwMinorVersion 0 Then
If osvi.wProductType = VER_NT_WORKSTATION Then
sBuffer = sBuffer & "Windows Vista "
Else
sBuffer = sBuffer & "Windows Server 2008 "
End If

GetProductInfo 6, 0, 0, 0, dwType

Select Case dwType
Case PRODUCT_ULTIMATE
sBuffer = sBuffer & "Ultimate Edition"
Case PRODUCT_HOME_PREMIUM
sBuffer = sBuffer & "Home Premium Edition"
Case PRODUCT_HOME_BASIC
sBuffer = sBuffer & "Home Basic Edition"
Case PRODUCT_ENTERPRISE
sBuffer = sBuffer & "Enterprise Edition"
Case PRODUCT_BUSINESS
sBuffer = sBuffer & "Business Edition"
Case PRODUCT_STARTER
sBuffer = sBuffer & "Starter Edition"
Case PRODUCT_CLUSTER_SERVER
sBuffer = sBuffer & "Cluster Server Edition"
Case PRODUCT_DATACENTER_SERVER
sBuffer = sBuffer & "Datacenter Edition"
Case PRODUCT_DATACENTER_SERVER_CORE
sBuffer = sBuffer & "Datacenter Edition (core installation)"
Case PRODUCT_ENTERPRISE_SERVER
sBuffer = sBuffer & "Enterprise Edition"
Case PRODUCT_ENTERPRISE_SERVER_CORE
sBuffer = sBuffer & "Enterprise Edition (core installation)"
Case PRODUCT_ENTERPRISE_SERVER_IA64
sBuffer = sBuffer & "Enterprise Edition for Itanium-based Systems"
Case PRODUCT_SMALLBUSINESS_SERVER
sBuffer = sBuffer & "Small Business Server"
Case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM
sBuffer = sBuffer & "Small Business Server Premium Edition"
Case PRODUCT_STANDARD_SERVER
sBuffer = sBuffer & "Standard Edition"
Case PRODUCT_STANDARD_SERVER_CORE
sBuffer = sBuffer & "Standard Edition (core installation)"
Case PRODUCT_WEB_SERVER
sBuffer = sBuffer & "Web Server Edition"
End Select
If si.wProcessorArchitecture = PROCESSOR_ARCHITECTURE_AMD64 Then
sBuffer = sBuffer & ", 64-bit"
ElseIf si.wProcessorArchitecture = PROCESSOR_ARCHITECTURE_INTEL Then
sBuffer = sBuffer & ", 32-bit"
End If
ElseIf osvi.dwMajorVersion 5 And osvi.dwMinorVersion 2 Then
If GetSystemMetrics(SM_SERVERR2) Then
sBuffer = sBuffer & "Windows Server 2003 R2, "
ElseIf osvi.wSuiteMask = VER_SUITE_STORAGE_SERVER Then
sBuffer = sBuffer & "Windows Storage Server 2003"
ElseIf osvi.wProductType VER_NT_WORKSTATION And si.wProcessorArchitecture PROCESSOR_ARCHITECTURE_AMD64 Then
sBuffer = sBuffer & "Windows XP Professional x64 Edition"
Else
sBuffer = sBuffer & "Windows Server 2003, "
End If

'# Test for the server type.
If osvi.wProductType <> VER_NT_WORKSTATION Then
If si.wProcessorArchitecture = PROCESSOR_ARCHITECTURE_IA64 Then
If (osvi.wSuiteMask And VER_SUITE_DATACENTER) = VER_SUITE_DATACENTER Then
sBuffer = sBuffer & "Datacenter Edition for Itanium-based Systems"
ElseIf (osvi.wSuiteMask And VER_SUITE_ENTERPRISE) = VER_SUITE_ENTERPRISE Then
sBuffer = sBuffer & "Enterprise Edition for Itanium-based Systems"
End If
ElseIf si.wProcessorArchitecture = PROCESSOR_ARCHITECTURE_AMD64 Then
If (osvi.wSuiteMask And VER_SUITE_DATACENTER) = VER_SUITE_DATACENTER Then
sBuffer = sBuffer & "Datacenter x64 Edition"
ElseIf (osvi.wSuiteMask And VER_SUITE_ENTERPRISE) = VER_SUITE_ENTERPRISE Then
sBuffer = sBuffer & "Enterprise x64 Edition"
Else
sBuffer = sBuffer & "Standard x64 Edition"
End If
Else
If (osvi.wSuiteMask And VER_SUITE_COMPUTE_SERVER) = VER_SUITE_COMPUTE_SERVER Then
sBuffer = sBuffer & "Compute Cluster Edition"
ElseIf (osvi.wSuiteMask And VER_SUITE_DATACENTER) = VER_SUITE_DATACENTER Then
sBuffer = sBuffer & "Datacenter Edition"
ElseIf (osvi.wSuiteMask And VER_SUITE_ENTERPRISE) = VER_SUITE_ENTERPRISE Then
sBuffer = sBuffer & "Enterprise Edition"
ElseIf (osvi.wSuiteMask And VER_SUITE_BLADE) = VER_SUITE_BLADE Then
sBuffer = sBuffer & "Web Edition"
Else
sBuffer = sBuffer & "Standard Edition"
End If
End If
End If
ElseIf osvi.dwMajorVersion 5 And osvi.dwMinorVersion 1 Then
sBuffer = sBuffer & "Windows XP "
If (osvi.wSuiteMask And VER_SUITE_PERSONAL) = VER_SUITE_PERSONAL Then
sBuffer = sBuffer & "Home Edition"
Else
sBuffer = sBuffer & "Professional"
End If
ElseIf osvi.dwMajorVersion 5 And osvi.dwMinorVersion 0 Then
sBuffer = sBuffer & "Windows 2000 "
If osvi.wProductType = VER_NT_WORKSTATION Then
sBuffer = sBuffer & "Professional"
Else
If (osvi.wSuiteMask And VER_SUITE_DATACENTER) = VER_SUITE_DATACENTER Then
sBuffer = sBuffer & "Datacenter Server"
ElseIf (osvi.wSuiteMask And VER_SUITE_ENTERPRISE) = VER_SUITE_ENTERPRISE Then
sBuffer = sBuffer & "Advanced Server"
Else
sBuffer = sBuffer & "Server"
End If
End If
End If

'# Include service pack (if any) and build number.
sBuffer = sBuffer & " " & osvi.szCSDVersion
sBuffer = Left$(sBuffer, InStr(sBuffer, vbNullChar) - 1)
sBuffer = sBuffer & " (build " & osvi.dwBuildNumber & ")"
End If
Else

End If
GetOSDisplayString = sBuffer
End Function
Messages postés
1352
Date d'inscription
lundi 27 octobre 2003
Statut
Membre
Dernière intervention
19 novembre 2008
1
Merci pour cette explication :)
Messages postés
21042
Date d'inscription
jeudi 23 janvier 2003
Statut
Modérateur
Dernière intervention
21 août 2019
21
la version extend est reservee aux noyaux NT.
En fait on se sert de la meme structure, suffit de modifier la valeur de son 1er membre size. On teste l'appel une fois avec size de extended et si failure c'est pas NT donc on refait l'appel apres avoir mis le size pour la version bebe. Voila tout.
Messages postés
1352
Date d'inscription
lundi 27 octobre 2003
Statut
Membre
Dernière intervention
19 novembre 2008
1
Mais c'est génial ca ;) Merci Bru

et moi qui regardais OSVERSIONINFO seulement.

Enfait pourquoi ce "EX" ? Dans MSDN ya OSVERSIONINFO et OSVERSIONINFOEx ! Pour quoi cette différence ?

NB : Mise à Jour faite avec la détermination de la version du Service Pack.
Messages postés
21042
Date d'inscription
jeudi 23 janvier 2003
Statut
Modérateur
Dernière intervention
21 août 2019
21
Regarde dans MSDN, c'est explique pour le service pack.
Soit tu remets un appel avec une struct etendue OSVERSIONINFOEX.
Soit tu lis dans la base de registres.
Afficher les 12 commentaires

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.