Manage Binary Streams
The VBScript file WiStream.vbs is provided in the Windows SDK Components for Windows Installer Developers. This sample shows how script can be used to manage binary streams in a Windows Installer database. The sample may be used to enter compressed file cabinets into a database. This sample demonstrates the operation of the _Streams table in the Windows Installer database.
The sample also demonstrates the use of:
- OpenDatabase method (Installer Object)
- CreateRecord method
- LastErrorRecord method of the Installer object
- OpenView method
- Commit method of the Database object
- Fetch method
- Modify method
- Execute method of the View object
- StringData property
- SetStream method of the Record object
You'll require the CScript.exe or WScript.exe version of Windows Script Host to use this sample. To use CScript.exe to run this sample, type a command line at the command prompt using the following syntax. Help is displayed if the first argument is /? or if too few arguments are specified. To redirect the output to a file, end the command line with VBS > [path to file]. The sample returns a value of 0 for success, 1 if help is invoked, and 2 if the script fails.
cscript WiStream.vbs [path to database][path to file][options][stream name]
Specify the path to the Windows Installer database that is to receive the stream. Specify a path to the binary file containing the stream data. To list the streams in the installer database, omit this path. You may specify an optional stream name, if this is omitted it defaults to the file name.
The following option may be specified.
| Option | Description |
|---|---|
| no option specified | Add a stream to the Windows Installer database. |
| /d | Remove a stream. This option flag must be followed by the name of the substorage being removed. |
For additional scripting examples, see Windows Installer Scripting Examples. For sample utilities that do not require Windows Script Host, see Windows Installer Development Tools.
Send comments about this topic to Microsoft
Build date: 2/3/2012
' Windows Installer utility to manage binary streams in an installer package
' For use with Windows Scripting Host, CScript.exe or WScript.exe
' Copyright (c) Microsoft Corporation. All rights reserved.
' Demonstrates the use of the database _Streams table
' Used for entering non-database binary streams such as compressed file cabinets
' Streams that persist database binary values should be managed with table views
' Streams that persist database tables and system data are invisible in _Streams
'
Option Explicit
Const msiOpenDatabaseModeReadOnly = 0
Const msiOpenDatabaseModeTransact = 1
Const msiOpenDatabaseModeCreate = 3
Const msiViewModifyInsert = 1
Const msiViewModifyUpdate = 2
Const msiViewModifyAssign = 3
Const msiViewModifyReplace = 4
Const msiViewModifyDelete = 6
Const ForAppending = 8
Const ForReading = 1
Const ForWriting = 2
Const TristateTrue = -1
' Check arg count, and display help if argument not present or contains ?
Dim argCount:argCount = Wscript.Arguments.Count
If argCount > 0 Then If InStr(1, Wscript.Arguments(0), "?", vbTextCompare) > 0 Then argCount = 0
If (argCount = 0) Then
Wscript.Echo "Windows Installer database stream import utility" &_
vbNewLine & " 1st argument is the path to MSI database (installer package)" &_
vbNewLine & " 2nd argument is the path to a file containing the stream data" &_
vbNewLine & " If the 2nd argument is missing, streams will be listed" &_
vbNewLine & " 3rd argument is optional, the name used for the stream" &_
vbNewLine & " If the 3rd arugment is missing, the file name is used" &_
vbNewLine & " To remove a stream, use /D or -D as the 2nd argument" &_
vbNewLine & " followed by the name of the stream in the 3rd argument" &_
vbNewLine &_
vbNewLine & "Copyright (C) Microsoft Corporation. All rights reserved."
Wscript.Quit 1
End If
' Connect to Windows Installer object
On Error Resume Next
Dim installer : Set installer = Nothing
Set installer = Wscript.CreateObject("WindowsInstaller.Installer") : CheckError
' Evaluate command-line arguments and set open and update modes
Dim databasePath:databasePath = Wscript.Arguments(0)
Dim openMode : If argCount = 1 Then openMode = msiOpenDatabaseModeReadOnly Else openMode = msiOpenDatabaseModeTransact
Dim updateMode : If argCount > 1 Then updateMode = msiViewModifyAssign 'Either insert or replace existing row
Dim importPath : If argCount > 1 Then importPath = Wscript.Arguments(1)
Dim streamName : If argCount > 2 Then streamName = Wscript.Arguments(2)
If streamName = Empty And importPath <> Empty Then streamName = Right(importPath, Len(importPath) - InStrRev(importPath, "\",-1,vbTextCompare))
If UCase(importPath) = "/D" Or UCase(importPath) = "-D" Then updateMode = msiViewModifyDelete : importPath = Empty 'Stream will be deleted if no input data
' Open database and create a view on the _Streams table
Dim sqlQuery : Select Case updateMode
Case msiOpenDatabaseModeReadOnly: sqlQuery = "SELECT `Name` FROM _Streams"
Case msiViewModifyAssign: sqlQuery = "SELECT `Name`,`Data` FROM _Streams"
Case msiViewModifyDelete: sqlQuery = "SELECT `Name` FROM _Streams WHERE `Name` = ?"
End Select
Dim database : Set database = installer.OpenDatabase(databasePath, openMode) : CheckError
Dim view : Set view = database.OpenView(sqlQuery)
Dim record
If openMode = msiOpenDatabaseModeReadOnly Then 'If listing streams, simply fetch all records
Dim message, name
view.Execute : CheckError
Do
Set record = view.Fetch
If record Is Nothing Then Exit Do
name = record.StringData(1)
If message = Empty Then message = name Else message = message & vbNewLine & name
Loop
Wscript.Echo message
Else 'If adding a stream, insert a row, else if removing a stream, delete the row
Set record = installer.CreateRecord(2)
record.StringData(1) = streamName
view.Execute record : CheckError
If importPath <> Empty Then 'Insert stream - copy data into stream
record.SetStream 2, importPath : CheckError
Else 'Delete stream, fetch first to provide better error message if missing
Set record = view.Fetch
If record Is Nothing Then Wscript.Echo "Stream not present:", streamName : Wscript.Quit 2
End If
view.Modify updateMode, record : CheckError
database.Commit : CheckError
Set view = Nothing
Set database = Nothing
CheckError
End If
Sub CheckError
Dim message, errRec
If Err = 0 Then Exit Sub
message = Err.Source & " " & Hex(Err) & ": " & Err.Description
If Not installer Is Nothing Then
Set errRec = installer.LastErrorRecord
If Not errRec Is Nothing Then message = message & vbNewLine & errRec.FormatText
End If
Wscript.Echo message
Wscript.Quit 2
End Sub
- 7/21/2008
- Thomas Lee
- 9/11/2008
- maihodac