home *** CD-ROM | disk | FTP | other *** search
/ ftp2.jacobs.com / 2015.02.ftp2.jacobs.com.tar / ftp2.jacobs.com / pub / MicrosoftUAG / UAG2010_ClientSetup.msi / Binary.MsiBinary < prev    next >
Text File  |  2014-06-26  |  9KB  |  241 lines

  1. '================================================================
  2. ' Initialize global variables
  3. '================================================================
  4. Option Explicit
  5. On Error Resume Next
  6. 'Findout the execution environment MSI or WSH(for debugging)
  7. Dim MsiMode, Test
  8. Err.Clear
  9. Test = Session.Property("ComputerName")
  10. If Err.number = 0 Then
  11.     MsiMode = True
  12.     LogInfo ScriptEngine & " Msi Mode"
  13. Else
  14.     MsiMode = False
  15.     Err.Clear
  16.     LogInfo ScriptEngine & " Cscript Mode"
  17. ' Test area - Insert here function names to be executed in debug session
  18. '             running from command prompt with CSript
  19. End If
  20.  
  21. '================================================================
  22. ' Define constants
  23. '================================================================
  24. Const CustomActionSuccess = 1
  25. Const CustomActionFail    = 2
  26.  
  27. Const msiOpenDatabaseModeReadOnly = 0
  28. Const msiReadStreamAnsi           = 2   'msiReadStreamBytes fails - ON SOME BYTES ONLY!
  29. Const msiReadStreamBytes          = 1
  30. Const msiReadStreamDirect         = 3                
  31.  
  32.  
  33. '=========================================================================
  34. Function ExtractBinary()
  35.     On Error Resume Next
  36.     Dim BinaryKey, NewFileName
  37.     BinaryKey = Session.Property("BinaryKey") ' Get BinaryKey to extract
  38.     NewFileName = Session.Property("TempFolder") & Session.Property("ExtractedFileName") '
  39.     ExtractBinary = CustomActionFail 'assume failure
  40.     LogInfo "ExtractBinary(""" & BinaryKey & """, """ & NewFileName & """)"
  41.  
  42.     '--- Open the database -----------------------------------------------
  43.     LogInfo "Opening """ & NewFileName & """"
  44.     Dim DB
  45.     DB = Session.Property("DATABASE")
  46.     Dim oDB
  47.     Set oDB = session.installer.OpenDatabase(DB, msiOpenDatabaseModeReadOnly)
  48.     If (1 = HandleErr()) Then
  49.         Exit Function
  50.     End If
  51.     LogInfo "Successfully opened """ & NewFileName & """"
  52.  
  53.     '--- Look for the BINARY ---------------------------------------------
  54.     LogInfo "Looking for the binary """ & BinaryKey & """"
  55.     Dim oView, oRec
  56.     Set oView = oDB.OpenView("SELECT `Data` FROM `Binary` WHERE `Name`='" & BinaryKey & "'")
  57.     oView.Execute
  58.     Set oRec = oView.Fetch
  59.     If oRec Is Nothing Then
  60.         LogInfo "ExtractBinary(): Could not find the binary """ & BinaryKey & """"
  61.         Exit Function
  62.     End If
  63.     LogInfo "Found the binary """ & BinaryKey & """"
  64.  
  65.     '--- Extract the Binary ----------------------------------------------
  66.     LogInfo "Extracting the binary """ & BinaryKey & """"
  67.     Dim FileSize, FileContents, OutStream
  68.     Const adTypeBinary = 1
  69.     Const adTypeText = 2
  70.     Const adSaveCreateOverWrite = 2
  71.  
  72.     FileSize = oRec.DataSize(1)
  73.     FileContents = oRec.ReadStream(1, FileSize, msiReadStreamDirect)
  74.     oView.Close
  75.     Redim ByteArray(FileSize)
  76.     Set OutStream = CreateObject("ADODB.Stream")
  77.     OutStream.Type = adTypeBinary
  78.     OutStream.Open
  79.     'Temporary stream to convert variant bytes from String() to Byte() type.
  80.     With CreateObject("ADODB.Stream")
  81.         .Type = adTypeText
  82.         .Open
  83.         .WriteText FileContents
  84.         .Position = 2
  85.         .CopyTo OutStream
  86.         .Close
  87.     End With
  88.  
  89.     OutStream.SaveToFile NewFileName, adSaveCreateOverWrite
  90.     OutStream.Close
  91.  
  92.     If  (1 = HandleErr()) Then
  93.         LogInfo "ExtractBinary(): Tried to extract the binary """ & BinaryKey & """"
  94.     End If
  95.     Set oRec  = Nothing
  96.     Set oView = Nothing
  97.     Set OutStream = Nothing
  98.     If  (1 = HandleErr()) Then
  99.         LogInfo "ExtractBinary(): Failed creating the file """ & NewFileName & """"
  100.     End If
  101.     LogInfo "Successfully created the file """ & NewFileName & """"
  102.     '--- Finished -----------------------------------------------------
  103.     LogInfo "Binary """ & BinaryKey & """ extracted!"
  104.     ExtractBinary = CustomActionSuccess
  105. End Function
  106.  
  107.  
  108. Function CleanUp()
  109.     On Error Resume Next
  110.     CleanUp = CustomActionFail 'assume failure
  111.  
  112.     Dim objFS ,FolderPath, FilePath
  113.     Set objFS = CreateObject("Scripting.FileSystemObject")
  114.     
  115.     '-- Remove extracted CAB file ----------------  
  116.     FilePath = Session.Property("TempFolder") & "ClientSetup.cab" 
  117.     If (objFS.FileExists(FilePath)) Then
  118.         objFS.DeleteFile FilePath, true
  119.         If  (1 = HandleErr()) Then
  120.             LogInfo "CleanUp(): Failed deleting the file """ & FilePath & """"
  121.         Else
  122.             LogInfo "Successfully deleted the file """ & FilePath & """"
  123.         End If
  124.     End If
  125.     
  126.     '-- Remove extract.exe file ----------------  
  127.     FilePath = Session.Property("TempFolder") & "extract.exe" 
  128.     If (objFS.FileExists(FilePath)) Then
  129.         objFS.DeleteFile FilePath, true
  130.         If  (1 = HandleErr()) Then
  131.             LogInfo "CleanUp(): Failed deleting the file """ & FilePath & """"
  132.         Else
  133.             LogInfo "Successfully deleted the file """ & FilePath & """"
  134.         End If
  135.     End If
  136.  
  137. '    '-- Remove folder of extracted setup files ----------------
  138. '    FolderPath = Session.Property("TempFolder") & "ClientSetup"
  139. '    If (objFS.FolderExists(FolderPath)) Then
  140. '        objFS.DeleteFolder FolderPath, true
  141. '        If  (1 = HandleErr()) Then
  142. '            LogInfo "CleanUp(): Failed deleting the folder """ & FolderPath & """"
  143. '        Else
  144. '            LogInfo "Successfully deleted the folder """ & FolderPath & """"
  145. '        End If
  146. '    End If
  147.     
  148.     CleanUp = CustomActionSuccess  
  149.   
  150. End Function 
  151.  
  152. '================================================================
  153. ' Execute Quietly a command line passed via property
  154. '================================================================
  155. Function QuietExec()
  156.     On Error Resume Next
  157.     Dim objShell, strCommand, intResult
  158.  
  159.     QuietExec = CustomActionFail 'assume failure
  160.  
  161.     strCommand = Session.Property("CommandLine")
  162.     LogInfo "Running: " & strCommand     
  163.     Set objShell = CreateObject ("WSCript.shell")
  164.     If (1 = Err.Number <> 0) Then
  165.             HandleErr
  166.         Exit Function
  167.     End If
  168.     intResult = objShell.run(strCommand,7,True)
  169.         If  (1 = HandleErr()) Then
  170.             ReportError "Error Trying To run command: " & strCommand
  171.         Exit Function
  172.     End If
  173.     If (intResult <> 0) Then
  174.         ReportError "Command proccess failed."
  175.         Exit Function
  176.     End If
  177.     QuietExec = CustomActionSuccess
  178.     LogInfo "Command successfully completed."
  179.     Set objShell = Nothing    
  180. End Function
  181.  
  182.  
  183. '================================================================
  184. ' Log and error handling functions
  185. '================================================================
  186.  
  187. '================================================================
  188. 'Write string message to log/window
  189. '================================================================
  190. Function LogInfo(sMsg) 
  191.     On Error Resume Next
  192.     Dim sRec
  193.     If MsiMode Then
  194.         Set sRec = Session.Installer.CreateRecord(1)
  195.         sRec.StringData(0) = "CA:   " & sMsg
  196.         LogInfo = Session.Message(&H04000000, sRec)
  197.     Else
  198.         Wscript.Echo sMsg
  199.     End If
  200. End Function
  201.  
  202. '================================================================
  203. 'Check for error condition. Print err info
  204. '================================================================
  205. Function HandleError(bClear)
  206.     On Error Resume Next
  207.     If Err.number = 0 Then
  208.         HandleError = 0
  209.     Else
  210.         HandleError = 1
  211.         LogInfo "Error # " & Hex(Err.Number) & _
  212.                 " Description: " & Err.Description & _
  213.                 " Source: " & Err.Source
  214.         If bclear Then 'Clear error state in enabled
  215.             Err.Clear
  216.         End If
  217.     End If
  218. End Function
  219.  
  220. '================================================================
  221. 'Check error condition and clear
  222. '================================================================
  223. Function HandleErr()
  224.     HandleErr = HandleError(True)
  225. End Function
  226.  
  227. '================================================================
  228. 'Check error condition and keep unchanged
  229. '================================================================
  230. Function HandleErrUnchanged()
  231.     HandleErrUnchanged = HandleError(False)
  232. End Function
  233. '================================================================
  234. ' Write string message to log/window,
  235. ' Check error condition and clear
  236. '================================================================
  237. Function ReportError(strMsg)
  238.         ReportError = HandleErr()
  239.         LogInfo strMsg
  240. End Function
  241.