使用NSIS制作安装包(1)

NSIS是一个基于脚本语言的制作安装包的免费工具,它使用起来也很方便,下面是制作安装.NET框架和语言包的脚本。

NSIS的编译器和脚本编辑工具可以到nsis.sf.net上下载

!include WordFunc.nsh
!insertmacro VersionCompare
!include LogicLib.nsh

Name ".NET运行库"
OutFile "DotNetSetup.exe"

var "DOTNETFX_RETURN_CODE"
var "DOTNETLang_RETURN_CODE"
var "ERROR_DOTNET_FATAL"
var "INSTALL_SUCCESS"

Function .onInit
    StrCpy $ERROR_DOTNET_FATAL "在安装.NET运行库时出现一个严重的错误,安装.NET运行库失败!"
    StrCpy $INSTALL_SUCCESS "安装成功!"
   
    Call GetDotNETVersion
    pop $0
    ${If} $0 == "not found"
        Call InstallDotNetFx
        pop $DOTNETFX_RETURN_CODE
        ${If} $DOTNETFX_RETURN_CODE <> 0
            MessageBox MB_OK|MB_ICONSTOP $ERROR_DOTNET_FATAL
            Abort
        ${EndIf}
       
        Call InstallDotNetLang
        pop $DOTNETLang_RETURN_CODE
        ${If} $DOTNETLang_RETURN_CODE <> 0
            MessageBox MB_OK|MB_ICONINFORMATION $INSTALL_SUCCESS
            Abort
        ${EndIf}
    ${EndIf}

    StrCpy $0 $0 "" 1 # skip "v"

    ${VersionCompare} $0 "2.0" $1
    ${If} $1 == 2
        Call InstallDotNetFx
        pop $DOTNETFX_RETURN_CODE
        ${If} $DOTNETFX_RETURN_CODE <> 0
            MessageBox MB_OK|MB_ICONSTOP $ERROR_DOTNET_FATAL
            Abort
        ${EndIf}

        Call InstallDotNetLang
        pop $DOTNETLang_RETURN_CODE
        ${If} $DOTNETLang_RETURN_CODE <> 0
            MessageBox MB_OK|MB_ICONINFORMATION $INSTALL_SUCCESS
            Abort
        ${EndIf}
    ${EndIf}
FunctionEnd
;获取.NET版本,此函数在nsis.sf.net中有
Function GetDotNETVersion
    Push $0
    Push $1

    System::Call "mscoree::GetCORVersion(w .r0, i ${NSIS_MAX_STRLEN}, *i) i .r1"
    StrCmp $1 "error" 0 +2
    StrCpy $0 "not found"

    Pop $1
    Exch $0
FunctionEnd
;安装.NET框架
Function InstallDotNetFx
    push $DOTNETFX_RETURN_CODE
    StrCpy $DOTNETFX_RETURN_CODE "0"
    SetOutPath "$PLUGINSDIR"
    File /r "dotnetfx.exe"
    Banner::show /NOUNLOAD "正在安装.NET运行库..."
    nsExec::ExecToStack '"dotnetfx.exe" /q /c:"install.exe /noaspupgrade /q"'
    ;pop $DOTNETFX_RETURN_CODE
    Banner::destroy
FunctionEnd
;安装语言包
Function InstallDotNetLang
    push $DOTNETLang_RETURN_CODE
    StrCpy $DOTNETLang_RETURN_CODE "0"
    SetOutPath "$PLUGINSDIR"
    File /r "langpack.exe"
    Banner::show /NOUNLOAD "正在安装.NET运行库中文语言包..."
    nsExec::ExecToStack '"langpack.exe" /q /c:"install.exe /noaspupgrade /q"'
    ;pop $DOTNETLang_RETURN_CODE
    Banner::destroy
FunctionEnd

Section
SectionEnd

你可能感兴趣的:(安装)