脚本:包含mysql数据打包安装广告制作管理软件的打包源码

优质
小牛编辑
132浏览
2023-12-01

以下是一个广告制作管理软件的打包源码,包含mysql数据打包安装。公司名字给用XX注释掉了,打包完整源码如下:

; 该脚本使用 HM VNISEdit 脚本编辑器向导产生

; 安装程序初始定义常量
!define PRODUCT_NAME "XX广告制作软件"
!define PRODUCT_VERSION "2.2"
!define PRODUCT_PUBLISHER "XX.com, Inc."
!define PRODUCT_WEB_SITE "http://www.1bsoft.com"
!define PRODUCT_DIR_REGKEY "Software\Microsoft\Windows\CurrentVersion\App Paths\YBest.Ads.exe"
!define PRODUCT_UNINST_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}"
!define PRODUCT_UNINST_ROOT_KEY "HKLM"

SetCompressor lzma

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

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

; 欢迎页面
!insertmacro MUI_PAGE_WELCOME
; 许可协议页面
!insertmacro MUI_PAGE_LICENSE "Readme.txt"
; 安装目录选择页面
!insertmacro MUI_PAGE_DIRECTORY
; 安装过程页面
!insertmacro MUI_PAGE_INSTFILES
; 安装完成页面
!define MUI_FINISHPAGE_RUN "$INSTDIR\YBest.Ads.exe"
!insertmacro MUI_PAGE_FINISH

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

; 安装界面包含的语言设置
!insertmacro MUI_LANGUAGE "SimpChinese"
;文件版本声明
VIProductVersion "2.2.0.0"
VIAddVersionKey /LANG=2052 "ProductName" "XX广告制作软件"
VIAddVersionKey /LANG=2052 "Comments" "软件版权归XX软件所有,他人不得复制或二次开发本程序。"
VIAddVersionKey /LANG=2052 "CompanyName" "www.XX.com"
VIAddVersionKey /LANG=2052 "LegalTrademarks" "XX软件"
VIAddVersionKey /LANG=2052 "LegalCopyright" "XX.com"
VIAddVersionKey /LANG=2052 "FileDescription" "XX广告制作软件安装程序"
VIAddVersionKey /LANG=2052 "FileVersion" "2.2.0.0"

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

Name "${PRODUCT_NAME} ${PRODUCT_VERSION}"
OutFile "YBSetup.exe"
InstallDir "$PROGRAMFILES\XX广告制作软件"
InstallDirRegKey HKLM "${PRODUCT_UNINST_KEY}" "UninstallString"
ShowInstDetails show
ShowUnInstDetails show
BrandingText "XX软件 www.XX.com"
DirText "安装向导将把 $(^NameDA) 安装在下列文件夹,如果要安装到其他文件夹请单击 [浏览(B)] 进行选择。"

Section "MainSection" SEC01
  SetOutPath "$INSTDIR"
  SetOverwrite ifnewer
  CreateDirectory "$SMPROGRAMS\XX广告制作软件"
  CreateShortCut "$SMPROGRAMS\XX广告制作软件\XX广告制作软件.lnk" "$INSTDIR\YBest.Ads.exe"
  CreateShortCut "$DESKTOP\XX广告制作软件.lnk" "$INSTDIR\YBest.Ads.exe"
  File "DevComponents.DotNetBar2.dll"
  File "DevComponents.DotNetBar2.xml"
  File "LitJSON.dll"
  File "MySql.Data.dll"
  File "NPOI.dll"
  File "ybkj.ico"
  File "YBest.Ads.exe"
  File "YBest.Ads.exe.config"
  File "YBest.BLL.dll"
  File "YBest.Common.dll"
    File "YBest.DAL.dll"
    File "YBest.DBUtility.dll"
    File "YBest.Model.dll"
  File "Readme.txt"
  File "MqttLib.dll"
  File "PullService.dll"
  SetOutPath "$INSTDIR\XmlConfig"
  SetOverwrite ifnewer
  File /r "XmlConfig\*"
  SetOutPath "$INSTDIR\images"
  SetOverwrite ifnewer
  File /r "images\*"
  SetOutPath "$INSTDIR\mysql5.1"
  SetOverwrite ifnewer
  File /r "mysql5.1\*"
  ;安装mysql服务
  call installMysql
SectionEnd

Section -AdditionalIcons
  WriteIniStr "$INSTDIR\${PRODUCT_NAME}.url" "InternetShortcut" "URL" "${PRODUCT_WEB_SITE}"
  CreateShortCut "$SMPROGRAMS\XX广告制作软件\XX软件官网.lnk" "$INSTDIR\${PRODUCT_NAME}.url"
  CreateShortCut "$SMPROGRAMS\XX广告制作软件\卸载${PRODUCT_NAME}.lnk" "$INSTDIR\uninst.exe"
SectionEnd

Section -Post
  WriteUninstaller "$INSTDIR\uninst.exe"
  WriteRegStr HKLM "${PRODUCT_DIR_REGKEY}" "" "$INSTDIR\AdsBow.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\AdsBow.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}"
  WriteRegDword HKLM "${PRODUCT_DIR_REGKEY}" "Installed" 1
  WriteRegStr HKLM "${PRODUCT_DIR_REGKEY}" "InstalledPath" "$INSTDIR"
  WriteRegStr HKLM "${PRODUCT_DIR_REGKEY}" "version" "${PRODUCT_VERSION}" ;写入版本号
SectionEnd

Section -.NET
Call GetNetFrameworkVersion
Pop $R1
  ${If} $R1 < '2.0.50727'
  SetDetailsPrint textonly
  DetailPrint "正在安装 .NET Framework 2.0,这个可能需要几分钟,请耐心等待..."
  SetDetailsPrint listonly
  SetOutPath "$TEMP"
  SetOverwrite on
  File "NetFx20SP2_x86.exe"
  ExecWait '$TEMP\NetFx20SP2_x86.exe /quiet /norestart' $R1
  Delete "$TEMP\NetFx20SP2_x86.exe"
  ${EndIf}
SectionEnd

/******************************
 *  以下是安装程序的卸载部分  *
 ******************************/

Section Uninstall
  Delete "$INSTDIR\${PRODUCT_NAME}.url"
  Delete "$INSTDIR\uninst.exe"
  Delete "$INSTDIR\YBest.Ads.exe"
  Delete "$INSTDIR\YBest.Ads.exe.config"
  Delete "$INSTDIR\YBest.BLL.dll"
  Delete "$INSTDIR\YBest.Common.dll"
  Delete "$INSTDIR\YBest.DAL.dll"
  Delete "$INSTDIR\YBest.DBUtility.dll"
  Delete "$INSTDIR\YBest.Model.dll"
  Delete "$INSTDIR\DevComponents.DotNetBar2.dll"
  Delete "$INSTDIR\DevComponents.DotNetBar2.xml"
  Delete "$INSTDIR\LitJSON.dll"
  Delete "$INSTDIR\MySql.Data.dll"
  Delete "$INSTDIR\NPOI.dll"
  Delete "$INSTDIR\ybkj.ico"
  Delete "$INSTDIR\Readme.txt"
  Delete "$INSTDIR\MqttLib.dll"
  Delete "$INSTDIR\PullService.dll"
  ;停止mysql服务 并移除服务
  call un.removeService
    ; 循环删除文件直至删除文件夹
    RMDir /r "$INSTDIR\images"
    RMDir /r "$INSTDIR\XmlConfig"
    RMDir /r "$INSTDIR\mysql5.1"

  Delete "$SMPROGRAMS\XX广告制作软件\卸载${PRODUCT_NAME}.lnk"
  Delete "$SMPROGRAMS\XX广告制作软件\XX软件官网.lnk"
  Delete "$DESKTOP\XX广告制作软件.lnk"
  Delete "$SMPROGRAMS\XX广告制作软件\XX广告制作软件.lnk"

  RMDir "$SMPROGRAMS\XX广告制作软件"
  RMDir "$INSTDIR"

  DeleteRegKey ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}"
  DeleteRegKey HKLM "${PRODUCT_DIR_REGKEY}"
  SetAutoClose true
SectionEnd

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



Function GetNetFrameworkVersion
;获取.Net Framework版本,支持
Push $1
Push $0
ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5" "Install"
ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5" "Version"
StrCmp $0 1 KnowNetFrameworkVersion +1
ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0\Setup" "InstallSuccess"
ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0\Setup" "Version"
StrCmp $0 1 KnowNetFrameworkVersion +1
ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v2.0.50727" "Install"
ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v2.0.50727" "Version"
StrCmp $1 "" +1 +2
StrCpy $1 "2.0.50727.832"
StrCmp $0 1 KnowNetFrameworkVersion +1
ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v1.1.4322" "Install"
ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\NET Framework Setup\NDP\v1.1.4322" "Version"
StrCmp $1 "" +1 +2
StrCpy $1 "1.1.4322.573"
StrCmp $0 1 KnowNetFrameworkVersion +1
ReadRegDWORD $0 HKLM "SOFTWARE\Microsoft\.NETFramework\policy\v1.0" "Install"
ReadRegDWORD $1 HKLM "SOFTWARE\Microsoft\.NETFramework\policy\v1.0" "Version"
StrCmp $1 "" +1 +2
StrCpy $1 "1.0.3705.0"
StrCmp $0 1 KnowNetFrameworkVersion +1
StrCpy $1 "not .NetFramework"
KnowNetFrameworkVersion:
Pop $0
Exch $1
FunctionEnd

;安装mysql服务
Function installMysql
  Push "mysqld.exe"
        ProcessWork::existsprocess
        Pop $R0
        IntCmp $R0 0 no_install ;判断mysql是否已经安装
        ;写入注册表表示不是本程序安装的mysql,卸载的时候不需要卸载mysql数据库
            WriteRegDword HKLM "${PRODUCT_DIR_REGKEY}" "MySqlInstalled" 0
            MessageBox MB_OK|MB_ICONEXCLAMATION "本机已安装了mysql数据库,请手动导入数据信息,谢谢!"
        Goto done ;退出安装mysql
        no_install: ;没有安装则安装
            ExecWait "$INSTDIR\mysql5.1\bin\mysqld --install mysql"
            ;设置mysql的环境变量到PATH路径
            ReadRegStr $0 HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment" "Path"
            WriteRegExpandStr HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment" "Path" "$0;$INSTDIR\mysql5.1\bin\"
            ;刷新环境变量
            SendMessage ${HWND_BROADCAST} ${WM_WININICHANGE} 0 "STR:Environment"
            ;启动服务
            ExecWait "net start mysql"
            ;写入注册表表示是本程序安装的mysql,方便卸载的时候卸载mysql数据库
            WriteRegDword HKLM "${PRODUCT_DIR_REGKEY}" "MySqlInstalled" 1
            ;完成
            done:
FunctionEnd

Function .onInit
InitPluginsDir
  ;创建互斥防止重复运行
  System::Call 'kernel32::CreateMutexA(i 0, i 0, t "YBSetup") i .r1 ?e'
  Pop $R0
  StrCmp $R0 0 +3
    MessageBox MB_OK|MB_ICONEXCLAMATION "有一个YBSetup安装向导已经运行!"
  Abort
;禁止多次安装实例 start
ReadRegDWORD $0 HKLM '${PRODUCT_DIR_REGKEY}' "Installed"
IntCmp $0 +1 +4
MessageBox MB_OK|MB_USERICON '$(^Name) 已安装在计算机中。如需重新安装,请卸载已有的安装。'
Quit
nop
;禁止多次安装实例 end
FunctionEnd

;--------------- 移除mysql服务 ----------------
Function un.removeService
;读取是否是本程序安装mysql
ReadRegDWORD $0 HKLM '${PRODUCT_DIR_REGKEY}' "MySqlInstalled"
IntCmp $0 1 0 clearDb
        ;停止mysql服务
        ExecWait "net stop Mysql"
        ;移除mysql服务
        ExecWait "$INSTDIR\mysql5.1\bin\mysqld --remove Mysql"
        ;移除环境变量
        ReadRegStr $R0 HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment" "Path"
        ${WordReplace} $R0 ";$INSTDIR\mysql5.1\bin\" "" "+" $R1
        ;MessageBox MB_OK|MB_USERICON '$R0 - $INSTDIR - $R1 '
        WriteRegExpandStr HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment" "Path" "$R1"
    ;清除数据库信息
    clearDb:
    
FunctionEnd
;---------------------------------
Function un.onInit
  FindProcDLL::FindProc "YBest.Ads.exe"
   Pop $R0
   IntCmp $R0 1 0 no_run
   MessageBox MB_ICONSTOP "安装程序检测到 $(^Name) 正在运行,请退出程序后重试"
   Quit
   no_run:
  MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 "您确实要完全移除 $(^Name) ,及其所有的组件?" IDYES +2
  Abort
FunctionEnd

Function un.onUninstSuccess
  HideWindow
  MessageBox MB_ICONINFORMATION|MB_OK "$(^Name) 已成功地从您的计算机移除。"
FunctionEnd