Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
4.5 KiB

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