声明:博客内所有技术性文章都是由作者本人工作经验的总结,  实现过程中有搜索资料为参考的绝不涉嫌抄袭,均为个人习惯的总结。
今天说的是公司日常使用NSIS打包客户端的一个完整的例子,,,使用NSIS v2.45集成增强版来完成打包的。包括 多语言 安装 卸载 检测.net 检测 注册dll 等等等等,算是一个比较完全的脚本了,,,以下为脚本内容,和一些注释说明,只要新建一个空白脚本就可以使用。
; 该脚本使用 HM VNISEdit 脚本编辑器向导产生

; 安装程序初始定义常量
!define PRODUCT_NAME "GloTrader"
;以下是用/file参数从某个文件中读取全部文件内容赋予PRODUCT_VERSION,这样所有的打包程序共用一个版本号
!define /file PRODUCT_VERSION "D:\test.ini"
!define PRODUCT_PUBLISHER "RMMSoft"
!define PRODUCT_WEB_SITE "http://www.rmmsoft.com.cn"
!define PRODUCT_DIR_REGKEY "Software\Microsoft\Windows\CurrentVersion\App Paths\GloTrader.exe"
!define PRODUCT_UNINST_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}"
!define PRODUCT_UNINST_ROOT_KEY "HKLM"

SetCompressor /SOLID lzma
SetCompressorDictSize 32

; ------ MUI 现代界面定义 (1.67 版本以上兼容) ------
!include "MUI.nsh"

; MUI 预定义常量
!define MUI_ABORTWARNING
!define MUI_ICON "${NSISDIR}\Contrib\Graphics\Icons\orange-install.ico"
!define MUI_UNICON "${NSISDIR}\Contrib\Graphics\Icons\orange-uninstall.ico"
!define MUI_CUSTOMFUNCTION_GUIINIT onGUIInit

; 语言选择窗口常量设置
!define MUI_LANGDLL_REGISTRY_ROOT "${PRODUCT_UNINST_ROOT_KEY}"
!define MUI_LANGDLL_REGISTRY_KEY "${PRODUCT_UNINST_KEY}"
!define MUI_LANGDLL_REGISTRY_VALUENAME "NSIS:Language"

; 欢迎页面
!insertmacro MUI_PAGE_WELCOME
; 许可协议页面
!define MUI_LICENSEPAGE_CHECKBOX
;!insertmacro MUI_PAGE_LICENSE "D:\Build Install\Client\GloTrader\src\Eula.English.rtf"
LicenseLangString License 1033 "D:\Build Install\Client\GloTrader\src\Eula.English.rtf"
LicenseLangString License 2052 "D:\Build Install\Client\GloTrader\src\Eula.Chinese.rtf"
!insertmacro MUI_PAGE_LICENSE $(License)

; 安装目录选择页面
!insertmacro MUI_PAGE_DIRECTORY
; 安装过程页面
!insertmacro MUI_PAGE_INSTFILES
; 安装完成页面
!define MUI_FINISHPAGE_RUN "$INSTDIR\GloTrader.exe"
!insertmacro MUI_PAGE_FINISH

; 安装卸载过程页面
!insertmacro MUI_UNPAGE_INSTFILES

; 安装界面包含的语言设置
!insertmacro MUI_LANGUAGE "English"
!insertmacro MUI_LANGUAGE "SimpChinese"

; 安装预释放文件
!insertmacro MUI_RESERVEFILE_LANGDLL
!insertmacro MUI_RESERVEFILE_INSTALLOPTIONS
; ------ MUI 现代界面定义结束 ------



Name "${PRODUCT_NAME} ${PRODUCT_VERSION}"
OutFile "GloTrader_${__DATE__}.exe"
InstallDir "$PROGRAMFILES\RMMSoft\GloTrader"
InstallDirRegKey HKLM "${PRODUCT_UNINST_KEY}" "UninstallString"
ShowInstDetails show
ShowUnInstDetails show
BrandingText "RMMSoft www.rmmsoft.com.cn"

Section "MainSection" SEC01
 ;RMDir /r "$INSTDIR\config"

 SetOutPath "$INSTDIR\config"
 SetOverwrite on
 File "D:\Build Install\Client\GloTrader\src\config\*.*"

   SetOutPath "$INSTDIR\en-US"
 SetOverwrite on
 File "D:\Build Install\Client\GloTrader\src\en-US\*.*"

 SetOutPath "$INSTDIR\sounds"
 SetOverwrite on
 File "D:\Build Install\Client\GloTrader\src\sounds\*.*"

   ;SetOutPath "$INSTDIR\Resources"
 ;SetOverwrite on
 ;File "D:\Build Install\Client\GloTrader\src\Resources\*.*"

 ;SetOutPath "$INSTDIR\x64"
 ;SetOverwrite on
 ;File "D:\Build Install\Client\GloTrader\src\x64\*.*"

 ;SetOutPath "$INSTDIR\x86"
 ;SetOverwrite on
 ;File "D:\Build Install\Client\GloTrader\src\x86\*.*"

   SetOutPath "$INSTDIR"
 SetOverwrite on
 File "D:\Build Install\Client\GloTrader\src\*.*"

####---注册现有的dll---####
 RegDLL "$INSTDIR\StockChartX.ocx"
SectionEnd

Section -AdditionalIcons
 CreateDirectory "$SMPROGRAMS\RMMSoft\GloTrader"
 CreateShortCut "$SMPROGRAMS\RMMSoft\GloTrader\GloTrader.lnk" "$INSTDIR\GloTrader.exe"
 CreateShortCut "$DESKTOP\GloTrader.lnk" "$INSTDIR\GloTrader.exe"
 CreateShortCut "$SMPROGRAMS\RMMSoft\GloTrader\Uninstall.lnk" "$INSTDIR\uninst.exe"
SectionEnd

Section -Post
 WriteUninstaller "$INSTDIR\uninst.exe"
 WriteRegStr HKLM "${PRODUCT_DIR_REGKEY}" "" "$INSTDIR\GloTrader.exe"
 WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayName" "$(^Name)"
 WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "UninstallString" "$INSTDIR\uninst.exe"
 WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayIcon" "$INSTDIR\GloTrader.exe"
 WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayVersion" "${PRODUCT_VERSION}"
 WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "URLInfoAbout" "${PRODUCT_WEB_SITE}"
 WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "Publisher" "${PRODUCT_PUBLISHER}"
SectionEnd

#-- 根据 NSIS 脚本编辑规则,所有 Function 区段必须放置在 Section 区段之后编写,以避免安装程序出现未可预知的问题。--#
  LangString Message0 ${Lang_ENGLISH} "The previous version of the GloTrader has been detected and has to be removed before installing the new version. Are you sure to remove the previous version along with all components from your computer?"
    LangString Message0 ${Lang_SIMPCHINESE} "安装新版本以前,需要先卸载以前的版本!您确实要完全移除 GloTrader ,及其所有的组件?"

    LangString Message5 ${Lang_ENGLISH} "The $(^Name) installation wizard is already running!"
    LangString Message5 ${Lang_SIMPCHINESE} "$(^Name) 安装向导已经运行!"

    LangString Message6 ${Lang_ENGLISH} "Current .Net Framework found incompatible, please click OK and continue to download the correct version"
    LangString Message6 ${Lang_SIMPCHINESE} "安装程序检测到本机的.Net Framework组件不兼容, 点击确定开始下载"
Function .onInit

 ;创建互斥防止重复运行
 !addplugindir "${NSISDIR}\Plugins"
 InitPluginsDir
 ;获得此程序自身名称
 System::Call 'kernel32::CreateMutexA(i 0, i 0, t "GloTrader") i .r1 ?e'
 Pop $R0
 StrCmp $R0 0 +3
   MessageBox MB_OK|MB_ICONEXCLAMATION "$(Message5)"
   Abort

   ;判断操作系统是否安装.Net Framework 3.5 SP1  XP = "3.5.30729.01" WIN7 = "3.5.30729.4926" .Net Framework 4.0 = 4.0.30319
    ReadRegStr $0 HKLM "Software\Microsoft\NET Framework Setup\NDP\v4\Full" "Version"
    StrCpy $1 $0 9
   ${If} $1 != "4.0.30319"
       MessageBox MB_ICONSTOP|MB_OKCANCEL "$(Message6)" IDOK NoAbort
       Abort
       NoAbort:
       ExecShell open "www.microsoft.com/en-us/download/confirmation.aspx?id=17718"
       Quit
   ${Endif}

   ####安装时检测程序是否在运行,并且提示
   /*The return codes are as follows:
   0 = Process was not found
   1 = Process was found
   605 = Unable to search for process
   606 = Unable to identify system type
   607 = Unsupported OS
   632 = Process name is invalid*/
 !addplugindir "${NSISDIR}\Plugins"
  FindProcDLL::FindProc "GloTrader.exe"
   ;执行FindProcdll后返回的值自动赋予下面的$R0,然后和1对比,等于1则从+1个命令开始执行反之跳到+4命令执行
    StrCmp $R0 1 +1 +4
    MessageBox MB_ICONINFORMATION|MB_RETRYCANCEL "$(Message3)"   IDRetry retry IDCANCEL Cancel
    Retry:
     Goto -3
    Cancel:
     Quit

        ReadRegStr $0 HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Full" "Version"
 ;判断操作系统是否安装本程序
        ;如果这个文件存在则继续往下执行代码,反之则从第四个命令执行(注意不是第四行,也就是跳出这个if语句)
 IfFileExists "$INSTDIR\uninst.exe" 0 +4
     MessageBox MB_ICONQUESTION|MB_YESNO "$(Message0)" IDYES +2
     Abort
     ExecWait '"$INSTDIR\uninst.exe" /S _?=$INSTDIR'

 !insertmacro MUI_LANGDLL_DISPLAY

FunctionEnd

Function onGUIInit
  BrandingURL::Set /NOUNLOAD "200" "0" "0" "http://www.rmmsoft.com.cn"
FunctionEnd

Function .onGUIEnd
BrandingURL::Unload
FunctionEnd

/******************************
*  以下是安装程序的卸载部分  *
******************************/
 LangString Message1 ${Lang_ENGLISH} "Retain personal perferences?"
   LangString Message1 ${Lang_SIMPCHINESE} "是否保留设置文件和历史记录?"
Section Uninstall
   ;下面是删除nsis自动创建的一些文件
 Delete "$INSTDIR\${PRODUCT_NAME}.url"
 Delete "$SMPROGRAMS\RMMSoft\GloTrader\Uninstall.lnk"
 Delete "$DESKTOP\GloTrader.lnk"
 Delete "$SMPROGRAMS\RMMSoft\GloTrader\GloTrader.lnk"
 RMDir "$SMPROGRAMS\RMMSoft\GloTrader"
 RMDir /r "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\RMMSoft\GloTrader"
 DeleteRegKey ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}"
 DeleteRegKey HKLM "${PRODUCT_DIR_REGKEY}"
 ;下面是删除程序安装目录下的所有文件,不包括子目录
 Delete "$INSTDIR\*"
 ;删除判断后的文件
   RMDir /r "$INSTDIR\en-US"
   RMDir /r "$INSTDIR\Resources"
   RMDir /r "$INSTDIR\x86"
   RMDir /r "$INSTDIR\x64"
 MessageBox MB_YESNO "$(Message1)" IDYES FOR_YES IDNO FOR_NO
 FOR_YES:
 Goto NEXT
 FOR_NO:
 RMDir /r "$INSTDIR\config"
 RMDir /r "$INSTDIR"
 NEXT:
 SetAutoClose true
SectionEnd

#-- 根据 NSIS 脚本编辑规则,所有 Function 区段必须放置在 Section 区段之后编写,以避免安装程序出现未可预知的问题。--#

 LangString Message2 ${Lang_ENGLISH} "Are you sure to remove $(^Name) with all components from your computer?"
   LangString Message2 ${Lang_SIMPCHINESE} "卸载开始!您确实要完全移除 $(^Name) ,及其所有的组件?"

   LangString Message3 ${Lang_ENGLISH} "Application detected running, please close before continue.  Click CANCEL to exit."
   LangString Message3 ${Lang_SIMPCHINESE} "检测到 $(^Name) 正在运行!请退出程序重试或取消卸载!"

   LangString Message4 ${Lang_ENGLISH} "$(^Name) has uninstalled completely!"
   LangString Message4 ${Lang_SIMPCHINESE} "$(^Name) 卸载完毕!"
Function un.onInit
!insertmacro MUI_UNGETLANGUAGE
 MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 "$(Message2)" IDYES +2
 Abort

####卸载时检测程序是否在运行,并且提示
   /*The return codes are as follows:
   0 = Process was not found
   1 = Process was found
   605 = Unable to search for process
   606 = Unable to identify system type
   607 = Unsupported OS
   632 = Process name is invalid*/
 !addplugindir "${NSISDIR}\Plugins"
  FindProcDLL::FindProc "GloTrader.exe"
   ;执行FindProcdll后返回的值自动赋予下面的$R0,然后和1对比,等于1则从+1个命令开始执行反之跳到+4命令执行
    StrCmp $R0 1 +1 +4
    MessageBox MB_ICONINFORMATION|MB_RETRYCANCEL "$(Message3)"   IDRetry retry IDCANCEL Cancel
    Retry:
     Goto -3
    Cancel:
     Quit
FunctionEnd

Function un.onUninstSuccess
 HideWindow
 MessageBox MB_ICONINFORMATION|MB_OK "$(Message4)"
FunctionEnd
结束,,,恭喜你成功了        QQ 交流 群 221046788