home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 June / Chip_2001-06_cd1.bin / zkuste / vbasic / Data / Utility / MSISDK15.msi / WiStream.vbs < prev    next >
Text File  |  2000-10-05  |  5KB  |  105 lines

  1. ' Windows Installer utility to manage binary streams in an installer package
  2. ' For use with Windows Scripting Host, CScript.exe or WScript.exe
  3. ' Copyright (c) 1999-2000, Microsoft Corporation
  4. ' Demonstrates the use of the database _Streams table
  5. ' Used for entering non-database binary streams such as compressed file cabinets
  6. ' Streams that persist database binary values should be managed with table views
  7. ' Streams that persist database tables and system data are invisible in _Streams
  8. '
  9. Option Explicit
  10.  
  11. Const msiOpenDatabaseModeReadOnly     = 0
  12. Const msiOpenDatabaseModeTransact     = 1
  13. Const msiOpenDatabaseModeCreate       = 3
  14.  
  15. Const msiViewModifyInsert         = 1
  16. Const msiViewModifyUpdate         = 2
  17. Const msiViewModifyAssign         = 3
  18. Const msiViewModifyReplace        = 4
  19. Const msiViewModifyDelete         = 6
  20.  
  21. Const ForAppending = 8
  22. Const ForReading = 1
  23. Const ForWriting = 2
  24. Const TristateTrue = -1
  25.  
  26. ' Check arg count, and display help if argument not present or contains ?
  27. Dim argCount:argCount = Wscript.Arguments.Count
  28. If argCount > 0 Then If InStr(1, Wscript.Arguments(0), "?", vbTextCompare) > 0 Then argCount = 0
  29. If (argCount = 0) Then
  30.     Wscript.Echo "Windows Installer database stream import utility" &_
  31.         vbNewLine & " 1st argument is the path to MSI database (installer package)" &_
  32.         vbNewLine & " 2nd argument is the path to a file containing the stream data" &_
  33.         vbNewLine & " If the 2nd argument is missing, streams will be listed" &_
  34.         vbNewLine & " 3rd argument is optional, the name used for the stream" &_
  35.         vbNewLine & " If the 3rd arugment is missing, the file name is used" &_
  36.         vbNewLine & " To remove a stream, use /D or -D as the 2nd argument" &_
  37.         vbNewLine & " followed by the name of the stream in the 3rd argument" &_
  38.         vbNewLine &_
  39.         vbNewLine & "Copyright (C) Microsoft Corporation, 1999-2000.  All rights reserved."
  40.     Wscript.Quit 1
  41. End If
  42.  
  43. ' Connect to Windows Installer object
  44. On Error Resume Next
  45. Dim installer : Set installer = Nothing
  46. Set installer = Wscript.CreateObject("WindowsInstaller.Installer") : CheckError
  47.  
  48. ' Evaluate command-line arguments and set open and update modes
  49. Dim databasePath:databasePath = Wscript.Arguments(0)
  50. Dim openMode    : If argCount = 1 Then openMode = msiOpenDatabaseModeReadOnly Else openMode = msiOpenDatabaseModeTransact
  51. Dim updateMode  : If argCount > 1 Then updateMode = msiViewModifyAssign  'Either insert or replace existing row
  52. Dim importPath  : If argCount > 1 Then importPath = Wscript.Arguments(1)
  53. Dim streamName  : If argCount > 2 Then streamName = Wscript.Arguments(2)
  54. If streamName = Empty And importPath <> Empty Then streamName = Right(importPath, Len(importPath) - InStrRev(importPath, "\",-1,vbTextCompare))
  55. If UCase(importPath) = "/D" Or UCase(importPath) = "-D" Then updateMode = msiViewModifyDelete : importPath = Empty 'Stream will be deleted if no input data
  56.  
  57. ' Open database and create a view on the _Streams table
  58. Dim sqlQuery : Select Case updateMode
  59.     Case msiOpenDatabaseModeReadOnly: sqlQuery = "SELECT `Name` FROM _Streams"
  60.     Case msiViewModifyAssign:         sqlQuery = "SELECT `Name`,`Data` FROM _Streams"
  61.     Case msiViewModifyDelete:         sqlQuery = "SELECT `Name` FROM _Streams WHERE `Name` = ?"
  62. End Select
  63. Dim database : Set database = installer.OpenDatabase(databasePath, openMode) : CheckError
  64. Dim view     : Set view = database.OpenView(sqlQuery)
  65. Dim record
  66.  
  67. If openMode = msiOpenDatabaseModeReadOnly Then 'If listing streams, simply fetch all records
  68.     Dim message, name
  69.     view.Execute : CheckError
  70.     Do
  71.         Set record = view.Fetch
  72.         If record Is Nothing Then Exit Do
  73.         name = record.StringData(1)
  74.         If message = Empty Then message = name Else message = message & vbNewLine & name
  75.     Loop
  76.     Wscript.Echo message
  77. Else 'If adding a stream, insert a row, else if removing a stream, delete the row
  78.     Set record = installer.CreateRecord(2)
  79.     record.StringData(1) = streamName
  80.     view.Execute record : CheckError
  81.     If importPath <> Empty Then  'Insert stream - copy data into stream
  82.         record.SetStream 2, importPath : CheckError
  83.     Else  'Delete stream, fetch first to provide better error message if missing
  84.         Set record = view.Fetch
  85.         If record Is Nothing Then Wscript.Echo "Stream not present:", streamName : Wscript.Quit 2
  86.     End If
  87.     view.Modify updateMode, record : CheckError
  88.     database.Commit : CheckError
  89.     Set view = Nothing
  90.     Set database = Nothing
  91.     CheckError
  92. End If
  93.  
  94. Sub CheckError
  95.     Dim message, errRec
  96.     If Err = 0 Then Exit Sub
  97.     message = Err.Source & " " & Hex(Err) & ": " & Err.Description
  98.     If Not installer Is Nothing Then
  99.         Set errRec = installer.LastErrorRecord
  100.         If Not errRec Is Nothing Then message = message & vbNewLine & errRec.FormatText
  101.     End If
  102.     Wscript.Echo message
  103.     Wscript.Quit 2
  104. End Sub
  105.