NSIS学习笔记三-中文测试

来源:互联网 发布:python进入命令行模式 编辑:程序博客网 时间:2024/05/19 16:22

Title:  NSIS学习笔记三-中文测试
Author: Kagula
Date: 2016-08-30
Environment: NSIS 3.0(Released  July 24, 2016)

Introduction
  Simple install program code demonstrate.  Using default MUI2 Style GUI.
  I think NSIS 3.0 is the first version that support unicode(chinese).
 
Feature List
 [1]支持检查安装程序是否在运行。
 [2]支持检查主程序是否在运行。
 [3]支持版本检测。
 [4]支持升级安装。
 [5]支持用户修改安装路径。
 [6]支持中文。
 [7]支持从控制面板卸载程序。

 

Prepare:

    suppose source directory name is issue and exist in the same directory as nis script.

 

Manifest:

!include "MUI2.nsh" !include "FileFunc.nsh"!include "LogicLib.nsh"  !include "nsProcess.nsh" Unicode true!define APPNAME     "XXX软件客户端"!define COMPANYNAME "XXX有限公司"!define DESCRIPTION  ""  !define APPEXENAME  "XXX.exe"!define VERSIONMAJOR    1!define VERSIONMINOR    0!define VERSIONBUILD    0  !define VERSIONREVISION 0!define MYINSTALLDIR      "organizationName"!define MYINSTALLSUBDIR   "productName"  # This is the size (in kB) of all the files copied into "Program Files"  !define INSTALLSIZE 20000!define HELPURL   "http://www.kagula.com/" # "Support Information" link  !define UPDATEURL "http://www.kagula.com/" # "Product Updates" link  !define ABOUTURL  "http://www.kagula.com/" # "Publisher" link  ;--------------------------------------------------------------------!define VERSIONLONG  "${VERSIONMAJOR}.${VERSIONMINOR}.${VERSIONBUILD}.${VERSIONREVISION}"  !define SETUPFILENAME "Setup${VERSIONLONG}.exe"OutFile "${SETUPFILENAME}"Name "${APPNAME}"InstallDir "$PROGRAMFILES\${MYINSTALLDIR}\${MYINSTALLSUBDIR}"ShowInstDetails showRequestExecutionLevel adminXPStyle onCompletedText "完成安装"!define      MUI_PAGE_CUSTOMFUNCTION_SHOW MyFuncDirFunction MyFuncDir  #不知道为什么这里不能引用全局变量  ReadRegStr $0 HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "MainProgramLocation"    ${If} $0 != "";disable browser button.FindWindow $0 "#32770" "" $HWNDPARENTGetDlgItem $0 $0 1001EnableWindow $0 0;disable edit install path.FindWindow $0 "#32770" "" $HWNDPARENTGetDlgItem $0 $0 1019EnableWindow $0 0  ${EndIf}    FunctionEnd!insertmacro MUI_PAGE_DIRECTORY!insertmacro MUI_PAGE_INSTFILES!insertmacro MUI_LANGUAGE "SimpChinese";--------------------------------------------------------------------Var VersionNumber   Function VerCheck  pop $0  ;${GetFileVersion} "$INSTDIR\${APPEXENAME}" $VersionNumber  ${GetFileVersion} "$0" $VersionNumber  FunctionEndFunction VersionCompare    !define VersionCompare `!insertmacro VersionCompareCall`         !macro VersionCompareCall _VER1 _VER2 _RESULT          Push `${_VER1}`          Push `${_VER2}`          Call VersionCompare          Pop ${_RESULT}      !macroend         Exch $1      Exch      Exch $0      Exch      Push $2      Push $3      Push $4      Push $5      Push $6      Push $7         begin:      StrCpy $2 -1      IntOp $2 $2 + 1      StrCpy $3 $0 1 $2      StrCmp $3 '' +2      StrCmp $3 '.' 0 -3      StrCpy $4 $0 $2      IntOp $2 $2 + 1      StrCpy $0 $0 '' $2         StrCpy $2 -1      IntOp $2 $2 + 1      StrCpy $3 $1 1 $2      StrCmp $3 '' +2      StrCmp $3 '.' 0 -3      StrCpy $5 $1 $2      IntOp $2 $2 + 1      StrCpy $1 $1 '' $2         StrCmp $4$5 '' equal         StrCpy $6 -1      IntOp $6 $6 + 1      StrCpy $3 $4 1 $6      StrCmp $3 '0' -2      StrCmp $3 '' 0 +2      StrCpy $4 0         StrCpy $7 -1      IntOp $7 $7 + 1      StrCpy $3 $5 1 $7      StrCmp $3 '0' -2      StrCmp $3 '' 0 +2      StrCpy $5 0         StrCmp $4 0 0 +2      StrCmp $5 0 begin newer2      StrCmp $5 0 newer1      IntCmp $6 $7 0 newer1 newer2         StrCpy $4 '1$4'      StrCpy $5 '1$5'      IntCmp $4 $5 begin newer2 newer1         equal:      StrCpy $0 0      goto end      newer1:      StrCpy $0 1      goto end      newer2:      StrCpy $0 2         end:      Pop $7      Pop $6      Pop $5      Pop $4      Pop $3      Pop $2      Pop $1      Exch $0  FunctionEnd  Var IsSkipCustomFunction .onInit  #Prevent reenter.System::Call 'kernel32::CreateMutex(p 0, i 0, t "mutexEscortCashbox") p .r1 ?e'Pop $R0 StrCmp $R0 0 +3MessageBox MB_OK|MB_ICONEXCLAMATION "安装程序已经运行了..."Abort#Prevent can not overwrite event happen.${nsProcess::FindProcess} ${APPEXENAME} $R0      ${If} $R0 == "0"            # it's running            MessageBox MB_OK "你的程序无法自动关闭,请先关闭${APPNAME}程序,再启动安装卸载程序!"            Quit      ${EndIf}    #MessageBox MB_OK "当前软件的版本为:${VERSIONLONG}"    #check version from custom install place        ReadRegStr $0 HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "MainProgramLocation"    ${If} $0 != ""        MessageBox MB_OK "文件位置:$0"        ${If} ${FileExists} $0  ;        #if installed version is greater, return 2          ;        #else if equality, return 0.  ;        #else if less, return 1 and resume install process.  ;        #MessageBox MB_OK "Version=$R0"        ;       MessageBox MB_OK "before push the parameter for VerCheck function.$0"         push $0         Call VerCheck         Strcpy $IsSkipCustom 0       ${VersionCompare} $VersionNumber ${VERSIONLONG} $R0                ${if} $R0 == "1"             MessageBox MB_OK "已安装较新版本的软件,请卸载后再重新安装!"           Quit          ${Endif}              ${if} $R0 == "0"                      MessageBox MB_OK "你已经安装相同版本的${APPNAME}软件,请卸载后再重新安装!"               Quit       ${Endif}              ${if} $R0 == "2"              MessageBox MB_OKCANCEL "你已经安装老版本的${APPNAME}软件,确定要升级安装吗?" IDOK  True IDCANCEL False            True:              Strcpy $IsSkipCustom 1                             DetailPrint "开始升级安装..."              Goto Next            False:              Quit            Next:        ${Endif}      ${EndIf}      ${EndIf}        ${if} $IsSkipCustom == "1"      ReadRegStr $0 HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "MainProgramLocationPath"        ${If} $0 != ""        Strcpy $INSTDIR $0      ${EndIf}    ${EndIf}       FunctionEnd;--------------------------------------------------------------------SectionSetOutPath $INSTDIRFile /r "issue\"WriteUninstaller $INSTDIR\uninstall.exeSetShellVarContext allCreateDirectory "$SMPROGRAMS\${APPNAME}"CreateShortCut  "$SMPROGRAMS\${APPNAME}\卸载.lnk" "$INSTDIR\uninstall.exe"CreateShortcut "$DESKTOP\${APPNAME}.lnk" "$INSTDIR\${APPEXENAME}"     # Registry information for add/remove programs      WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "DisplayName" ${APPNAME}      WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "UninstallString" "$\"$INSTDIR\uninstall.exe$\""      WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "QuietUninstallString" "$\"$INSTDIR\uninstall.exe$\" /S"      WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "InstallLocation" "$\"$INSTDIR$\""      #WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "DisplayIcon" "$\"$INSTDIR\${ICOFILENAME}$\""      WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "Publisher" ${COMPANYNAME}      WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "HelpLink"       ${HELPURL}    WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "URLUpdateInfo"  ${UPDATEURL}    WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "URLInfoAbout"   ${ABOUTURL}    WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "DisplayVersion" ${VERSIONLONG}      WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "VersionMajor" ${VERSIONMAJOR}      WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "VersionMinor" ${VERSIONMINOR}            # There is no option for modifying or repairing the install      WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "NoModify" 1      WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "NoRepair" 1            # Set the INSTALLSIZE constant (!defined at the top of this script) so Add/Remove Programs can accurately report the size      WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "EstimatedSize" ${INSTALLSIZE}            #Write file install location to register table.      WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "MainProgramLocation" "$INSTDIR\${APPEXENAME}"      WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "MainProgramLocationPath" "$INSTDIR"        CreateShortcut "$DESKTOP\${APPNAME}.lnk" "$INSTDIR\${APPEXENAME}"SectionEndSection "Uninstall"    # Remove Desktop launcher      SetShellVarContext all  delete "$DESKTOP\${APPNAME}.lnk"  delete "$SMPROGRAMS\${APPNAME}\${APPNAME}.lnk"  delete "$SMPROGRAMS\${APPNAME}\卸载.lnk"    # Try to remove the Start Menu folder - this will only happen if it is empty  rmDir "$SMPROGRAMS\${APPNAME}"  # Always delete uninstaller as the last action  delete $INSTDIR\uninstall.exermDir /r $INSTDIR# Remove uninstaller information from the registry    DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}"      # For show uninstall finish page at last.  # SetAutoClose trueSectionEnd


Remark

    一些老的NSI Editor tool 新建的文件对中文支持有问题,引起乱码,建议使用notepad或在example->unicode.nsi基础上修改代码。


Reference
[1]unicode.nsi、languages.nsi in Install Example Directory.
[2]Whether is 32bits OS or 64bits OS?
http://stackoverflow.com/questions/10448116/change-the-default-install-folder-in-nsis
[3]《NSIS进阶教程(一)~(五)》
http://blog.csdn.net/taiyang1987912/article/details/40506439

0 0
原创粉丝点击