简体   繁体   中英

NSIS Detect Windows Version

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

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)

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.

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

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM