WiX是Windows Installer XML的簡稱,它是用於製作Windows安裝包的工具集。它支持命令行環境,開發者可以及將它集成到他們的編譯過程中創建MSI和MSM安裝包。
更多信息可以參考:http://wix.sourceforge.net/
最近研究了一下如何使用WiX製作具有時間限制的安裝包,下圖是demo的效果圖。
完成這個demo主要有兩點值得注意:
1. 選擇合適的腳本語言(VBScript or JScript)實現定製操作。
2. 選擇合適的實際執行上面的腳本。
下面分享我的實現過程:
1. 實現一段檢查當前時間是否越界的腳本代碼,並將它封裝到CustomAction中。
使用VBScript比較當前日期和限定日期,小於0表示越界,這時調用WScript想註冊表添加壞鍵。
- <CustomAction Id="ValidateTimeLimitCA" Script="vbscript" Execute="immediate" >
- <![CDATA[
- If DateDiff("d", Date, #2011/07/31#) < 0 Then
- Set WShell = CreateObject("WScript.Shell")
- WShell.RegWrite "HKCU\Software\GrapeCity\TimeLimit\DateDiff", 1, "REG_DWORD"
- Set WShell = Nothing
- End If
- ]]>
- </CustomAction>
2. 選擇合適的時機執行上述腳本,越早越好。
在本文的demo中,我在PrepareDlg之前執行腳本,對於大多數情況而言,這已經是最早的時機了。當然,你也可以選擇在AppSearch之前執行。
- <InstallUISequence>
- <Custom Action="ValidateTimeLimitCA" Before="PrepareDlg" />
- </InstallUISequence>
3. 添加一個RegistrySearch屬性,用於第一步提到的壞鍵。
- <Property Id="TIMELIMIT">
- <RegistrySearch Id="TIMELIMIT_VALUE" Root="HKCU" Key="Software\GrapeCity\TimeLimit" Name="DateDiff" Type="raw" />
- </Property>
4. 添加Condition用於檢查第三步添加的屬性是否存在,並在需要的時候彈出錯誤信息。
- <Condition Message="This is package is out of date at July 31, 2011">
- Installed OR NOT TIMELIMIT
- </Condition>
5. 實現一個清除操作,用於清除第一步添加的壞鍵。
- <CustomAction Id="CleanupTimeLimitCA" Script="vbscript" Execute="immediate" >
- <![CDATA[
- Set WShell = CreateObject("WScript.Shell")
- WShell.RegDelete "HKCU\Software\GrapeCity\TimeLimit\"
- Set WShell = Nothing
- >
- </CustomAction>
6. 在FatalError對話框中發佈Finish事件,同時將值設置給第五步提到的清除操作。
- <UI>
- <UIRef Id="WixUI_Minimal" />
- <Publish Dialog="FatalError" Control="Finish" Event="DoAction" Value="CleanupTimeLimitCA">1</Publish>
- </UI>
到這裏就完成,代碼很簡單,希望有價值。
源碼下載地址如下: