nsis 學習筆記(一)

為了將 web 程式打包成安裝檔,讓 windows 環境也能在不用做任何複雜的 httpd、mysql、php.ini等設定下,系統管理人員就可以使用 icon 一鍵自動安裝程式,並直接就可以開啟瀏覽器,打開系統網站,開始了我的打包生涯,nsis 大概是我第二個接觸打包安裝檔程式的語言,之前是學習 android 打包成 apk 在手機上進行軟體測試,言歸正傳,本篇算是初探,因此我們則不免其俗的先來寫個 hello word! 試試看。

1.請先下載NSIS這套開發工具, 並安裝完成(一直按Next即可),筆者目前是安裝最新版的2.46版本。

2.開啟桌面上的NSIS捷徑, 並點選 [Compile NSI scripts],如下圖所示:

3.在跳出的視窗(MakeNSISW)中, 點選 [Load Script] 按鈕,並載入寫好的hello.nsi



4.確認載入視窗中無任何錯誤訊息,即可點選 [Test Installer] 來測試這個腳本語言的執行檔

5.直接點擊剛剛編譯好的執行檔來看執行結果(產生的執行檔會放置於跟腳本檔案同一目錄下)

以上僅僅只是打包程式的過程,非常簡單,真正的學問還是在程式碼的編寫的部分,如何寫出 hello world?我們可以到 nsis 官網找到程式語法 example,http://nsis.sourceforge.net/Simple_tutorials,有篇範例還不錯,我們先使用 notepad++ 來編程,如下

# declare name of installer file (宣告安裝檔案的名稱)
Outfile "hello world.exe"

# open section (開啟 section)
Section

# create a popup box, with an OK button and some text (新增一個對話視窗及一個ok按鈕與文字)
MessageBox MB_OK "Now We are Creating Hello_world.txt at Desktop!"

/* open an output file called "Hello_world.txt",
on the desktop in write mode. This file does not need to exist
before script is compiled and run */(開啟一個輸出檔案並呼叫"Hello_world.txt"寫在桌面上,如果這個檔案不存在就編譯然後執行)

FileOpen $0 "$DESKTOP\Hello_world.txt" w

# write the string "hello world!" to the output file (寫一個字串"hello world!"到輸出檔)
FileWrite $0 "hello world!"

# close the file(關閉檔案)
FileClose $0
# Show Success message.(秀出成功訊息)
MessageBox MB_OK "Hello_world.txt has been created successfully at Desktop!"


# end the section(結束一個section)
SectionEnd
   
將文件存成 hello.nsi 並開啟 nsis 將 hello.nsi 載入編譯,請參考上面第一步,編譯完成後立即執行安裝程式如下圖
 安裝程式會參照腳本中的語法去新增一個 Hello_world.txt 存放於桌面上


 完成後會在桌面上產生一個 Hello_world.txt 文字檔


 我們還可以在對話視窗訊息中看到 Completed



留言

這個網誌中的熱門文章

c語言-關於#define用法

CMD常用網管指令

PHP 與 JavaScript 之間傳值利用 json