NSIS Detect Windows Version
Asked Answered
M

4

9

In NSIS is there a way to determine what version of windows the user is currently running?

The reason I want to do this is because my installer looks different on a Windows XP computer. My installer uses MUI2 but I dont seem to have the same GUI buttons(I think its called XP Style) as I do in Windows 7 and the main installer window is much larger than in Windows 7(where its about 500 by 400 pixels). Is it normal to a have these differences in an installer using MUI2? I thought MUI2 made the look consistant in windows versions XP and up?

To overcome the difference in installer window size, my solution is to detect if the user is using Windows XP and resize the window accordingly. Is this possible?

I need to have the window a specific size because I have a background image and the image is 500px wide so if the installer window is bigger I have a blank gap. I can change the background image to be wider but the easiest solution for myself is the one I explained above

Mader answered 19/1, 2013 at 0:25 Comment(1)
Maybe post screenshots of XP and 7 so we can see what you are talking about?Dillydally
S
8

In case Anders' answer is not explicit enough (took me a few hours to get it right), here is a more "beginner's friendly" version.

You will need to add !include WinVer.nsh to the top section of the cd.nsi file.

You then can use code like this:

${If} ${IsWinXP}
     MessageBox MB_OK|MB_ICONEXCLAMATION "We have Win XP"
${EndIf}

This is the only function I tested, but the WinVer.nsh file starts with a mini-manual with its functions, which include:

  • AtLeastWin<version> which checks if the installer is running on Windows version at least as specified.
  • IsWin<version> which checks if the installer is running on Windows version exactly as specified.
  • AtMostWin<version> which checks if the installer is running on Windows version at most as specified.

<version> can be replaced with the following values (and maybe more, depending on how recent your WinVer.nsh file is): 95, 98, ME, NT4, 2000, XP, 2003, Vista, 2008, 7, 2008R2

There are some more functions and some usage examples in the WinVer.nsh file, which is probably located somewhere like C:\Program Files\NSIS\Include, like:

  • AtLeastServicePack which checks if the installer is running on Windows service pack version at least as specified.
  • IsServicePack which checks if the installer is running on Windows service pack version exactly as specified.
  • AtMostServicePack which checks if the installer is running on Windows service version pack at most as specified.
  • IsWin2003R2 (no more details supplied)
  • IsStarterEdition (no more details supplied)
  • OSHasMediaCenter (no more details supplied)
  • OSHasTabletSupport (no more details supplied)
Statecraft answered 2/6, 2014 at 12:23 Comment(1)
N.B. According to the nsis.sourceforge.net/Get_Windows_version page, the WinVer.nsh is fairly obsolete. Better to add the GetWindowsVersion function that's further down the page. That has been extended to match upto Windows 10 / Server 2016. Just copy and paste the script into the top of your .nsi file. Then you can compare the output via ${GetWindowsVersion} $R0, StrCmp $R0 "10.0" win10 not_win10 (You can also get Windows 7 / 8.1 when you expect Windows 10 if you've done an upgrade).Reinhard
D
7

MUI does not resize the window based on the Windows version. The window size is affected by the font and DPI settings however.

Use WinVer.nsh to detect the Windows version. This module is included in the NSIS includes folder by default.

Dillydally answered 19/1, 2013 at 10:16 Comment(0)
P
3

The snippet bellow shows how to identify the Windows Version with as many detail as I could imagine to be useful:

!include WinVer.nsh
!include "LogicLib.nsh"

Function LogWinVer

    ${WinVerGetMajor} $R0
    ${WinVerGetMinor} $R1
    ${WinVerGetBuild} $R2
    ${WinVerGetServicePackLevel} $R3

    ; determine windows product name
    ${If} $R0 == 5 
            ${If} $R1 == 0
                    DetailPrint "Windows 2000 SP $R3"
            ${ElseIf} $R1 == 1
                    DetailPrint "Windows XP SP $R3"
            ${ElseIf} $R1 == 2
                    DetailPrint "Windows Server 2003 SP $R3"
            ${EndIf}
    ${ElseIf} $R0 == 6
            ${If} $R1 == 0
                    ${If} ${IsServerOS}
                            DetailPrint "Windows Server 2008 SP $R3"
                    ${Else}
                            DetailPrint "Windows Vista SP $R3"
                    ${EndIf}
            ${ElseIf} $R1 == 1
                    ${If} ${IsServerOS}
                            DetailPrint "Windows Server 2008 R2 SP $R3"
                    ${Else}
                            DetailPrint "Windows 7 SP $R3"
                    ${EndIf}
            ${ElseIf} $R1 == 2
                    ${If} ${IsServerOS} 
                            DetailPrint "Windows Server 2012 SP $R3"
                    ${Else}
                            DetailPrint "Windows 8 SP $R3"
                    ${EndIf}
            ${ElseIf} $R1 == 3
                    ${If} ${IsServerOS}
                            DetailPrint "Windows Server 2012 R2 SP $R3"
                    ${Else}
                            DetailPrint "Windows 8.1 SP $R3"
                    ${EndIf}
            ${EndIf}
    ${EndIf}

    ; version
    DetailPrint "Kernel $R0.$R1 build $R2"


    ; x86 or x64:
    Call LogWinVer
    System::Call "kernel32::GetCurrentProcess() i .s"
    System::Call "kernel32::IsWow64Process(i s, *i .r0)"
    StrCmp $0 "0" is32bit is64bit

    is32bit:
            DetailPrint "32 bit"
            Goto exit
    is64bit:
            DetailPrint "64 bit"
    exit:
FunctionEnd
Prestigious answered 15/7, 2014 at 14:26 Comment(0)
P
2

You could also read from the registry directly:

ReadRegStr $WinEdition HKLM "SOFTWARE\Microsoft\Windows NT\CurrentVersion" "ProductName"

Next, you can compare it using "==", for example:

${If} $WinEdition == "Windows XP"

or you could use StrContains to check if the windows version contains "Windows XP"

Pie answered 1/9, 2014 at 10:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.