Nsis检测Windows版本具体实现的三种方式

用Nsis打包安装程序时,常用之一的就是根据软件需求检测相应的Windows版本,其有关脚本可在网络上可搜索到不少。
检测Windows版本的脚本主要依赖插件或头文件,具体实施时,有可能会遇到问题,本文就此作一浅探。如需更多了解,
可参阅所介绍的插件、头文件的相关说明文件。
1. 使用Version插件
    NSIS v2.51 集成增强版和NSIS v3.03绿色版都带有version.dll,大小都为6 KB,NSIS v2.51 集成增强版中的
Docs\Version文件夹有version.dll的Readme.txt,但经实际使用,都为旧版,编译时会出错。需从http://nsis.sourceforge.net/Version_plug-in页面下载NSIS_version_plugin_03.zip(http://nsis.sourceforge.net/mediawiki/images/b/b8/NSIS_version_plugin_03.zip),其说明指出其检测的Windows为:
Windows95、Windows98、WindowsME、WindowsNT351、WindowsNT40、Windows2000、WindowsXP、Windows2003、WindowsXPx64、WindowsVista、WindowsServer2008、Windows7、WindowsServer2008R2、Windows8、WindowsServer2012
举例如下:
!include Sections.nsh
!include "LogicLib.nsh"
name    "version判断Win7操作系统"
outfile 'version插件判断Win7操作系统.exe'
Section
SectionEnd
var isWindows7
Function .onInit
 #检测 Windows 7
  Version::IsWindows7
  #获取值
  Pop $isWindows7
  ${if} $isWindows7 == "1"
    MessageBox MB_OK "Its Windows 7!"
  ${Else}
    MessageBox MB_OK "Its not Windows 7!"
  ${EndIf}
 FunctionEnd
 
2. 使用Getversion插件
    Getversion.dll 插件能获取Windows名称,允许的值如下:
Server 2012、8、Server 2008 R2、7、Server 2008、Vista、Server 2003 R2、Server 2003、XP、XP x64、2000、CE、NT、ME、98、98 SE、95、95 OSR2、Win32s
举例如下:
name    "判断32位XP操作系统"
outfile '判断32位XP操作系统.exe'
Section
SectionEnd
Function .onInit
GetVersion::WindowsName
Pop $0
StrCmp $0 "XP" 0 Invalid_OS
GetVersion::WindowsPlatformArchitecture
Pop $0
StrCmp $0 "32" Valid_OS Invalid_OS
Invalid_OS:
MessageBox MB_OK "该软件只能安装在32位Windows XP操作系统上。 "
Valid_OS:
FunctionEnd
该脚本在NSIS v2.51 集成增强版编译顺利通过,并且运行正常,结果准确。
换成检测Windows 7的相应脚本:
GetVersion::WindowsName
Pop $0
StrCmp $0 "7" Valid_OS Invalid_OS
Invalid_OS:
MessageBox MB_OK "该软件只能装在Win7系统上。"
Valid_OS:
       在NSIS v2.51 集成增强版和NSIS v3.03绿色版编译通过,并且可运行,但结果却不正确。经查这两个版本所含的GetVersion.dll插件虽然修改日期不相同,但大小都为5.00 Kb,从http://nsis.sourceforge.net/GetVersion_(Windows)_plug-in页面下载GetVersion.zip (http://nsis.sourceforge.net/mediawiki/images/8/8a/GetVersion.zip) 解压包内大小为6.00 Kb,1.7.0.0版的GetVersion.dll 替换原来的 GetVersion.dll后,编译通过且运行结果准确。
3. 使用Winver.nsh头文件
    Getversion的作者已中止其更新并推荐使用Winver.nsh头文件来判断Windows的版本,Winver.nsh允许的值如下:
95、98、ME、NT4、2000、XP、2003、Vista、2008、7、2008R2、8、2012、8.1、2012R2、10
并支持 AtLeastWin 检测是否高于指定版本和AtMostWin 检测是否低于指定版本。
举例如下:
name    "使用头文件判断操作系统"
outfile '使用头文件判断操作系统.exe'
!include "MUI.nsh"
!include "WinVer.nsh"
!insertmacro MUI_LANGUAGE "simpchinese"
Section
SectionEnd
Function .onInit
${If} ${atleastwin10}
MessageBox MB_OK "系统为 windows 10 系统或更高!"
${EndIf}
FunctionEnd
系统检测的其它脚本:
${If} ${IsWin10}
MessageBox MB_OK "系统为 windows 10 系统!"
${EndIf}
${If} ${IsWinVista}
  MessageBox MB_OK "系统为 Vista !"
${EndIf}
${If} ${IsWin2000}
${OrIf} ${IsWinXP}
  MessageBox MB_OK "系统为 2000 或 XP!"
${EndIf}
${If} ${AtMostWinXP}
  MessageBox MB_OK "系统版本为 XP 或更低版本的系统!"
${EndIf}
       脚本在NSIS 3.03编译通过并且运行正常,结果准确。在低于NSIS 3.0版本中编译,如检测Windows10版本会报错,无法通过。
有些版本的Winver.nsh头文件还可能会与其它的头文件冲突,其表现为在Winver.nsh或相冲突的头文件处高亮显示并报错,
所以最好在NSIS 3.0以上的版本中编译。
       如上所述,在检测Windows版本时,可根据自己手头已有的NSIS编译版本和软件安装需求,选择相应的方式。

你可能感兴趣的:(Nsis检测Windows版本具体实现的三种方式)