NSIS學習筆記(轉)

轉自:http://blog.csdn.net/lee353086/article/details/45919901html

NSIS學習筆記
Date:2015-05-20
Author:kagula
Env:VS2013Update四、nsis-2.46.5-Unicode-setup.exe、CoolSoft_NSISDialogDesigner_1.4.0、eclipse-jee-kepler-SR2-win3二、Win7-64bits


設置NSIS環境
Step1:(編譯NSIS腳本)

從http://www.scratchpaper.com/網站下載「nsis-2.46.5-Unicode-setup.exe」文件並安裝。


Step2:(NSIS腳本語法高亮)
參考資料[3]爲Eclipse安裝NSIS插件,用來編譯NSIS腳本。
[Eclipse main menu]->[Help]->[Install new software...]->type the nsis http address and select component to install.
這裏要注意的是
 [1]Eclipse NSIS插件不支持JDK1.8或更高版本。不支持Windows8或更高版本。
 [2]要先建一個空的project,而後經過嚮導添加NSIS Script會報錯。錯誤信息爲空。
 [3]經過嚮導添加Install Options文件(ini)文件git

 輸出文件名必須爲[/"項目名"/"你文件的事名字"]這種形式,不然會報輸出文件名非法的提示。github

[4]你可能經過outline子窗口,快速定位NSI腳本中的變量與函數。數據庫

如今你能夠直接把nsi文件拖入Eclipse中編輯了。
最新版本(不穩定版)能夠直接從下面網址下載
https://github.com/henrikor2/eclipsensis


Step3:(自定義GUI)
參考資料[1]下載"CoolSoft_NSISDialogDesigner_1.4.0.exe",咱們須要這個工具來自定義安裝界面。




最簡單的流程是
第一步:先在EclipseNSIS(或其它NSIS腳本編輯器)裏把腳本寫好.
第二步:啓動NSIS compiler。[NSIS Menu]->[Compiler]->[compiler NSI scripts]。
第三步:把nsi文件,拖到NSIS compiler裏,NSIS compiler會自動編譯,setup.exe的生成。


要使用NSIS首先得學會使用它的腳本語言,NSIS腳本的每一行表明命令,源文件擴展名爲nsi,頭文件擴展名爲.nsh。


一個軟件可能有不少組件組成,NSIS用section表明每一個組件。示例代碼以下:
Section "My Program"
  SetOutPath $INSTDIR
  File "My Program.exe"
  File "Readme.txt"
SectionEnd


下面是一個典型的NSIS示例代碼,除了自定義頁面風格,該有的都有了。app

# All the other settings can be tweaked by editing the !defines at the top of this script  
!define APPNAME       "比價系統"  
!define COMPANYNAME   "浙江天下商邦科技股份有限公司"  
!define SETUPFILENAME "setup.exe"  
!define DESCRIPTION   ""  
  
!define APPEXENAME    "cat8637_priceComparingSystem.exe"  
  
# These three must be integers  
# 這裏定義的是安裝包的版本,應該和當前主程序(EXE文件)的版本一致  
!define VERSIONMAJOR    2  
!define VERSIONMINOR    0  
!define VERSIONBUILD    0  
!define VERSIONREVISION 0  
  
#定義當前軟件(EXE文件)的版本  
!define VERSIONLONG  "${VERSIONMAJOR}.${VERSIONMINOR}.${VERSIONBUILD}.${VERSIONREVISION}"  
  
#安裝程序ico文件名和位置  
!define ICOFILENAME "cat8637_brand2.ico"  
!define ICOFULLPATH "..\PriceComparingSystem\res\${ICOFILENAME}"  
  
# These will be displayed by the "Click here for support information" link in "Add/Remove Programs"  
# It is possible to use "mailto:" links in here to open the email client  
!define HELPURL   "http://www.8637.com/" # "Support Information" link  
!define UPDATEURL "http://www.8637.com/" # "Product Updates" link  
!define ABOUTURL  "http://www.8637.com/" # "Publisher" link  
  
# This is the size (in kB) of all the files copied into "Program Files"  
# issue 目錄的大小  
!define INSTALLSIZE 84379885  
   
RequestExecutionLevel admin ;Require admin rights on NT6+ (When UAC is turned on)  
    
InstallDir "$PROGRAMFILES\${COMPANYNAME}\${APPNAME}"  
  
# rtf or txt file - remember if it is txt, it must be in the DOS text format (\r\n)  
  
#MUI macro define  
!define MUI_PAGE_HEADER_TEXT ${COMPANYNAME}  
!define MUI_PAGE_HEADER_SUBTEXT '${APPNAME} v${VERSIONLONG}'  
!define MUI_ICON ${ICOFULLPATH}  
  
# This will be in the installer/uninstaller's title bar  
Name    "${APPNAME}"  
Icon    "${ICOFULLPATH}"  
outFile "${SETUPFILENAME}"  
;BrandingText '${APPNAME} v${VERSIONLONG}'  
 BrandingText '$0'  
   
!include LogicLib.nsh  
!include nsProcess.nsh  
  
#include custom page reference  
!include "MUI2.nsh"  
!include "nsDialogs.nsh"  
!include "kagulaWelcomePage.nsdinc"  
   
# Just three pages - license agreement, install location, and installation  
#!insertmacro MUI_PAGE_WELCOME  
;Page custom fnc_kagulaWelcomePage_Show  
;page license  
;page directory  
;Page instfiles  
!insertmacro MUI_PAGE_LICENSE "license.rtf"  
!insertmacro MUI_PAGE_DIRECTORY  
!insertmacro MUI_PAGE_INSTFILES  
  
!insertmacro MUI_UNPAGE_CONFIRM  
!insertmacro MUI_UNPAGE_INSTFILES  
!insertmacro MUI_UNPAGE_FINISH  
  
  
!insertmacro MUI_LANGUAGE "SimpChinese"  
  
!macro VerifyUserIsAdmin  
UserInfo::GetAccountType  
pop $0  
${If} $0 != "admin" ;Require admin rights on NT4+  
        messageBox mb_iconstop "須要管理員權限!"  
        setErrorLevel 740 ;ERROR_ELEVATION_REQUIRED  
        quit  
${EndIf}  
!macroend  
   
!include "FileFunc.nsh"  
Var VersionNumber  
  
Function VerCheck  
pop $0  
;${GetFileVersion} "$INSTDIR\${APPEXENAME}" $VersionNumber  
${GetFileVersion} "$0" $VersionNumber  
FunctionEnd  
  
Function VersionCompare  
    !define VersionCompare `!insertmacro VersionCompareCall`  
   
    !macro VersionCompareCall _VER1 _VER2 _RESULT  
        Push `${_VER1}`  
        Push `${_VER2}`  
        Call VersionCompare  
        Pop ${_RESULT}  
    !macroend  
   
    Exch $1  
    Exch  
    Exch $0  
    Exch  
    Push $2  
    Push $3  
    Push $4  
    Push $5  
    Push $6  
    Push $7  
   
    begin:  
    StrCpy $2 -1  
    IntOp $2 $2 + 1  
    StrCpy $3 $0 1 $2  
    StrCmp $3 '' +2  
    StrCmp $3 '.' 0 -3  
    StrCpy $4 $0 $2  
    IntOp $2 $2 + 1  
    StrCpy $0 $0 '' $2  
   
    StrCpy $2 -1  
    IntOp $2 $2 + 1  
    StrCpy $3 $1 1 $2  
    StrCmp $3 '' +2  
    StrCmp $3 '.' 0 -3  
    StrCpy $5 $1 $2  
    IntOp $2 $2 + 1  
    StrCpy $1 $1 '' $2  
   
    StrCmp $4$5 '' equal  
   
    StrCpy $6 -1  
    IntOp $6 $6 + 1  
    StrCpy $3 $4 1 $6  
    StrCmp $3 '0' -2  
    StrCmp $3 '' 0 +2  
    StrCpy $4 0  
   
    StrCpy $7 -1  
    IntOp $7 $7 + 1  
    StrCpy $3 $5 1 $7  
    StrCmp $3 '0' -2  
    StrCmp $3 '' 0 +2  
    StrCpy $5 0  
   
    StrCmp $4 0 0 +2  
    StrCmp $5 0 begin newer2  
    StrCmp $5 0 newer1  
    IntCmp $6 $7 0 newer1 newer2  
   
    StrCpy $4 '1$4'  
    StrCpy $5 '1$5'  
    IntCmp $4 $5 begin newer2 newer1  
   
    equal:  
    StrCpy $0 0  
    goto end  
    newer1:  
    StrCpy $0 1  
    goto end  
    newer2:  
    StrCpy $0 2  
   
    end:  
    Pop $7  
    Pop $6  
    Pop $5  
    Pop $4  
    Pop $3  
    Pop $2  
    Pop $1  
    Exch $0  
FunctionEnd  
  
function .onInit  
    setShellVarContext all  
    !insertmacro VerifyUserIsAdmin  
      
    #check program running.  
    ${nsProcess::FindProcess} ${APPEXENAME} $R0  
    ${If} $R0 == "0"  
        # it's running  
        MessageBox MB_OK "軟件正在運行,按肯定退出!"  
        Quit  
    ${EndIf}  
      
;    MessageBox MB_OK "當前軟件的版本爲:${VERSIONLONG}"  
    #check version from custom install place      
    ReadRegStr $0 HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "MainProgramLocation"  
    ${If} $0 != ""  
;      MessageBox MB_OK "文件位置:$0"  
      ${If} ${FileExists} $0  
;        #if installed version is greater, return 2          
;        #else if equality, return 0.  
;        #else if less, return 1 and resume install process.  
;        #MessageBox MB_OK "Version=$R0"        
;       MessageBox MB_OK "before push the parameter for VerCheck function.$0"  
       push $0  
       Call VerCheck  
       ${VersionCompare} $VersionNumber ${VERSIONLONG} $R0  
       ${if} $R0 != "1"  
           MessageBox MB_OK "你已經安裝同版本或較新版本${APPNAME}軟件,按肯定退出安裝!"  
           Quit   
        ${Endif}  
      ${EndIf}  
;    ${Else}  
;      MessageBox MB_ICONSTOP "Not found"  
    ${EndIf}  
functionEnd  
   
section "install"  
    # Files for the install directory - to build the installer, these should be in the same directory as the install script (this file)  
    setOutPath $INSTDIR  
      
    # Files added here should be removed by the uninstaller (see section "uninstall")  
    File /r "..\issue\"  
    file "${ICOFULLPATH}"  
    # Add any other files for the install directory (license files, app data, etc) here  
   
    # Uninstaller - See function un.onInit and section "uninstall" for configuration  
    writeUninstaller "$INSTDIR\uninstall.exe"  
   
    # Start Menu  
    createDirectory "$SMPROGRAMS\${COMPANYNAME}"  
    createShortCut "$SMPROGRAMS\${COMPANYNAME}\${APPNAME}.lnk" "$INSTDIR\${APPEXENAME}" "" "$INSTDIR\${ICOFILENAME}"  
      
    # create a shortcut named "new shortcut" in the start menu programs directory  
    # point the new shortcut at the program uninstaller  
    CreateShortCut "$SMPROGRAMS\${COMPANYNAME}\卸載.lnk" "$INSTDIR\uninstall.exe"  
   
    # Registry information for add/remove programs  
    WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "DisplayName" "${COMPANYNAME} - ${APPNAME} - ${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\${ICOFILENAME}$\""  
    WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "Publisher" "$\"${COMPANYNAME}$\""  
    WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "HelpLink" "$\"${HELPURL}$\""  
    WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "URLUpdateInfo" "$\"${UPDATEURL}$\""  
    WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "URLInfoAbout" "$\"${ABOUTURL}$\""  
    WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "DisplayVersion" "$\"${VERSIONMAJOR}.${VERSIONMINOR}.${VERSIONBUILD}.${VERSIONREVISION}$\""  
    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}  
      
    #Write file install location to register table.  
    WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}" "MainProgramLocation" "$INSTDIR\${APPEXENAME}"  
sectionEnd  
   
# Uninstaller  
   
function un.onInit  
    SetShellVarContext all   
   
    #Verify the uninstaller - last chance to back out  
    MessageBox MB_OKCANCEL "肯定要移除 ${APPNAME} 嗎?" IDOK next  
        Abort  
    next:  
    !insertmacro VerifyUserIsAdmin  
functionEnd  
  
section "uninstall"  
   
    # Remove Start Menu launcher  
    delete "$SMPROGRAMS\${COMPANYNAME}\${APPNAME}.lnk"  
    delete "$SMPROGRAMS\${COMPANYNAME}\卸載.lnk"  
      
    # Try to remove the Start Menu folder - this will only happen if it is empty  
    rmDir "$SMPROGRAMS\${COMPANYNAME}"  
   
    # Remove files  
    delete $INSTDIR\${ICOFILENAME}  
   
    # Always delete uninstaller as the last action  
    delete $INSTDIR\uninstall.exe  
   
    # Try to remove the install directory - this will only happen if it is empty  
    rmDir /r $INSTDIR  
   
    # Remove uninstaller information from the registry  
    DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${COMPANYNAME} ${APPNAME}"  
sectionEnd
View Code

Q一、中文問題
參考資料[1]裏的nsis-3.0b1-setup.exe是不支持中文的,得下載nsis-2.46.5-Unicode-setup.exe。
源文件設爲utf-8編碼類型。
採用下面的代碼測試
     CreateShortCut "$SMPROGRAMS\中文測試.lnk" "$INSTDIR\uninstall.exe"
     MessageBox MB_OK  "中文測試" 
是能夠正常顯示中文的。


參考下面的代碼段,把安裝界面弄成中文
!include "MUI2.nsh"
..................
!insertmacro MUI_LANGUAGE "SimpChinese"


Q二、如何測試進程已經運行?
A:須要四個步驟。
第一步:從「http://nsis.sourceforge.net/NsProcess_plugin」下載nsProcess_1_6.7z
第二步:覆蓋NSIS Unicode安裝目錄的目錄樹
第三步:把當前plugin目錄中的nsProcessW.dll文件複製到Plugins目錄並重命名爲nsProcess.dll。
第四步:參數下面的代碼修改便可
    ${nsProcess::FindProcess} "calc.exe" $R0
    ${If} $R0 == "0"
        # it's running
        MessageBox MB_OK "程序已經運行,按肯定退出!"
        Quit
    ${EndIf}


Q三、如何使用NSISDialogDesigner
A1:
使用NSISDialogDesigner生成kagulaWelcomePage.nsdinc文件
在你的nsi主文件裏,專門放Page命令的地方,添加下面的代碼
!include "MUI2.nsh"
!include "nsDialogs.nsh"
!include "kagulaWelcomePage.nsdinc"


Page custom fnc_kagulaWelcomePage_Show
就會顯示你自定義的頁面,不過你會發現它是在frame裏面。


Q四、關於Modern User Interface同NSIS傳統的區別
顯示license頁面,MUI採用下面這行命令:
LicenseData "license.rtf"
page license
MUI方式採用下面這個命令。
!insertmacro MUI_PAGE_LICENSE "license.rtf"less

 

Q5 如何實現高壓縮比eclipse

SetCompressor lzma編輯器

 

Q6 NSIS 打包 win7 中沒法刪除快捷方式

http://www.cnblogs.com/08shiyan/archive/2011/01/10/1931766.htmlide

 

參數資料
[1]NSIS Dialog Designer
http://coolsoft.altervista.org/en/nsisdialogdesigner#download
[2]NSIS home page
http://nsis.sourceforge.net/Main_Page
[3]Eclipse NSIS
http://eclipsensis.sourceforge.net/index.shtml
[4]Auto-uninstall old before installing new
http://nsis.sourceforge.net/Auto-uninstall_old_before_installing_new
[5]NSIS常見問題集錦 推薦新手參考學習
http://www.jb51.net/softjc/33522.html
[6]Unicode NSIS調用nsProcess插件報錯
http://tunps.com/nsis-unicode-nsprocess-error
[7]NSIS detection of a 32-bit process in Win 7 x64
http://stackoverflow.com/questions/5353666/nsis-detection-of-a-32-bit-process-in-win-7-x64
[8]NSIS Modern User Interface
http://nsis.sourceforge.net/Docs/Modern%20UI/Readme.html#examples
[9]Customizing an exsisting NSIS MUI2 page
http://stackoverflow.com/questions/6531115/customizing-an-exsisting-nsis-mui2-page
[10]A sample script that uses several cool functions (replace txt, mutually exclusive functions, MUI, patch install, etc.)
http://nsis.sourceforge.net/A_sample_script_that_uses_several_cool_functions_(replace_txt,_mutually_exclusive_functions,_MUI,_patch_install,_etc.)
[11]NSIS 自定義安裝界面準確獲取安裝進度完美解決方案
http://blog.csdn.net/shuijing_0/article/details/8291299
[12]DB 數據庫地址及路徑配置函數
http://www.cnblogs.com/freeliver54/archive/2010/11/26/1888902.html
[13]NSIS軟件安裝完成界面添加「設置主頁」代碼
http://www.veryhuo.com/a/view/40444.html函數

相關文章
相關標籤/搜索