Extract an EXE from the binary table and run it from a VBScript Custom Action

Somebody over at ITNinja recently asked how they could run an executable stored in the binary table, from a VBScript Custom Action. Here’s an example I knocked up which describes how to extract an EXE from the binary table and run it from a VBScript Custom Action. I basically streamed notepad.exe into the binary table, and gave it a name of ‘notepad’.  In this Custom Action, I extract it to the %temp% folder and run it from there.  Note that there is no cleanup of the extraction afterwards.  Also note that because this uses the Session object, it can only be executed in the Immediate context.  So more consideration would be required (and probably a separate Custom Action) to execute the extracted EXE in a deferred context.

Rename Single-file Components

Description

This script will rename single-file components to their filename.

Usage

CScript.exe {Script} {MSI}

Script

 

Set Arbitrary Keypaths

Description:

This script will set arbitrary keypaths for components with no keypath.

Usage

CScript.exe {Script} {MSI}

Script

 

Create Short File Names

Description:

This script will create short file names (8.3 format) for the File table and the IniFile table of a Windows Installer.

Usage

CScript.exe {Script} {MSI}

Script

 

Delete Duplicate Registry

Description:

This script will delete duplicate registry entries in the Registry table of a Windows Installer.

Usage

CScript.exe {Script} {MSI}

Script

 

Delete Empty Components

Description:

This script will delete empty components from a Windows Installer.

Usage

CScript.exe {Script} {MSI}

Script

 

Split INI Files into Separate Components

Description:

This script will split INI files into separate components, as per Windows Installer Best Practices.

Usage

CScript.exe {Script} {MSI}

Script

 

Split PE Files into Separate Components

Description:

This script will split PE files into separate components, as per Windows Installer Best Practices.

Usage

CScript.exe {Script} {MSI}

Script

 

Merge Multiple Windows Installer Transforms

Description:

This script will merge multiple Windows Installer transforms into one single transform.

Usage:

CScript.exe {Script} {MSI} {Transform 1} {Transform 2} {Transform x..}
(or if transform ordering is not important, drag an MSI and multiple MSTs onto the VBS file)

Script:

 

 

Tutorial 1: Introduction to using VBScript with MSI and Golden Rules

An MSI (Microsoft Installer) is a relational database that contains multiple tables which reference each other using foreign keys constraints. We can use a simplified form of SQL to extract data from these tables – Windows Installer SQL. Windows Instaler SQL provides a vastly reduced selection of built-in functions and join operations. For example, it doesn’t contain string functions such as ‘Left‘ and ‘Right‘, we can’t use ‘LIKE‘ operators and wildcards for string comparisons, and we’re limited to Inner Joins only.

We have made the ‘MSI Scripting Golden Rules’ below, which highlights common mistakes when using Windows Installer SQL:

MSI Scripting Golden Rules

■  Golden Rule 1: Windows Installer SQL is case-sensitive:

SELECT * FROM Registry is correct.

SELECT * FROM registry is incorrect.

This applies to column names too.

SELECT Key FROM Registry is correct.

SELECT key FROM Registry is incorrect.

■  Golden Rule 2: Be careful when using single quotes and backticks. Single quotes (‘) are used to enclose string comparisons, and backticks () are used to enclose column/table names:

SELECT * FROM Registry WHERE Key = 'SOFTWARE\alkaneTest' is correct.

SELECT * FROM 'Registry' WHERE Key = 'SOFTWARE\alkaneTest' is incorrect.

SELECT * FROM Registry WHERE Key = SOFTWARE\alkaneTest is incorrect.

■  Golden Rule 3: Be careful when using single quotes for comparisons! Use them for STRING comparisons, don't use them for INTEGER comparisons!

SELECT Key FROM Registry WHERE Root = 2 is correct.

SELECT Key FROM Registry WHERE Root` = ‘2’ is incorrect. The Root column is an Integer data type!

■  Golden Rule 4: It is NOT possible to update primary key columns. The only alternative is to use the Modify method and msiViewModifyReplace action.

■  Golden Rule 5: Do not try inserting/updating if you have not opened the database in transact mode!

■  Golden Rule 6: Do not try inserting/updating (Eg, opening in transact mode) an MSI when the MSI is open (Eg, in Orca)!

■  Golden Rule 7: When you insert/update colums, make sure you set values for nullable fields!

■  Golden Rule 8: If you’re creating an object in a Custom Action, do NOT use the WScript directive! Use CreateObject, not WScript.CreateObject!

Dim oInstaller : Set oInstaller = CreateObject(“WindowsInstaller.Installer”) is correct.

Dim oInstaller : Set oInstaller = WScript.CreateObject(“WindowsInstaller.Installer”) is incorrect.

■  Golden Rule 9: If you’re not sure about table names, column names and data types consult the online SDK or your local copy of MSI.chm

■  Golden Rule 10: Always close your View objects after using them (with View.Close()) and dispose of Record, Database and Installer objects by setting them to Nothing.