NSIS中判断.net framework 是否安装

时间:2021-05-16 16:57:19

在网上找了几个脚本,原理都是读取注册表进行判断,都可以用。

1、CheckDotNetFramework

/*
* Name: CheckDotNetFramework.nsh
* Version: 0.1
* Date: 20110720
*
* Author: Michael Mefford
* Contact info: meffordm@gmail.com
*
* Description: NSIS header file to check a windows system for a specified .NET
* framework. CheckDotNetFramework.nsh uses the NSIS stack to
* receive and pass values.
*
* Modified from: http://nsis.sourceforge.net/How_to_Detect_any_.NET_Framework
*
* License: Copyright (C) 2011 Michael Mefford
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the author(s) be held liable for any
* damages arising from the use of this software.
*
* Permission is granted to anyone to use this software for any
* purpose, including commercial applications, and to alter it and
* redistribute it freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented;
* you must not claim that you wrote the original software.
* If you use this software in a product, an acknowledgment in
* the product documentation would be appreciated but is not
* required.
*
* 2. Altered versions must be plainly marked as such,
* and must not be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any
* distribution.
*
* Usage: Push ${DotNetFrameworkVersion}
* Call CheckDotNetFramework
* Exch $R0
* StrCmp $R0 "0" found not_found
*
* Algorithm: ...
*
* Input: A .NET Framework version. This must be verbatim, including major,
* minor, and build version - i.e.
*
* 1.1.4322
* 2.0.50727
* 3.0
* 3.5
* 4
* 4.0
* .
* .
* .
* etc.
*
* Output: "0" if the requested .Net Framework version IS FOUND
* "1" if the requested .NET Framework version IS NOT FOUND
*
*/
Function CheckDotNetFramework /* Exchange $R0 with the top of the stack to get the value passed by caller */
Exch $R0 /* Save other NSIS registers */
Push $R1
Push $R2
Push $R3 /* Zero out $R2 for the indexer */
StrCpy $R3 "0" loop:
/* Get each sub key under "SOFTWARE\Microsoft\NET Framework Setup\NDP" */
EnumRegKey $R1 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP" $R3
StrCmp $R1 "" version_not_found /* Requested version is not found */ StrCpy $R2 $R1 "" 1 /* Remove "v" from subkey */
StrCmp $R2 $R0 version_found /* Requested version is found */ IntOp $R3 $R3 + 1 /* Increment registry key index */
Goto loop /* The requested .Net Framework version WAS NOT FOUND on this system */
version_not_found:
/* Restore the registers saved earlier */
Pop $R3
Pop $R2
Pop $R1
Pop $R0 Push "1" /* Put "1" on the top of the stack for caller to use */
Goto end /* The requested .Net Framework version WAS FOUND on this system */
version_found:
/* Restore the registers saved earlier */
Pop $R3
Pop $R2
Pop $R1
Pop $R0 Push "0" /* Put "0" on the top of the stack for caller to use */
end: FunctionEnd

使用方法:

	;检查是否安装了3.5
StrCpy $DotNetFrameworkVersion 3.5
Push $DotNetFrameworkVersion
Call CheckDotNetFramework
Exch $R0
${If} $R0 == "1" ; 没有找到
MessageBox MB_ICONSTOP|MB_OK "程序运行所必需的 Microsoft .NET Framework$DotNetFrameworkVersion 尚未安装,请先安装该组件。" IDYES
abort
${ElseIf} $R0 == "0" ; 找到指定版本的DotNetFramework
;检查是否安装了4.0
pop $DotNetFrameworkVersion
StrCpy $DotNetFrameworkVersion 4.0
Push $DotNetFrameworkVersion
Call CheckDotNetFramework
Exch $R0
${If} $R0 == "1"
MessageBox MB_ICONSTOP|MB_OK "程序运行所必需的 Microsoft .NET Framework$DotNetFrameworkVersion 尚未安装,请先安装该组件。" IDYES
abort
${EndIf}
${Endif}

  

2、GetNetFrameworkVersion

Function GetNetFrameworkVersion
;获取.Net Framework版本支持
Push $1
Push $0
ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full" "Install"
ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full" "Version"
StrCmp $0 1 KnowNetFrameworkVersion +1
ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5" "Install"
ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5" "Version"
StrCmp $0 1 KnowNetFrameworkVersion +1
ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0\Setup" "InstallSuccess"
ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0\Setup" "Version"
StrCmp $0 1 KnowNetFrameworkVersion +1
ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v2.0.50727" "Install"
ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v2.0.50727" "Version"
StrCmp $1 "" +1 +2
StrCpy $1 "2.0.50727.832"
StrCmp $0 1 KnowNetFrameworkVersion +1
ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v1.1.4322" "Install"
ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v1.1.4322" "Version"
StrCmp $1 "" +1 +2
StrCpy $1 "1.1.4322.573"
StrCmp $0 1 KnowNetFrameworkVersion +1
ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\.NETFramework\policy\v1.0" "Install"
ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\.NETFramework\policy\v1.0" "Version"
StrCmp $1 "" +1 +2
StrCpy $1 "1.0.3705.0"
StrCmp $0 1 KnowNetFrameworkVersion +1
StrCpy $1 "not .NetFramework"
KnowNetFrameworkVersion:
Pop $0
Exch $1
FunctionEnd

  

使用方法:

Call GetNetFrameworkVersion
Pop $1
${If} $1 < '2.0.50727'
MessageBox MB_OK "正在安装 .NET Framework..."
SetDetailsPrint textonly
DetailPrint "正在安装 .NET Framework..."
SetDetailsPrint listonly SetOutPath "$TEMP"
SetOverwrite on
;File "NetFx20SP2_x86.exe"
ExecWait '$TEMP\NetFx20SP2_x86.exe /quiet /norestart' $R1
Delete "$TEMP\NetFx20SP2_x86.exe"
${EndIf}

  

  

3、封装成宏

这种方式不用在脚本中添加函数代码,复用性好,有两个缺点:1)不能在一个脚本中调用多次进行多个版本的判断;2)参数中必须传入DOTNETVMAJOR DOTNETVMINOR DOTNETVMINORMINOR,如果想查4或者4.0版本,脚本中会变成4.或者4.0.,造成找不到,需要改写一下这个脚本,限于目前自己的水平,还改不了。

!macro DotNetSearch DOTNETVMAJOR DOTNETVMINOR DOTNETVMINORMINOR DOTNETLASTFUNCTION DOTNETPATH
Var /GLOBAL DOTNET1
Var /GLOBAL DOTNET2
Var /GLOBAL DOTNET3
Var /GLOBAL DOTNET4
Var /GLOBAL DOTNET5
Var /GLOBAL DOTNET6
Push $DOTNET1
Push $DOTNET2
Push $DOTNET3
Push $DOTNET4
Push $DOTNET5
Push $DOTNET6 StrCpy $DOTNET1 "0"
StrCpy $DOTNET2 "SOFTWARE\Microsoft\.NETFramework"
StrCpy $DOTNET3 0 DotNetStartEnum:
EnumRegKey $DOTNET4 HKLM "$DOTNET2\policy" $DOTNET3 StrCmp $DOTNET4 "" noDotNet dotNetFound dotNetFound:
StrCpy $DOTNET5 $DOTNET4 1 0
StrCmp $DOTNET5 "v" +1 goNextDotNet
StrCpy $DOTNET5 $DOTNET4 1 1 IntCmp $DOTNET5 ${DOTNETVMAJOR} +1 goNextDotNet yesDotNetReg
StrCpy $DOTNET5 $DOTNET4 1 3 IntCmp $DOTNET5 ${DOTNETVMINOR} +1 goNextDotNet yesDotNetReg
StrCmp ${DOTNETVMINORMINOR} "" yesDotNetReg yesDotNetReg
;StrCmp ${DOTNETVMINORMINOR} "" yesDotNetReg +1
;Changed this line (otherwise it would not work with my setup!) - Vinz0r IntCmpU $DOTNET5 ${DOTNETVMINORMINOR} yesDotNetReg goNextDotNet yesDotNetReg goNextDotNet:
IntOp $DOTNET3 $DOTNET3 + 1
Goto DotNetStartEnum yesDotNetReg:
EnumRegValue $DOTNET3 HKLM "$DOTNET2\policy\$DOTNET4" 0
StrCmp $DOTNET3 "" noDotNet
ReadRegStr $DOTNET5 HKLM $DOTNET2 "InstallRoot"
StrCmp $DOTNET5 "" noDotNet
StrCpy $DOTNET5 "$DOTNET5$DOTNET4.$DOTNET3\mscorlib.dll"
IfFileExists $DOTNET5 yesDotNet noDotNet noDotNet:
StrCmp ${DOTNETLASTFUNCTION} "INSTALL_ABORT" +1 nDN2
MessageBox MB_YESNO|MB_ICONQUESTION \
"You must have Microsoft .NET Framework version ${DOTNETVMAJOR}.${DOTNETVMINOR}.${DOTNETVMINORMINOR}$\nor higher installed. Install now?" \
IDYES +2 IDNO +1
Abort
ExecWait '${DOTNETPATH}'
Goto DotNetStartEnum
nDN2:
StrCmp ${DOTNETLASTFUNCTION} "INSTALL_NOABORT" +1 nDN3
MessageBox MB_YESNO|MB_ICONQUESTION \
"Microsoft .NET Framework version ${DOTNETVMAJOR}.${DOTNETVMINOR}.${DOTNETVMINORMINOR} is not installed.$\nDo so now?" \
IDYES +1 IDNO +3
ExecWait '${DOTNETPATH}'
Goto DotNetStartEnum
StrCpy $DOTNET1 0
Goto DotNetFinish
nDN3:
StrCmp ${DOTNETLASTFUNCTION} "WARNING" +1 nDN4
MessageBox MB_OK|MB_ICONEXCLAMATION \
"Warning:$\n$\n$\t$\tMicrosoft .NET Framework version$\n$\t$\t${DOTNETVMAJOR}.${DOTNETVMINOR}.${DOTNETVMINORMINOR} is not installed!" \
IDOK 0
StrCpy $DOTNET1 0
Goto DotNetFinish
nDN4:
StrCmp ${DOTNETLASTFUNCTION} "ABORT" +1 nDN5
MessageBox MB_OK|MB_ICONEXCLAMATION \
"Error:$\n$\n$\t$\tMicrosoft .NET Framework version$\n$\t$\t${DOTNETVMAJOR}.${DOTNETVMINOR}.${DOTNETVMINORMINOR} is not installed, aborting!" \
IDOK 0
Abort
nDN5:
StrCmp ${DOTNETLASTFUNCTION} "IGNORE" +1 nDN6
StrCpy $DOTNET1 0
Goto DotNetFinish
nDN6:
MessageBox MB_OK \
"$(^Name) Setup internal error.$\nMacro 'DotNetSearch', parameter '4'(${DOTNETLASTFUNCTION})invalid.$\nValue must be INSTALL_ABORT|INSTALL_NOABORT|WARNING|ABORT|IGNORE$\nSorry for the inconvenience.$\n$\tAborting..." \
IDOK 0
Abort yesDotNet:
StrCpy $DOTNET1 1 DotNetFinish:
Pop $DOTNET6
Pop $DOTNET5
Pop $DOTNET4
Pop $DOTNET3
Pop $DOTNET2
!define ${DOTNETOUTCOME} $DOTNET1
Exch $DOTNET1
!macroend