只作代码记录,不作详细说明
32和64系统的权限和安装路径等有些区别,此篇文章仅对32系统有用
从接触NSIS到写完这个脚本只花了2、3天时间,所以其中肯定还有很多冗余或不够合理的地方,但还是实现了我想要的功能。
; 该脚本使用 HM VNISEdit 脚本编辑器向导产生
; 安装程序初始定义常量
RequestExecutionLevel admin
;获取管理员权限,貌似对win7等需要权限的系统有用
!define PRODUCT_NAME "xxx"
;定义产品的安装名
!define PRODUCT_VERSION "1.0"
!define PRODUCT_DIR_REGKEY "Software\Microsoft\Windows\CurrentVersion\App Paths\xxx.exe"
;定义注册表路径,此条语句会将你需要运行的程序写到注册表的这个路径下
!define PRODUCT_UNINST_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}"
;同上也是对注册表的操作
!define PRODUCT_UNINST_ROOT_KEY "HKLM"
!define PRODUCT_STARTMENU_REGVAL "NSIS:StartMenuDir"
SetCompressor /SOLID lzma
;选择压缩模式
SetCompressorDictSize 32
; ------ MUI 现代界面定义 (1.67 版本以上兼容) ------
!include "MUI.nsh"
; MUI 预定义常量
!define MUI_ABORTWARNING
!define MUI_ICON "E:\xxx\logo.ico"
;安装包logo图标路径
!define MUI_UNICON "${NSISDIR}\Contrib\Graphics\Icons\modern-uninstall.ico"
;安装包的卸载
logo图标路径
; 语言选择窗口常量设置
!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
; 安装目录选择页面
!insertmacro MUI_PAGE_DIRECTORY
; 开始菜单设置页面
; -------------------定义语言字符串变量-----------------------------
LangString LANG_PRODUCT_FLODER 1033 "xxx"
;安装界面为英文显示
LangString LANG_PRODUCT_FLODER 2052 "xxx"
;安装界面为简中
显示
LangString LANG_PRODUCT_FLODER 1028 "xxx"
;安装界面为繁中显示
LangString LANG_PRODUCT_NAME 1033 "xxx"
;安装界面为英文显示
LangString LANG_PRODUCT_NAME 2052 "xxx"
;安装界面为简中显示
LangString LANG_PRODUCT_NAME 1028 "xxx"
;安装界面为繁中显示
; ------------------------------------------------------------------
var ICONS_GROUP
!define MUI_STARTMENUPAGE_NODISABLE
!define MUI_STARTMENUPAGE_DEFAULTFOLDER "$(LANG_PRODUCT_FLODER)\$(LANG_PRODUCT_NAME)\xxx"
!define MUI_STARTMENUPAGE_REGISTRY_ROOT "${PRODUCT_UNINST_ROOT_KEY}"
!define MUI_STARTMENUPAGE_REGISTRY_KEY "${PRODUCT_UNINST_KEY}"
!define MUI_STARTMENUPAGE_REGISTRY_VALUENAME "${PRODUCT_STARTMENU_REGVAL}"
!insertmacro MUI_PAGE_STARTMENU Application $ICONS_GROUP
; 安装过程页面
!insertmacro MUI_PAGE_INSTFILES
; 安装完成页面
!define MUI_FINISHPAGE_RUN "$INSTDIR\xxx.exe"
!insertmacro MUI_PAGE_FINISH
; 安装卸载过程页面
!insertmacro MUI_UNPAGE_INSTFILES
; 安装界面包含的语言设置
!insertmacro MUI_LANGUAGE "English"
!insertmacro MUI_LANGUAGE "SimpChinese"
!insertmacro MUI_LANGUAGE "TradChinese"
; 安装预释放文件
!insertmacro MUI_RESERVEFILE_LANGDLL
!insertmacro MUI_RESERVEFILE_INSTALLOPTIONS
; ------ MUI 现代界面定义结束 ------
Name "${PRODUCT_NAME} ${PRODUCT_VERSION}"
OutFile "x86Setup.exe"
InstallDir "$PROGRAMFILES\xxx\xxx\xxx"
;安装路径,$PROGRAMFILES默认为c:\windows\system32\drivers
InstallDirRegKey HKLM "${PRODUCT_UNINST_KEY}" "UninstallString"
ShowInstDetails show
ShowUnInstDetails show
Section "MainSection" SEC01
Call .inFirefox
SetOutPath "$INSTDIR"
SetOverwrite ifnewer
File "E:\xxx\xxx.exe"
SetOutPath "$SYSDIR"
File "E:\xxx\xxx"
File "E:\xxx\xxx"
File "E:\xxx\xxx"
ExecWait "$SYSDIR\xxx"
; --------------------------------------------------------
; 创建开始菜单快捷方式
!insertmacro MUI_STARTMENU_WRITE_BEGIN Application
CreateDirectory "$SMPROGRAMS\$ICONS_GROUP"
CreateShortCut "$SMPROGRAMS\$ICONS_GROUP\xxx.lnk" "$INSTDIR\xxx.exe"
CreateShortCut "$DESKTOP\xxx.lnk" "$INSTDIR\xxx.exe"
!insertmacro MUI_STARTMENU_WRITE_END
SectionEnd
Section -AdditionalIcons
!insertmacro MUI_STARTMENU_WRITE_BEGIN Application
CreateShortCut "$SMPROGRAMS\$ICONS_GROUP\Uninstall.lnk" "$INSTDIR\uninst.exe"
!insertmacro MUI_STARTMENU_WRITE_END
SectionEnd
Section -Post
WriteUninstaller "$INSTDIR\uninst.exe"
;创建自启动
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Run" "xxx" "$INSTDIR\xxx.exe"
;写注册表让你的程序开机自启动
WriteRegStr HKLM "${PRODUCT_DIR_REGKEY}" "" "$INSTDIR\xxx.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\xxx.exe"
WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayVersion" "${PRODUCT_VERSION}"
SectionEnd
#-- 根据 NSIS 脚本编辑规则,所有 Function 区段必须放置在 Section 区段之后编写,以避免安装程序出现未可预知的问题。--#
; ------判断是否安装Firefox代码如下:-------------------
Var Firefox
Function .inFirefox
ClearErrors
ReadRegStr $Firefox HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\firefox.exe" "Path"
;此段代码通过读你机器上注册表里是否有firefox程序来判断有没有安装firefox
IfErrors 0 ExitCheckFirefox
Goto NoFirefox
ExitCheckFirefox:
SetOutPath "$Firefox\plugins"
;如果有firefox需将你要装的插件装到firefox的plugins的文件夹下,否则跳过这段代码
SetOverwrite ifnewer
File "E:\xxx\xxx"
NoFirefox:
FunctionEnd
/******************************
* 以下是安装程序的卸载部分 *
******************************/
#-- 根据 NSIS 脚本编辑规则,所有 Function 区段必须放置在 Section 区段之后编写,以避免安装程序出现未可预知的问题。--#
;卸载菜单语言设置
; -------------------设置多语言字符串显示-----------------------------
LangString UNINSTALL_MESSAGE 1033 "Are you sure you want to completely remove [xxx] and all of its components?"
LangString UNINSTALL_MESSAGE 2052 "你确实要完全移除 [xxx] ,及其所有的组件?"
LangString UNINSTALL_MESSAGE 1028 "妳確實要完全移除 [xxx] ,及其所有的組件?"
LangString UNINSTALL_MESSAGE_OK 1033 "[xxx] was successfully removed from your computer."
LangString UNINSTALL_MESSAGE_OK 2052 "[xxx] 已成功地从你的计算机移除。"
LangString UNINSTALL_MESSAGE_OK 1028 "[xxx] 已成功地從妳的計算機移除。"
; --------------------------------------------------------------------
Section Uninstall
Call un.Firefox
!insertmacro MUI_STARTMENU_GETFOLDER "Application" $ICONS_GROUP
KillProcDLL::KillProc "xxx.exe"
;卸载时如果程序正在运行,利用插件终止进程
Delete "$INSTDIR\uninst.exe"
Delete /rebootok "$INSTDIR\xxx.exe"
;卸载时删不掉程序,加上这条语句可以重启后进行删除
Delete "$SYSDIR\xxx"
Delete "$SYSDIR\xxx"
Delete "$SYSDIR\xxx"
Delete /rebootok "$SYSDIR\xxx"
Delete "$SYSDIR\xxx"
Delete "$SYSDIR\xxx"
Delete "$SYSDIR\xxx"
Delete "$SYSDIR\xxx"
Delete "$SYSDIR\xxx"
;为了卸载干净,之前装了多少东西,这里就用多少删除语句干掉它们,卸不掉的就用rebootok卸载
Delete "$SMPROGRAMS\$ICONS_GROUP\Uninstall.lnk"
Delete "$DESKTOP\xxx.lnk"
Delete "$SMPROGRAMS\$ICONS_GROUP\xxx.lnk"
RMDir "$SMPROGRAMS\$ICONS_GROUP"
RMDir "$SMPROGRAMS\$(LANG_PRODUCT_FLODER)\$(LANG_PRODUCT_NAME)"
RMDir "$SMPROGRAMS\$(LANG_PRODUCT_FLODER)"
RMDir /rebootok "$INSTDIR"
;卸载时删除一些文件夹
RMDir /rebootok "$PROGRAMFILES\xxx\xxx"
;卸载时删除一些文件夹
RMDir /rebootok "$PROGRAMFILES\xxx"
;卸载时删除一些文件夹
;清理注册表
DeleteRegKey HKCU "Software\Microsoft\SystemCertificates\My\PhysicalStores"
DeleteRegKey HKLM "SOFTWARE\Microsoft\Cryptography\Defaults\Provider\xxx"
DeleteRegValue HKLM "Software\Microsoft\Windows\CurrentVersion\Run" "xxx"
DeleteRegKey ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}"
DeleteRegKey HKLM "${PRODUCT_DIR_REGKEY}"
SetAutoClose true
SectionEnd
Function un.Firefox
; ------判断是否安装Firefox并删除DLL文件-------------------
ClearErrors
ReadRegStr $0 HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\firefox.exe" "Path"
IfErrors 0 unExitCheckFirefox
Goto unNoFirefox
unExitCheckFirefox:
Delete "$0\plugins\xxx"
RMDIR "$0\plugins"
unNoFirefox:
FunctionEnd
; ----------------------------------------------------------
Function un.onInit
!insertmacro MUI_UNGETLANGUAGE
MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 "$(UNINSTALL_MESSAGE)" IDYES +2
Abort
FunctionEnd
Function un.onUninstSuccess
HideWindow
MessageBox MB_ICONINFORMATION|MB_OK "$(UNINSTALL_MESSAGE_OK)"
FunctionEnd