文中用到了一个NSIS查找进程是否存在的插件FindProc.dll, 请移步下面地址下载
链接: https://pan.baidu.com/s/1efp8Azs5b8sEtQUTyhywMw 提取码: nbt3
;XXXNSIS安装脚本
;--------------------------------
!include "MUI2.nsh"
;--------------------------------
;General
!define APPNAME "XXX"
!define COMPANYNAME "xxx信息技术有限公司"
!define DESCRIPTION "XXX 1.0"
!define PROCESS_NAME "PROGRESSNAME"
!define MAIN_EXE_NAME "PROGRESSNAME"
!define ABOUTURL "http://xxxxxx.com//"
# These three must be integers
!define VERSIONMAJOR 1
!define VERSIONMINOR 0
!define VERSIONBUILD 0
!define INSTALLSIZE 415*1024
;Name and file
Name "${APPNAME}"
OutFile "XXX安装包.exe"
Var DefaultInstallDir
;Default installation folder
InstallDir "$PROGRAMFILES\${APPNAME}"
;Request application privileges for Windows Vista
RequestExecutionLevel admin
;--------------------------------
;Interface Settings
!define MUI_ABORTWARNING
!define MUI_ICON "PROGRESSNAMEDG\icon\project.ico"
!define MUI_UNICON "PROGRESSNAMEDG\icon\project-uninstall.ico"
;--------------------------------
;Pages
!insertmacro MUI_PAGE_WELCOME
!define MUI_PAGE_CUSTOMFUNCTION_SHOW BEFORE_DIRECTORY
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH
!insertmacro MUI_UNPAGE_WELCOME
!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES
!insertmacro MUI_UNPAGE_FINISH
;--------------------------------
;Languages
!insertmacro MUI_LANGUAGE "SimpChinese"
;开始安装时检查是否正在运行
;${PROCESS_NAME}为自定义的常量
Function .onInit
FindProcDLL::FindProc "${PROCESS_NAME}.exe"
Pop $R0
IntCmp $R0 1 0 no_run
MessageBox MB_ICONSTOP "安装程序检测到 ${APPNAME} 正在运行,请退出程序后重试"
Quit
no_run:
ReadRegStr $DefaultInstallDir HKLM "Software\${APPNAME}" "InstallLocation"
StrLen $0 $DefaultInstallDir
${If} $0 > 0
StrCpy $INSTDIR $DefaultInstallDir
${EndIf}
FunctionEnd
;开始卸载时检查:
Function un.onInit
FindProcDLL::FindProc "${PROCESS_NAME}.exe"
Pop $R0
IntCmp $R0 1 0 no_run
MessageBox MB_ICONSTOP "安装程序检测到 ${APPNAME} 正在运行,请退出程序后重试"
Quit
no_run:
FunctionEnd
; 如果已经是安装过的, 禁用修改目录的地方
Function BEFORE_DIRECTORY
StrLen $1 $DefaultInstallDir
${If} $1 > 0
;禁用浏览按钮
FindWindow $0 "#32770" "" $HWNDPARENT
GetDlgItem $0 $0 1001
EnableWindow $0 0
;禁止编辑目录
FindWindow $0 "#32770" "" $HWNDPARENT
GetDlgItem $0 $0 1019
EnableWindow $0 0
${EndIf}
FunctionEnd
;--------------------------------
;Installer Sections
Section "install"
; 释放文件
SetOutPath "$INSTDIR"
File /r PROGRESSNAMEDG\*.*
; 创建桌面快捷方式
CreateShortCut "$desktop\${APPNAME}.lnk" "$INSTDIR\${MAIN_EXE_NAME}.exe" "" "$INSTDIR\icon\project.ico"
;Create uninstaller
WriteUninstaller "$INSTDIR\Uninstall.exe"
# Start Menu
createDirectory "$SMPROGRAMS\${COMPANYNAME}"
createShortCut "$SMPROGRAMS\${COMPANYNAME}\${APPNAME}.lnk" "$INSTDIR\${MAIN_EXE_NAME}.exe" "" "$INSTDIR\icon\project.ico"
createShortCut "$SMPROGRAMS\${COMPANYNAME}\卸载客户端.lnk" "$INSTDIR\Uninstall.exe" "" "$INSTDIR\icon\project-uninstall.ico"
; 写入注册信息
WriteRegStr HKLM "Software\${APPNAME}" "InstallLocation" "$INSTDIR"
# Registry information for add/remove programs
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "DisplayName" "${DESCRIPTION}"
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\icon\project.ico"
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "Publisher" "${COMPANYNAME}"
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "URLInfoAbout" "${ABOUTURL}"
WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "DisplayVersion" "${VERSIONMAJOR}.${VERSIONMINOR}.${VERSIONBUILD}"
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}
SectionEnd
;--------------------------------
;Uninstaller Section
Section "Uninstall"
; 删除文件
RMDir /r $INSTDIR
; 删除桌面快捷方式
Delete "$desktop\${APPNAME}.lnk"
; 删除开始菜单
RMDir /r "$SMPROGRAMS\${COMPANYNAME}"
# Remove uninstaller information from the registry
DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}"
;删除注册表项
DeleteRegKey HKLM "Software\${APPNAME}"
SectionEnd