“NSIS (Nullsoft Scriptable Install System) 是 Windows 下的一个工具,它允许程序员
来创建安装程序。它发布于一个开源的协议并且对于任何使用来说都是完全免费的。”
“NSIS 创建的安装程序能够安装、卸载、设置系统设置、解压文件等等。因为它基于脚本文
件,你可以完全的控制安装程序的每一部分。脚本语言支持变量、函数、字串操作,就像一个
普通的程序语言一样。即使有那么多的特性,NSIS 仍然是最小的安装程序系统。在默认选项下,
它仅增加了 34 KB 的开销。”
花了我近1个月时间,我用NSIS制作了一个完整的演示产品安装程序,实现功能概要如下:
1.安装路径的选择
2.浏览器的选择
3.桌面数据库的自动安装及启动
4.数据库实例及演示数据的自动恢复
5.应用服务器的自动安装
6.JDK的安装及环境变量的设置
7.应用程序的自动安装
8.数据库实例端口的设置(要考虑用户机器上可能存在别的sqlserver数据库实例)
9.windows服务中的自动启动项的安装
10.启动应用
11.readme的打开
12.用浏览器打开应用的登录界面
13.在安装过程中切换公司或应用的介绍信息;
14.自动卸载等
还存在的遗留问题:
1.如果安装的机器上存在sqlserver2005 则本程序的sqlserver桌面引擎会安装失败;
2.安装过程中的信息切换会在操作系统的最前面,所以会把你别的程序的界面覆盖掉,除非你用 窗口键 +D键 最小化所有应用;
如果哪位网友解决了这个问题,请将解决方法及源码留在本留言栏,如有疑问也请留言,谢谢。
#author 超能
# 2008-7-03 9:48:08
Name 超能 System
SetCompressor zlib
# Defines
!define REGKEY "SOFTWARE\$(^Name)"
!define VERSION 1.0
!define COMPANY 超能公司
!define URL www.XXXX.com
# MUI defines
#!define MUI_ICON logo.ico
#!define MUI_UNICON logo.ico
!define MUI_FINISHPAGE_NOAUTOCLOSE
#定义readme
!define MUI_FINISHPAGE_SHOWREADME $INSTDIR\readme.txt
!define MUI_FINISHPAGE_RUN "$PROGRAMFILES\Internet Explorer\IEXPLORE.EXE"
!define MUI_FINISHPAGE_RUN_PARAMETERS 登录主页的url
!define MUI_CUSTOMFUNCTION_GUIINIT CustomGUIInit
!define MUI_UNFINISHPAGE_NOAUTOCLOSE
# Included files
!include Sections.nsh
!include MUI.nsh
!include "LogicLib.nsh"
!include "${NSISDIR}\Examples\System\System.nsh"
!include WinMessages.nsh
# Reserved Files
ReserveFile "${NSISDIR}\Plugins\BGImage.dll"
ReserveFile "${NSISDIR}\Plugins\AdvSplash.dll"
# Variables
Var StartMenuGroup
# Installer pages
!insertmacro MUI_PAGE_WELCOME
#!insertmacro MUI_PAGE_COMPONENTS
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH
!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES
# Installer languages
!insertmacro MUI_LANGUAGE SimpChinese
Caption "超能MIS"
Icon "img\install.ico"
BrandingText " 超能有限公司 -- www.超能.com "
# Installer attributes
OutFile 超能.Exe
InstallDir $PROGRAMFILES\超能
!define MUI_HEADERIMAGE
CRCCheck on
XPStyle on
ShowInstDetails show
#nevershow
VIProductVersion 1.0.0.0
VIAddVersionKey /LANG=${LANG_SIMPCHINESE} ProductName 超能system
VIAddVersionKey ProductVersion "${VERSION}"
VIAddVersionKey /LANG=${LANG_SIMPCHINESE} CompanyName "${COMPANY}"
VIAddVersionKey /LANG=${LANG_SIMPCHINESE} CompanyWebsite "${URL}"
VIAddVersionKey /LANG=${LANG_SIMPCHINESE} FileVersion ""
VIAddVersionKey /LANG=${LANG_SIMPCHINESE} FileDescription ""
VIAddVersionKey /LANG=${LANG_SIMPCHINESE} LegalCopyright ""
InstallDirRegKey HKLM "${REGKEY}" Path
ShowUninstDetails hide
UninstallText "将要卸载 超能system .点击继续."
UninstallIcon "img\uninstall.ico"
var DBPATH
# Installer SQL Server 2000 sections
Section SqlServer SEC0000
#MessageBox MB_OK $EXEDIR
#调用第一个信息
IntOp $9 0 + 1
call showBg
StrCpy $DBPATH $INSTDIR
;ReadRegStr $0 HKLM "Software\Microsoft\MSSQLServer\Setup" "SQLPath"
ReadRegStr $0 HKLM "Software\Microsoft\Microsoft SQL Server\XXXX\Setup" "SQLPath"
StrCmp $0 "" 0 +9
DetailPrint "正在安装SQL Server 2000 桌面引擎,请等约两分钟。 "
ExecWait '"$EXEDIR\Disk1\MSDERelA\setup.exe" DISABLENETWORKPROTOCOLS=0 SAPWD="XXXXXX"
SECURITYMODE=SQL INSTANCENAME="XXXX" DATADIR="$INSTDIR\" TARGETDIR="$INSTDIR\"'
DetailPrint "SQL Server 2000 桌面引擎安装完成!"
IfFileExists $DBPATH\MSSQL$$XXXX\Data\XXXXX.mdf 0 +2
Delete /REBOOTOK $DBPATH\MSSQL$$XXXXX\Data\XXXXX.mdf
IfFileExists $DBPATH\MSSQL$$XXXXX\Data\XXXXX_log.LDF 0 +2
Delete /REBOOTOK $DBPATH\MSSQL$$XXXXX\Data\XXXXX_log.LDF
goto +2
StrCpy $DBPATH $0
IntOp $9 0 + 2
call showBg
ExecWait '"net.exe" start MSSQL$$XXXXX'
ExecWait '"osql.exe" -U sa -P XXXXXX -S .\XXXXX -Q "Create Database XXXXX"'
DetailPrint "正在恢复演示数据!请耐心等待。"
ExecWait '"osql.exe" -U sa -P styx2008 -S .\XXXXX -Q "restore database XXXX from
disk=$\'$EXEDIR\Disk1\XXXX$\' with move $\'XXXX_Data$\' to
$\'$DBPATH\MSSQL$$XXXX\Data\XXXX.mdf$\', move $\'XXXX_Log$\' to
$\'$DBPATH\MSSQL$$XXXX\Data\XXXX_log.LDF$\'"'
DetailPrint "演示数据恢复完成!"
IntOp $9 0 + 3
call showBg
SectionEnd
# Installer jdk1.5.0_06 sections
Section -JDK5 SEC0001
SetOutPath $INSTDIR
File readme.txt
File img\install.ico
DetailPrint "正在安装Java虚拟机!"
SetOutPath "$INSTDIR\jdk1.5.0_06"
SetOverwrite on
File /r "C:\Program Files\Java\jdk1.5.0_06\*"
push $9
IntOp $9 0 + 4
call showBg
pop $9
;SetOutPath $INSTDIR
SendMessage ${HWND_BROADCAST} ${WM_WININICHANGE} 0 "STR:Environment" /TIMEOUT=0
WriteRegExpandStr HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
"JAVA_HOME" "$INSTDIR\jdk1.5.0_06"
WriteRegExpandStr HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
"TOMCAT_HOME" "$PROGRAMFILES\Tomcat 5.0"
SendMessage ${HWND_BROADCAST} ${WM_WININICHANGE} 0 "STR:Environment" /TIMEOUT=0
WriteRegStr HKLM "${REGKEY}\Components" JDK5 1
DetailPrint "Java虚拟机安装完成!"
push $9
IntOp $9 0 + 5
call showBg
pop $9
SectionEnd
# Installer Tomcat 5.0 sections
Section -Server SEC0002
IntOp $9 0 + 6
call showBg
DetailPrint "正在安装WEB服务器!"
SetOutPath "$PROGRAMFILES\Tomcat 5.0"
SetOverwrite on
File /r "D:\Tomcat 5.0\*"
push $9
IntOp $9 0 + 7
call showBg
pop $9
;ReadRegStr $0 HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
"TOMCAT_HOME"
;WriteRegExpandStr HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
"TOMCAT_HOME" "$PROGRAMFILES\Tomcat 5.0"
SendMessage ${HWND_BROADCAST} ${WM_WININICHANGE} 0 "STR:Environment"
#在windows服务中加入”启动服务“项
ExecWait '"$PROGRAMFILES\Tomcat 5.0\bin\NetInstall.bat" "$PROGRAMFILES\Tomcat 5.0\bin"
"$INSTDIR\jdk1.5.0_06"'
#ExecWait '"$PROGRAMFILES\Tomcat 5.0\bin\tomcat5.exe" //IS//XXXX --DisplayName="XXXX"
--Install="$PROGRAMFILES\Tomcat 5.0\bin\tomcat5.exe" --Jvm=auto --StartMode=jvm --
StopMode=jvm --StartClass=org.apache.catalina.startup.Bootstrap --StartParams=start --
StopClass=org.apache.catalina.startup.Bootstrap --StopParams=stop'
#ExecWait '"$PROGRAMFILES\Tomcat 5.0\bin\tomcat5.exe" //IS//XXXX --DisplayName="XXXX"
--Install="$PROGRAMFILES\Tomcat 5.0\bin\tomcat5.exe" --Jvm="$INSTDIR\jdk1.5.0_06
\jre\bin\server\jvm.dll" --StartMode=jvm --StopMode=jvm --
StartClass=org.apache.catalina.startup.Bootstrap --StartParams=start --
StopClass=org.apache.catalina.startup.Bootstrap --StopParams=stop'
ExecWait '"$PROGRAMFILES\Tomcat 5.0\bin\tomcat5.exe" //US//XXXX --Description="XXXX" -
-Startup=auto'
DetailPrint "WEB服务器安装完成!"
DetailPrint "WEB服务器开始启动!"
push $9
IntOp $9 0 + 8
call showBg
pop $9
启动tomcat
ExecWait '"net" start XXXX'
push $9
IntOp $9 0 + 9
call showBg
pop $9
DetailPrint "WEB服务器启动完成!"
WriteRegStr HKLM "${REGKEY}\Components" Server 1
push $9
IntOp $9 0 + 10
call showBg
pop $9
push $1
push $0
#读取当前数据库实例的端口,并且写入应用的配置文件
ReadRegStr $1 HKLM "Software\Microsoft\Microsoft SQL
Server\XXXX\MSSQLServer\SuperSocketNetLib\Tcp" "TcpPort"
StrCmp $1 "" done 0
;IfFileExists $PROGRAMFILES\Tomcat 5.0\webapps\XXXX\WEB-INF\Node.driver.xml 0 +2
FileOpen $0 '$PROGRAMFILES\Tomcat 5.0\webapps\XXXX\WEB-INF\Node.driver.xml' w
FileWrite $0 '<?xml version="1.0" encoding="utf-8"?>$\r$\n'
FileWrite $0 '<driver project-version="1.0"
class="com.microsoft.jdbc.sqlserver.SQLServerDriver">$\r$\n'
FileWrite $0 '<url
value="jdbc:microsoft:sqlserver://127.0.0.1:$1;DatabaseName=XXXX"/>$\r$\n'
FileWrite $0 '<connectionPool min="1" max="100" />$\r$\n'
FileWrite $0 '<login userName="sa" password="XXXX"/>$\r$\n'
FileWrite $0 '</driver>'
FileClose $0
done:
pop $1
pop $0
; CopyFiles $INSTDIR\*.dat $INSTDIR\backup
SectionEnd
# Installer Shortcut sections
Section -Shortcut SEC0003
SetOutPath $SMPROGRAMS\$StartMenuGroup
CreateShortcut $SMPROGRAMS\$StartMenuGroup\bin.lnk "$PROGRAMFILES\Tomcat 5.0\bin"
CreateShortcut $SMPROGRAMS\$StartMenuGroup\超能系统.lnk "$PROGRAMFILES\Internet
Explorer\IEXPLORE.EXE " "http://127.0.0.1:8080/XXXX/login.jsp"
CreateShortcut $SMPROGRAMS\$StartMenuGroup\ReadMe.lnk "notepad.exe "
"$INSTDIR\readme.txt"
WriteRegStr HKLM "${REGKEY}\Components" Shortcut 1
IntOp $9 0 + 11
call showBg
SectionEnd
Section -post SEC0004
WriteRegStr HKLM "${REGKEY}" Path $INSTDIR
SetOutPath $INSTDIR
WriteUninstaller $INSTDIR\uninstall.exe
SetOutPath $SMPROGRAMS\$StartMenuGroup
CreateShortcut "$SMPROGRAMS\$StartMenuGroup\$(^UninstallLink).lnk"
$INSTDIR\uninstall.exe
WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$(^Name)"
DisplayName "$(^Name)"
WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$(^Name)"
DisplayVersion "${VERSION}"
WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$(^Name)"
Publisher "${COMPANY}"
WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$(^Name)"
URLInfoAbout "${URL}"
WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$(^Name)"
DisplayIcon $INSTDIR\logo.ico
WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$(^Name)"
UninstallString $INSTDIR\uninstall.exe
WriteRegDWORD HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$(^Name)"
NoModify 1
WriteRegDWORD HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$(^Name)"
NoRepair 1
IntOp $9 0 + 12
call showBg
SectionEnd
# Macro for selecting uninstaller sections
!macro SELECT_UNSECTION SECTION_NAME UNSECTION_ID
Push $R0
ReadRegStr $R0 HKLM "${REGKEY}\Components" "${SECTION_NAME}"
StrCmp $R0 1 0 next${UNSECTION_ID}
!insertmacro SelectSection "${UNSECTION_ID}"
GoTo done${UNSECTION_ID}
next${UNSECTION_ID}:
!insertmacro UnselectSection "${UNSECTION_ID}"
done${UNSECTION_ID}:
Pop $R0
!macroend
# Uninstaller sections
Section /o un.Server UNSEC0000
ExecWait '"cd" /d $PROGRAMFILES\Tomcat 5.0\bin'
ExecWait '"net" stop XXXX'
ExecWait '"$PROGRAMFILES\Tomcat 5.0\bin\NetRemove.bat" "$PROGRAMFILES\Tomcat 5.0\bin"'
Delete /REBOOTOK $INSTDIR\readme.txt
Delete /REBOOTOK $INSTDIR\install.ico
RmDir /r /REBOOTOK "$PROGRAMFILES\Tomcat 5.0"
DeleteRegValue HKLM "${REGKEY}\Components" Server
SectionEnd
Section /o un.Shortcut UNSEC0001
Delete /REBOOTOK $SMPROGRAMS\$StartMenuGroup\bin.lnk
Delete /REBOOTOK $SMPROGRAMS\$StartMenuGroup\超能系统登录.lnk
Delete /REBOOTOK $SMPROGRAMS\$StartMenuGroup\ReadMe.lnk
RmDir /r /REBOOTOK "$SMPROGRAMS\$StartMenuGroup"
DeleteRegValue HKLM "${REGKEY}\Components" Shortcut
SectionEnd
Section /o un.JDK5 UNSEC0002
RmDir /r /REBOOTOK "$INSTDIR\jdk1.5.0_06"
DeleteRegValue HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
"JAVA_HOME"
DeleteRegValue HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
"TOMCAT_HOME"
SendMessage ${HWND_BROADCAST} ${WM_WININICHANGE} 0 "STR:Environment" /TIMEOUT=0
DeleteRegValue HKLM "${REGKEY}\Components" JDK5
SectionEnd
Section un.post UNSEC0003
DeleteRegKey HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$(^Name)"
Delete /REBOOTOK "$SMPROGRAMS\$StartMenuGroup\$(^UninstallLink).lnk"
Delete /REBOOTOK $INSTDIR\uninstall.exe
DeleteRegValue HKLM "${REGKEY}" Path
DeleteRegKey /IfEmpty HKLM "${REGKEY}\Components"
DeleteRegKey /IfEmpty HKLM "${REGKEY}"
RmDir /REBOOTOK $SMPROGRAMS\$StartMenuGroup
RmDir /REBOOTOK $INSTDIR
SectionEnd
# Installer functions
Function CustomGUIInit
CreateFont $R1 "华文中宋" 15 500 #/NORMAL
IntOp $9 0 + 0
call showBg
CALL repositionWindow
;System::Call "User32::SetWindowPos(i, i, i, i, i, i, i) b ($HWNDPARENT, -1, 0, 0, 0, 0,
${SWP_NOOWNERZORDER}|${SWP_NOSIZE})"
FunctionEnd
Function .onGUIEnd
BGImage::Destroy
FunctionEnd
Function .onInit
InitPluginsDir
StrCpy $StartMenuGroup "超能系统"
Push $R1
File /oname=$PLUGINSDIR\bg.bmp "img\bg.bmp"
File /oname=$PLUGINSDIR\spltmp.bmp img\MainSplash.bmp
advsplash::show 1000 600 400 -1 $PLUGINSDIR\spltmp
Pop $R1
Pop $R1
FunctionEnd
# Uninstaller functions
Function un.onInit
ReadRegStr $INSTDIR HKLM "${REGKEY}" Path
StrCpy $StartMenuGroup "超能系统"
!insertmacro SELECT_UNSECTION Server ${UNSEC0000}
!insertmacro SELECT_UNSECTION Shortcut ${UNSEC0001}
!insertmacro SELECT_UNSECTION JDK5 ${UNSEC0002}
FunctionEnd
Function repositionWindow
;Save existing register values to the stack
Push $0
Push $1
Push $2
Push $3
Push $4
Push $5
Push $6
Push $7
Push $8
; !define SPI_GETWORKAREA 0x0030
; !define SWP_NOSIZE 0x0001
; !define SWP_NOOWNERZORDER 0x0200
; Reposition window in the lower left
; Create RECT struct
System::Call "*${stRECT} .r1"
; Find Window info for the window we're displaying
System::Call "User32::GetWindowRect(i, i) i ($HWNDPARENT, r1) .r2"
; Get left/top/right/bottom
System::Call "*$1${stRECT} (.r2, .r3, .r4, .r5)"
; Calculate width/height of our window
IntOp $2 $4 - $2 ; $2 now contains the width
IntOp $3 $5 - $3 ; $3 now contains the height
; Determine the screen work area
System::Call "User32::SystemParametersInfo(i, i, i, i) i (${SPI_GETWORKAREA}, 0, r1, 0)
.r4"
; Get left/top/right/bottom
System::Call "*$1${stRECT} (.r4, .r5, .r6, .r7)"
System::Free $1
; Right side of screen - window - 10
IntOp $0 $6 - $2
IntOp $0 $0 / 2
; Left side of screen + 10
;IntOp $0 $4 + 10
; Bottom of screen - window - 5
IntOp $1 $7 - $3
IntOp $1 $1 / 2
IntOp $8 $7 / 10
IntOp $1 $1 + $8
System::Call "User32::SetWindowPos(i, i, i, i, i, i, i) b ($HWNDPARENT, -1, $0, $1, 0,
0, ${SWP_NOOWNERZORDER}|${SWP_NOSIZE})"
;Restore register values from the stack
Pop $8
Pop $7
Pop $6
Pop $5
Pop $4
Pop $3
Pop $2
Pop $1
Pop $0
FunctionEnd
Function showBg
BgImage::Clear /NOUNLOAD
BgImage::SetBg /NOUNLOAD /FILLSCREEN $PLUGINSDIR\bg.bmp
${Switch} $9
${Case} 0
BGImage::AddText /NOUNLOAD " 信息1。" $R1 0 0 0 40 130 990 230
${Break}
${Case} 1
;BGImage::AddImage /NOUNLOAD 38 130 $EXEDIR\img\1.bmp
BGImage::AddText /NOUNLOAD " 信息2 " $R1 0 0 0 40 130 990 230
${Break}
${Case} 2
BGImage::AddText /NOUNLOAD "信息3" $R1 0 0 0 200 130 990 230
${Break}
${Case} 3
BGImage::AddText /NOUNLOAD "信息4" $R1 0 0 0 220 150 990 230
${Break}
${Case} 4
BGImage::AddText /NOUNLOAD " 信息5" $R1 0 0 0 180 130 850 230
${Break}
${Case} 5
BGImage::AddText /NOUNLOAD " 信息6" $R1 0 0 0 120 130 880 230
${Break}
${Case} 6
BGImage::AddText /NOUNLOAD "“信息7" $R1 0 0 0 330 170 990 230
${Break}
${Case} 7
BGImage::AddText /NOUNLOAD "信息8" $R1 0 0 0 400 170 990 230
${Break}
${Case} 8
BGImage::AddText /NOUNLOAD "信息9" $R1 0 0 0 300 150 990 230
${Break}
${Case} 9
BGImage::AddText /NOUNLOAD "信息10" $R1 0 0 0 400 170 990 230
${Break}
${EndSwitch}
BGImage::Redraw /NOUNLOAD
FunctionEnd
/*Function WriteEnvStr
Exch $1 ; $1 has environment variable value
Exch
Exch $0 ; $0 has environment variable name
Push $2
Call IsNT
Pop $2
StrCmp $2 1 WriteEnvStr_NT
; Not on NT
StrCpy $2 $WINDIR 2 ; Copy drive of windows (c:)
FileOpen $2 "$2\autoexec.bat" a
FileSeek $2 0 END
FileWrite $2 "$\r$\nSET $0=$1$\r$\n"
FileClose $2
SetRebootFlag true
Goto WriteEnvStr_done
WriteEnvStr_NT:
;WriteRegExpandStr ${WriteEnvStr_RegKey} $0 $1
SendMessage ${HWND_BROADCAST} ${WM_WININICHANGE} \
0 "STR:Environment" /TIMEOUT=5000
WriteEnvStr_done:
Pop $2
Pop $0
Pop $1
FunctionEnd*/
# Installer Language Strings
# TODO Update the Language Strings with the appropriate translations.
LangString ^UninstallLink ${LANG_SIMPCHINESE} "Uninstall $(^Name)"
文章出处:http://scnmutou.blog.163.com/blog/static/193453220086651433225/