NSIS安装脚本简单学习

; 注意 : 文件要保存为 UTF-8-BOM 编码 , 否则在执行此nsi脚本的时候会出现 Bad Text Encoding的错误 
# 包含头文件 
!include "MUI.nsh"
!include "LogicLib.nsh"

!define MUI_ABORTWARNING
!define MUI_ICON "./uires/logo.ico" #安装icon
!define MUI_UNICON "./uires/logo.ico" #卸载icon


!insertmacro MUI_PAGE_WELCOME #安装欢迎页面

!insertmacro MUI_PAGE_DIRECTORY #选择安装目录页面

!insertmacro MUI_LANGUAGE "SimpChinese" #安装界面显示的语言

!insertmacro MUI_PAGE_INSTFILES #安装过程页面

LangString ProductName ${LANG_SimpChinese} "PC客户端"
LangString UnInstallTip ${LANG_SimpChinese} "准备好卸载了吗" #卸载提示
LangString InstallTip ${LANG_SimpChinese} "准备好安装了吗" # 安装提示
LangString RunExeTip ${LANG_SimpChinese} "是否立即运行程序" # 运行程序提示
LangString FinishRunButtonText ${LANG_SimpChinese} "完成" 
LangString FinishCheckBoxText ${LANG_SimpChinese} "myapp.exe" 
LangString UninstallEnsure ${LANG_SimpChinese} "你确实要完全移除 $(ProductName) ,及其所有的组件?"
LangString UninstallResult ${LANG_SimpChinese} "$(ProductName) 已成功地从你的计算机移除。"

!define MUI_FINISHPAGE_TEXT $(RunExeTip)
!define MUI_FINISHPAGE_BUTTON $(FinishRunButtonText) #完成按钮的文案
!define MUI_FINISHPAGE_RUN_TEXT  $(FinishCheckBoxText) #运行应用程序旁边的文字
!define MUI_FINISHPAGE_RUN "$INSTDIR\myapp.exe" #安装完成运行程序
!insertmacro MUI_PAGE_FINISH #安装完成页面 define定义的属性 要在对应的页面 insertmacro 之前


; 安装程序初始定义常量
!define PRODUCT_NAME $(ProductName)

!define PRODUCT_VERSION "1.0.0.1"

!define PRODUCT_PUBLISHER $(ProductName)
!define PRODUCT_WEB_SITE "http://www.baidu.com"
!define PRODUCT_UNINST_ROOT_KEY "HKLM"
!define REG_NAME "myapp1"


Name "myapp" #安装界面显示的名称
OutFile "${REG_NAME}Install.exe" #输出的安装包的名称路径
InstallDir "D:\${REG_NAME}Install" #安装界面的选择目录时的默认安装目录

; 安装section 打包的时候(makenisi)程序会执行的地方
Section Install
    SetOutPath $INSTDIR
    File /r .\setup\*.dll #将此目录下的所有dll 打包进安装包
    File .\setup\myapp.exe
    ;协议注册启动程序
    WriteRegStr HKCR "${REG_NAME}" "" ""
    WriteRegStr HKCR "${REG_NAME}" "URL Protocol" ""
    WriteRegStr HKCR "${REG_NAME}\DefaultIcon" "" "${REG_NAME}.exe,1"
    WriteRegStr HKCR "${REG_NAME}\shell" "" ""
    WriteRegStr HKCR "${REG_NAME}\shell\open" "" ""
    WriteRegStr HKCR "${REG_NAME}\shell\open\command" "" '"$INSTDIR\myapp.exe" "%1"'
SectionEnd

; 卸载section 运行卸载程序会执行的地方
Section Uninstall
    ;MessageBox MB_ICONINFORMATION|MB_OK $(UnInstallTip) 
    DeleteRegKey HKCR "${REG_NAME}\shell\open\command"
    DeleteRegKey HKCR "${REG_NAME}\shell\open"
    DeleteRegKey HKCR "${REG_NAME}\shell"
    DeleteRegKey HKCR "${REG_NAME}\DefaultIcon"
    DeleteRegKey HKCR "${REG_NAME}"
    Delete "$INSTDIR\*.dll"
    Delete "$INSTDIR\myapp.exe"
    Delete "$INSTDIR\uninst.exe"
    ;RMDir /r "$INSTDIR"
SectionEnd

;卸载程序的输出
Section -Post
    WriteUninstaller ".\uninst.exe" #卸载程序 会在安装完成后写入到安装目录
SectionEnd    

/* 普通函数 可以用Call 来调用 */
Function NormalFunc
    MessageBox MB_ICONINFORMATION|MB_OK "normal func"
FunctionEnd

# 安装之前执行的回调函数
Function .onInit
    MessageBox MB_ICONINFORMATION|MB_OK $(InstallTip)
    Call NormalFunc
FunctionEnd

# 卸载之前执行的回调函数
Function un.onInit
    MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 $(UninstallEnsure)  IDYES true IDNO false
    true:
        ;检测进程是否运行
        Goto done
    false:
        Abort
    done:
        Goto +1 ;Goto +1 goes to the next instruction, Goto -1 goes to the previous instruction, etc.
FunctionEnd

Function un.onUninstSuccess
    HideWindow
    MessageBox MB_ICONINFORMATION|MB_OK $(UninstallResult)
FunctionEnd

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
一.环境变量配置 ; 定义系统环境变量的 注册表key值 !define WriteEnvStr_RegKey 'HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"' ;设置环境变量 Function "regExpandStr" SetOverwrite ifnewer ;设置jdk环境变量 WriteRegExpandStr ${WriteEnvStr_RegKey} "JAVA_HOME" "$INSTDIR\jdk1.8.0_131" ;设置CATALINA_DIR WriteRegExpandStr ${WriteEnvStr_RegKey} "CATALINA_DIR" "$INSTDIR\tomcat\bin" ;设置CATA_LINA WriteRegExpandStr ${WriteEnvStr_RegKey} "CATA_LINA" "$INSTDIR\tomcat" ;设置path ReadRegStr $0 HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment" "Path" WriteRegExpandStr HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment" "Path" "$0;C:\windows\system32;$INSTDIR\redis;$INSTDIR\mysql\bin;$INSTDIR\Java\jdk1.8.0_131\bin;" #第一次是使环境变量修改对其他进程有效;刷新环境变量 SendMessage ${HWND_BROADCAST} ${WM_WININICHANGE} 0 "STR:Environment" /TIMEOUT=5000 SetOverwrite on functionend ;注册服务 Function "installService" detailprint "------------------------------------install tomcat Service..." Sleep 2000 SetOutPath "$INSTDIR\tomcat\bin" nsExec::Exec 'cmd /c service install' nsExec::Exec 'cmd /c sc config tomcat8 start= auto' ;设置服务自动开启 detailprint "------------------------------------install tomcat Service success..." detailprint "install mysql Service..." Sleep 2000 SetOutPath "$INSTDIR\mysql\bin" nsExec::Exec 'cmd /c $INSTDIR\mysql\bin\mysqld.exe" -install Mysql --defaults-file="$INSTDIR\\mysql\\my.ini' detailprint "install mysql Service success..." detailprint "------------------------------------install redis Service..." Sleep 2000 SetOutPath "$INSTDIR\redis" nsExec::Exec 'cmd /c redis-server --service-install redis.windows.conf' detailprint "install redis Service success..." functionend ; 启动服务 Function "startService" detailprint "------------------------------------start tomcat..." nsExec::Exec 'cmd /c sc start Tomcat8' detailprint "------------------------------------start tomcat success..." detailprint "------------------------------------start mysql..." nsExec::Exec 'cmd /c sc start mysql' detailprint "------------------------------------start MySQL success..." detailprint "------------------------------------start redis..." ;nsExec::Exec 'cmd /c sc redis-server --service-start --service-name redis' detailprint "------------------------------------start redis success..." functionend ;停止服务 Function "un.stopService" detailprint "正在停止tomcat服务......." nsExec::Exec 'cmd /c sc stop tomcat8' detailprint "停止tomcat服务成功......." detailprint "正在停止mysql服务......." nsExec::Exec 'cmd /c sc stop mysql' detailprint "停止mysql服务成功......." detailprint "正在停止redis服务......." nsExec::Exec 'cmd /c sc redis-server --service-stop --service-name redis' detailprint "停止redis服务成功......." functionend ;删除服务 Function "un.uninstService" detailprint "正在卸载tomcat服务......." nsExec::Exec 'cmd /c sc delete tomcat8' detailprint "卸载tomcat服务成功......." detailprint "正在卸载mysql服务......." nsExec::Exec 'cmd /c sc delete mysql' detailprint "卸载mysql服务成功......." detailprint "正在卸载redis服务......." nsExec::Exec 'cmd /c sc delete redis' detailprint "卸载redis服务成功......." functionend ;清空环境变量 Function "un.emptyRegExpandStr" #删除 系统环境变量JAVA_HOME 和 CATALINA_HOME #卸载的时候必须设置JAVA_HOME 和 CATALINA_HOME两个环境系统变量为空,或者删除这两个系统变量。否则会出现NSIS选择系统变量的错误(NSIS会自动选择之前有值的系统变量,而不是选择当前刚设置的系统变量) detailprint "正在删除相关环境变量......." functionend 二.校验mac地址 #序列号 校验页面 Page custom check_serial_number Var "MacAddress" Function check_serial_number Call .GetMacAddress #MessageBox MB_OK '当前机器Mac地址为:$MacAddress' ${if} $MacAddress == '00-50-56-C0-00-08' ${OrIf} $MacAddress == '00-50-56-C0-00-081' #MessageBox MB_OK '已授权' ${Else} MessageBox MB_OK '未授权,请联系经销商并授权!' ;SendMessage $HWNDPARENT 0x408 3 0 #页面跳转 Call onClickClose ${EndIf} FunctionEnd #获取主机MAC地址 Function .GetMacAddress System::Call Iphlpapi::GetAdaptersInfo(i,*i.r0) System::Alloc $0 Pop $1 System::Call Iphlpapi::GetAdaptersInfo(ir1r2,*ir0)i.r0 StrCmp $0 0 0 finish loop: StrCmp $2 0 finish System::Call '*$2(i.r2,i,&t260;.s,&t132;.s,i.r5)i.r0' ;Unicode版将t改为m IntOp $3 403 + $5 StrCpy $6 "" ${For} $4 404 $3 IntOp $7 $0 + $4 System::Call '*$7(&i1;.r7)' IntFmt $7 "X" $7 StrCpy $6 "$6$7" StrCmp $4 $3 +2 StrCpy $6 "$6-" ${Next} StrCpy $MacAddress $6 Goto loop finish: System::Free $1 FunctionEnd 三.关闭按钮功能 ;点击右上角关闭按钮 Function onClickClose FindProcDLL::FindProc "${PRODUCT_NAME}.exe" Sleep 500 Pop $R0 ${If} $R0 != 0 KillProcDLL::KillProc "${PRODUCT_NAME}.exe" ${EndIf} FunctionEnd

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程经验随笔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值