This blog entry provides an example of reading a registry value from an MSI database using VBScript.  It follows on from the previous blog post which provided a tutorial on an introduction to using VBScript to manipulate MSI files and the golden rules.

It forms part 2 of an 17-part series that explores how to use VBScript to manipulate MSI relational databases using the Windows Installer API.  Throughout this series of tutorials, we identify the common issues that we encounter and the best practises that we use to overcome them.

Let’s start off with a simple example. Create a file called ‘readRegistry.vbs’, and copy and paste the following code into it. Save it.

'create 2 constants - one for when we want to just query the MSI (read) and one for when we want to make changes (write)

Const msiOpenDatabaseModeReadOnly = 0
Const msiOpenDatabaseModeTransact = 1

'create WindowsInstaller.Installer object
Dim oInstaller : Set oInstaller = CreateObject("WindowsInstaller.Installer")

'open the MSI (the first argument supplied to the vbscript)
Dim oDatabase : Set oDatabase = oInstaller.OpenDatabase(WScript.Arguments(0),msiOpenDatabaseModeReadOnly) 

'create a view of the registry we want to see
Dim sql : sql = "SELECT `Key` FROM `Registry`"
Dim regView : Set regView = oDatabase.OpenView(sql)

'execute the query
regView.Execute 

'fetch the first row of data (if there is one!)
Dim regRecord : Set regRecord = regView.Fetch

'whilst we've returned a row and therefore regRecord is not Nothing
While Not regRecord Is Nothing

	'print out the registry key
	wscript.echo "Registry key is: " & regRecord.StringData(1)

	'go and fetch the next row of data	
	Set regRecord = regView.Fetch
Wend

regView.Close
Set regView = Nothing
Set regRecord = Nothing
Set oDatabase = Nothing
Set oInstaller = Nothing

tipTip: In the above example, we define the SQL query in a variable called ‘sql’. The reason we do this is because some SQL queries can be quite large,
and defining them as a string makes it easier for us to write large queries and pass them into the OpenView method.

Line 11 uses WScript.Arguments(0) – This piece of code represents the first parameter/argument passed to the vbscript at runtime. In our example, it is our test MSI. Here’s how we run it:

Open up a Command Prompt

Type: cscript.exe readRegistry.vbs “c:\alkaneMSIs\testMSI.msi” (we force the script to use the cscript engine so that our output writes to the command window and not dialog boxes)

Running a VBscript from the command line

Running a VBscript from the command line

Of course, in a production script you’ll probably use the FileSystem Object to ensure the file definitely exists, and that it is an MSI file. But that’s getting on to another topic altogether….

You should see after running it that it prints out a list of all of the registry keys contained in your registry table.

Thanks for reading the instruction and examples of reading a registry value from an MSI database using VBScript.  Next you can find out how to insert a registry value in an MSI database using VBScript.

Reading a registry value from an MSI database
Comments have now been disabled. If you have a question to ask about this post please ask the community!