Get Windows version

From NSIS Wiki

Author: sunjammer (talk, contrib)


Contents

Semi-Obsolete

NSIS includes a header file aptly named WinVer.nsh since version 2.21. It allows for easy detection of the Windows version using LogicLib macros and has automatic future-support so you can always tell if the system at hand, even if undetected, is newer or older than what you need.
!include WinVer.nsh
 
Function .onInit
  ${IfNot} ${AtLeastWinXP}
    MessageBox MB_OK "XP and above required"
    Quit
  ${EndIf}
  ${If} ${IsWin2003}
  ${AndIfNot} ${AtLeastServicePack} 1
    MessageBox MB_OK "2003 SP1 is required"
    Quit
  ${EndIf}
FunctionEnd

The current stable release of NSIS (2.46) only supports up through Windows 7 and Windows 2008. The alpha release of NSIS (3.0a2) supports Windows 8 and 8.1. If you wish to detect Windows 8 and 8.1 rather than just ${AtLeastWindows7} or similar with NSIS stable you can use this script or update your local WinVer.nsh with the missing defines and macros from NSIS 3 (This will fix Windows 8 but 8.1 lies unless you have the correct manifest).

Description

A simple function to return the version of Windows that the script is running on. It can detect Windows 95 through Windows 8.1, returns the version number if it is Windows NT 3 or 4 and returns a blank string if it can not determine the Windows version.

Usage

Include the script as normal in your NSIS script. Call it as:

${GetWindowsVersion} $R0

At this point, $R0 will contain one of the following: 95, 98, ME, NT x.x, 2000, XP, 2003, Vista, 7, 8 or a blank string (for unknown). For NT 3 and 4, the version number will be included.

The Script

; GetWindowsVersion 3.1 (2014-04-04)
;
; Based on Yazno's function, http://yazno.tripod.com/powerpimpit/
; Update by Joost Verburg
; Update (Macro, Define, Windows 7 detection) - John T. Haller of PortableApps.com - 2008-01-07
; Update (Windows 8 detection) - Marek Mizanin (Zanir) - 2013-02-07
; Update (Windows 8.1 detection) - John T. Haller of PortableApps.com - 2014-04-04
;
; Usage: ${GetWindowsVersion} $R0
;
; $R0 contains: 95, 98, ME, NT x.x, 2000, XP, 2003, Vista, 7, 8, 8.1 or '' (for unknown)
 
Function GetWindowsVersion
 
  Push $R0
  Push $R1
 
  ClearErrors
 
  ReadRegStr $R0 HKLM \
  "SOFTWARE\Microsoft\Windows NT\CurrentVersion" CurrentVersion
 
  IfErrors 0 lbl_winnt
 
  ; we are not NT
  ReadRegStr $R0 HKLM \
  "SOFTWARE\Microsoft\Windows\CurrentVersion" VersionNumber
 
  StrCpy $R1 $R0 1
  StrCmp $R1 '4' 0 lbl_error
 
  StrCpy $R1 $R0 3
 
  StrCmp $R1 '4.0' lbl_win32_95
  StrCmp $R1 '4.9' lbl_win32_ME lbl_win32_98
 
  lbl_win32_95:
    StrCpy $R0 '95'
  Goto lbl_done
 
  lbl_win32_98:
    StrCpy $R0 '98'
  Goto lbl_done
 
  lbl_win32_ME:
    StrCpy $R0 'ME'
  Goto lbl_done
 
  lbl_winnt:
 
  StrCpy $R1 $R0 1
 
  StrCmp $R1 '3' lbl_winnt_x
  StrCmp $R1 '4' lbl_winnt_x
 
  StrCpy $R1 $R0 3
 
  StrCmp $R1 '5.0' lbl_winnt_2000
  StrCmp $R1 '5.1' lbl_winnt_XP
  StrCmp $R1 '5.2' lbl_winnt_2003
  StrCmp $R1 '6.0' lbl_winnt_vista
  StrCmp $R1 '6.1' lbl_winnt_7
  StrCmp $R1 '6.2' lbl_winnt_8
  StrCmp $R1 '6.3' lbl_winnt_81 lbl_error
 
  lbl_winnt_x:
    StrCpy $R0 "NT $R0" 6
  Goto lbl_done
 
  lbl_winnt_2000:
    Strcpy $R0 '2000'
  Goto lbl_done
 
  lbl_winnt_XP:
    Strcpy $R0 'XP'
  Goto lbl_done
 
  lbl_winnt_2003:
    Strcpy $R0 '2003'
  Goto lbl_done
 
  lbl_winnt_vista:
    Strcpy $R0 'Vista'
  Goto lbl_done
 
  lbl_winnt_7:
    Strcpy $R0 '7'
  Goto lbl_done
 
  lbl_winnt_8:
    Strcpy $R0 '8'
  Goto lbl_done
 
  lbl_winnt_81:
    Strcpy $R0 '8.1'
  Goto lbl_done
 
  lbl_error:
    Strcpy $R0 ''
  lbl_done:
 
  Pop $R1
  Exch $R0
 
FunctionEnd
 
!macro GetWindowsVersion OUTPUT_VALUE
	Call GetWindowsVersion
	Pop `${OUTPUT_VALUE}`
!macroend
 
!define GetWindowsVersion '!insertmacro "GetWindowsVersion"'
Personal tools
donate
ads