DÉTERMINER LA VERSION DE WINDOWS (FONCTION API GETVERSION)

Signaler
Messages postés
521
Date d'inscription
mardi 24 décembre 2002
Statut
Membre
Dernière intervention
11 septembre 2008
-
Messages postés
17286
Date d'inscription
mercredi 2 janvier 2002
Statut
Modérateur
Dernière intervention
23 décembre 2019
-
Cette discussion concerne un article du site. Pour la consulter dans son contexte d'origine, cliquez sur le lien ci-dessous.

https://codes-sources.commentcamarche.net/source/27507-determiner-la-version-de-windows-fonction-api-getversion

Messages postés
17286
Date d'inscription
mercredi 2 janvier 2002
Statut
Modérateur
Dernière intervention
23 décembre 2019
62
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
1356
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
20
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
1356
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.
Afficher les 12 commentaires