Leaked source code of windows server 2003
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.

104 lines
4.7 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-2001, 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. vbNewLine &_
  35. vbNewLine & "Copyright (C) Microsoft Corporation, 1999-2001. All rights reserved."
  36. Wscript.Quit 1
  37. End If
  38. ' Connect to Windows Installer object
  39. On Error Resume Next
  40. Dim installer : Set installer = Nothing
  41. Set installer = Wscript.CreateObject("WindowsInstaller.Installer") : CheckError
  42. ' Evaluate command-line arguments and set open and update modes
  43. Dim databasePath:databasePath = Wscript.Arguments(0)
  44. Dim openMode : If argCount = 1 Then openMode = msiOpenDatabaseModeReadOnly Else openMode = msiOpenDatabaseModeTransact
  45. Dim updateMode : If argCount > 1 Then updateMode = msiViewModifyAssign 'Either insert or replace existing row
  46. Dim importPath : If argCount > 1 Then importPath = Wscript.Arguments(1)
  47. Dim streamName : If argCount > 2 Then streamName = Wscript.Arguments(2)
  48. If streamName = Empty And importPath <> Empty Then streamName = Right(importPath, Len(importPath) - InStrRev(importPath, "\",-1,vbTextCompare))
  49. If UCase(importPath) = "/D" Or UCase(importPath) = "-D" Then updateMode = msiViewModifyDelete : importPath = Empty 'Stream will be deleted if no input data
  50. ' Open database and create a view on the _Streams table
  51. Dim sqlQuery : Select Case updateMode
  52. Case msiOpenDatabaseModeReadOnly: sqlQuery = "SELECT `Name` FROM _Streams"
  53. Case msiViewModifyAssign: sqlQuery = "SELECT `Name`,`Data` FROM _Streams"
  54. Case msiViewModifyDelete: sqlQuery = "SELECT `Name` FROM _Streams WHERE `Name` = ?"
  55. End Select
  56. Dim database : Set database = installer.OpenDatabase(databasePath, openMode) : CheckError
  57. Dim view : Set view = database.OpenView(sqlQuery)
  58. Dim record
  59. If openMode = msiOpenDatabaseModeReadOnly Then 'If listing streams, simply fetch all records
  60. Dim message, name
  61. view.Execute : CheckError
  62. Do
  63. Set record = view.Fetch
  64. If record Is Nothing Then Exit Do
  65. name = record.StringData(1)
  66. If message = Empty Then message = name Else message = message & vbNewLine & name
  67. Loop
  68. Wscript.Echo message
  69. Else 'If adding a stream, insert a row, else if removing a stream, delete the row
  70. Set record = installer.CreateRecord(2)
  71. record.StringData(1) = streamName
  72. view.Execute record : CheckError
  73. If importPath <> Empty Then 'Insert stream - copy data into stream
  74. record.SetStream 2, importPath : CheckError
  75. Else 'Delete stream, fetch first to provide better error message if missing
  76. Set record = view.Fetch
  77. If record Is Nothing Then Wscript.Echo "Stream not present:", streamName : Wscript.Quit 2
  78. End If
  79. view.Modify updateMode, record : CheckError
  80. database.Commit : CheckError
  81. Set view = Nothing
  82. Set database = Nothing
  83. CheckError
  84. End If
  85. Sub CheckError
  86. Dim message, errRec
  87. If Err = 0 Then Exit Sub
  88. message = Err.Source & " " & Hex(Err) & ": " & Err.Description
  89. If Not installer Is Nothing Then
  90. Set errRec = installer.LastErrorRecord
  91. If Not errRec Is Nothing Then message = message & vbNewLine & errRec.FormatText
  92. End If
  93. Wscript.Echo message
  94. Wscript.Quit 2
  95. End Sub