2010-01-19 92 views
1

在WiX中,我有一個vbScript用於自定義操作,它將返回網絡打印機的ListItems。我想在安裝時使用這些ListItems來填充ComboBox,因爲在開始安裝之前我不會知道用戶系統上的打印機名稱。WiX動態填充組合框

這裏是vbScript。它目前輸出到一個文本文件,等待如何使用它來回答我的問題。

Const ForWriting = 2 

Set objNetwork = CreateObject("Wscript.Network") 

strName = objNetwork.UserName 
strDomain = objNetwork.UserDomain 
strUser = strDomain & "\" & strName 

strText = "" 

strComputer = "." 

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") 

Set colPrinters = objWMIService.ExecQuery _ 
    ("Select * From Win32_Printer Where Local = FALSE") 

For Each objPrinter in colPrinters 
    strText = strText & "<ListItem Text=""" & objPrinter.Name &""" Value="""& objPrinter.Name &"""/>" & vbcrlf 
Next 

Set objFSO = CreateObject("Scripting.FileSystemObject") 

Set objFile = objFSO.CreateTextFile _ 
    ("C:\Scripts\Printers.txt", ForWriting, True) 

objFile.Write strText 

objFile.Close 

這是輸出:

<ListItem Text="\\xfiles\Canon iR3030 PCL6" Value="\\xfiles\Canon iR3030 PCL6"/> 
<ListItem Text="\\xfiles\HP2110" Value="\\xfiles\HP2110"/> 

我希望能夠利用這個輸出作爲listItems中爲我的組合框。

<Control Type="ComboBox" Property="cboPrinters_Prop" Id="cboPrinters" Width="206" Height="16" X="19" Y="139" ComboList="yes"> 
    <ComboBox Property="cboPrinters_Prop"> 
     <ListItem Text="" Value=""/> 
    </ComboBox> 
</Control> 

如果有更好的方法或我處理這個完全錯誤的(我一直想嘗試像一個開發者)請隨時指正。我厚皮膚... :)

回答

3

正如我懷疑我的原始文章的結尾有一個不同的方式,我是錯誤的。至少有點。作爲WiX的新手,我沒有考慮到值應該添加到數據庫中的事實。鑑於我沒有遇到這個問題,但根據我的經驗,我學到了一些新東西。

因此,這裏是我做了什麼,以獲得值我組合框顯示:

1)上面的腳本已修改爲使用收集到合適的值發送到指定的組合框的數據庫表。


2.)使用Orca我添加了ComboBox表。順便說一句,它只是持有ListItems使用的信息,等待它...組合框。


3.)添加了相應的自定義操作。


4.)在窗體加載之前調用它。

這裏的VB腳本:

Const ERROR_SUCCESS = 0 
Const ERROR_INSTALL_FAILURE = 1603 
Const msiViewModifyInsertTemporary = 7 

Function LogInfo(msg) 
    Dim rec 
    Set rec = Session.Installer.CreateRecord(1) 
    rec.StringData(0) = msg 
    LogInfo = Session.Message(&H04000000, rec) 
End Function 



Function GetNetworkPrinters() 
Dim oView, oReccombo 
Dim r 

LogInfo "INSIDE GetNetworkPrinters" 
Set objNetwork = CreateObject("Wscript.Network") 

strName = objNetwork.UserName 
strDomain = objNetwork.UserDomain 
strUser = strDomain & "\" & strName 

strText = "" 

strComputer = "." 

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") 

Set colPrinters = objWMIService.ExecQuery _ 
    ("Select * From Win32_Printer Where Local = FALSE") 


Set oView = Session.Database.OpenView("SELECT * FROM `ComboBox`") 
oView.Execute 
r = 1 
For Each objPrinter in colPrinters 
r = r + 1 

LogInfo "THE PRINTER NAME IS " & objPrinter.Name 

Set oReccombo = Session.Installer.CreateRecord(4) 
oReccombo.StringData(1) = "cboPrinters_Prop" 
oReccombo.IntegerData(2) = r 
oReccombo.StringData(3) = objPrinter.Name 
oReccombo.StringData(4) = objPrinter.Name 
LogInfo "Made it to the call to insert the record" 
oView.Modify msiViewModifyInsertTemporary, oReccombo 

Next 
oView.Close 

GetNetworkPrinters = ERROR_SUCCESS 

Set oView = Nothing 
End Function 

添加一個二進制項:

<Binary Id="GetNetworkPrinters" SourceFile="*Enter the full path to the script here* \GetNetworkPrinters.vbs" /> 

添加自定義操作:

<CustomAction Id="AddPrintersToComboBox" BinaryKey="GetNetworkPrinters" VBScriptCall="GetNetworkPrinters" Execute="immediate" Return="check" HideTarget="no" Impersonate="yes" /> 

呼叫添加到自定義操作中InstallUISequence和AdminUISequence:

<Custom Action="AddPrintersToComboBox" Before="MaintenanceForm"></Custom> 

就是這樣......當然,腳本需要清理以產生更好的日誌信息,並且還需要更好的(某些)錯誤處理,但它確實有效。

我希望這可以幫助其他人...