[Windows] WindowsVersion()
Posted: Sat Jun 17, 2017 9:20 am
The return value of the built-in Function OSVersion() is not always reliable, when a program which was compiled with an older PB version runs on newer Windows versions.
For instance, OSVersion() of PB 5.31 returns 90 when running on Windows 8 ... but it also returns 90 when running on Windows 10. So it's not possible for the program to distinguish between Windows 8 and Windows 10. The following code can do so.
//edit 2022-11-14:
See updated version below by ChrisR, which also covers Windows 11.
BTW, in case you wonder at the post below in this thread by that "expert" (who has repeatedly been banned from the forum): In contrast to his belief, it's not me who is responsible for his confusion.
For instance, OSVersion() of PB 5.31 returns 90 when running on Windows 8 ... but it also returns 90 when running on Windows 10. So it's not possible for the program to distinguish between Windows 8 and Windows 10. The following code can do so.
//edit 2022-11-14:
See updated version below by ChrisR, which also covers Windows 11.
BTW, in case you wonder at the post below in this thread by that "expert" (who has repeatedly been banned from the forum): In contrast to his belief, it's not me who is responsible for his confusion.

Code: Select all
; after
; https://msdn.microsoft.com/en-us/library/windows/hardware/ff563620(v=vs.85).aspx
; https://stackoverflow.com/questions/38102565/how-can-a-vb-6-app-determine-if-it-is-running-on-windows-10
; tested with PB 5.31, 5.44 LTS, 5.60
EnableExplicit
Prototype.i protoRtlGetVersion (*ver.OSVERSIONINFOEX)
Procedure.s WindowsVersion()
Protected ver.OSVERSIONINFOEX
Protected RtlGetVersion.protoRtlGetVersion
Protected hDLL.i
ver\dwOSVersionInfoSize = SizeOf(ver)
hDLL = OpenLibrary(#PB_Any, "ntdll.dll")
If hDLL
RtlGetVersion = GetFunction(hDLL, "RtlGetVersion")
CloseLibrary(hDLL)
EndIf
If RtlGetVersion = 0 Or RtlGetVersion(@ ver) <> 0
ProcedureReturn "Error: Can't call Windows API function RtlGetVersion()."
EndIf
If ver\dwPlatformId <> #VER_PLATFORM_WIN32_NT
ProcedureReturn "No Windows NT-based operating system"
EndIf
Select ver\dwMajorVersion
Case 3
If ver\wProductType = #VER_NT_WORKSTATION
ProcedureReturn "Windows NT 3.5 Workstation"
Else
ProcedureReturn "Windows NT 3.5 Server"
EndIf
Case 4
If ver\wProductType = #VER_NT_WORKSTATION
ProcedureReturn "Windows NT 4.0 Workstation"
Else
ProcedureReturn "Windows NT 4.0 Server"
EndIf
Case 5
Select ver\dwMinorVersion
Case 0
If ver\wProductType = #VER_NT_WORKSTATION
ProcedureReturn "Windows 2000 Workstation"
Else
ProcedureReturn "Windows 2000 Server"
EndIf
Case 1
If (ver\wSuiteMask & #VER_SUITE_PERSONAL)
ProcedureReturn "Windows XP Home Edition"
Else
ProcedureReturn "Windows XP Professional"
EndIf
Case 2
If ver\wProductType = #VER_NT_WORKSTATION
ProcedureReturn "Windows XP 64-bit Edition"
Else
ProcedureReturn "Windows Server 2003"
EndIf
EndSelect
Case 6
Select ver\dwMinorVersion
Case 0
If ver\wProductType = #VER_NT_WORKSTATION
ProcedureReturn "Windows Vista"
Else
ProcedureReturn "Windows Server 2008"
EndIf
Case 1
If ver\wProductType = #VER_NT_WORKSTATION
ProcedureReturn "Windows 7"
Else
ProcedureReturn "Windows Server 2008 R2"
EndIf
Case 2
If ver\wProductType = #VER_NT_WORKSTATION
ProcedureReturn "Windows 8"
Else
ProcedureReturn "Windows Server 2012"
EndIf
Case 3
If ver\wProductType = #VER_NT_WORKSTATION
ProcedureReturn "Windows 8.1"
Else
ProcedureReturn "Windows Server 2012 R2"
EndIf
EndSelect
Case 10
If ver\wProductType = #VER_NT_WORKSTATION
ProcedureReturn "Windows 10"
Else
ProcedureReturn "Windows Server 2016"
EndIf
EndSelect
ProcedureReturn "Unknown Windows version"
EndProcedure
Debug WindowsVersion()