NSIS製作32位安裝包
阿新 • • 發佈:2019-01-09
只作程式碼記錄,不作詳細說明
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" ;安裝包
; 語言選擇視窗常量設定
!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