Posts From November 2014

This article is one in my series focusing on what I learned creating a Windows Installer using the WiX toolkit’s "Managed Bootstrapper Application" (Custom .NET UI) framework. Each post builds upon the previous posts, so I would suggest you at least skim through the earlier articles in the series.

You can extract an embedded .msi from your bundle while it's running if you're using a custom bootstrapper application, then extract the contents of that .msi by using the WiX SDK.

Below is a walkthrough of just extracting the files and laying them down on the filesystem. It does not run any of the CustomActions (I'll post how to do that later). I do this all through a class I call MsiFileExtractor - the full source is at the end of this posting, so I'll walk through the methods a little at a time.

First, you need to know a couple of things (which I've put into the cxtr):

The path to the executing bundle file, in which the msi files are embedded. You can get this through the WixBundleOriginalSource engine variable

This list of features to be installed for each MSI. I'm assuming you don't just want to install the defaults and that you want to select which specific features to install.

Using my MsiFileExtractor class, you'll need to pass into the cxtr the target folder where you want the files extracted into, the list of features to be installed for each MSI, and a reference to the MBA object itself. Then RunInstall() and watch the magic happen.

Here's a high-level overview of what happens:

Step 1: extract MSIs from bundle

The ExtractComponentInstallersFromBundle method is used to extract the .msi files from inside the bundle .exe and place them into a temporary working folder. This is actually pretty straightforward using the WiX Unbinder class. This throws the MSI files into a AttachedContainer subfolder of our temp working folder.

step 2: explode MSIs

Now, we loop through each of the MSIs you want to install and use the InstallPackage method to actually extract the files from them into your target folder.

To do this, I used the WiX InstallPackage class, which wraps our MSI package. Keep in mind, the MSI package is basically just a database with the files embedded. So we'll interact with the database using SQL statements in order to change the behavior of the installer.

step 2a: set the install location in the MSI database

The first thing we need to do is update the install folder. In your .wix files, you should have defined a bunch of Directory targets, like this:

Note that I have a top-level folder APPLICATIONFOLDER defined just under the ProgramFilesFolder. All of my files get installed at or below this level. So I need to change the location of this folder in the MSI database to allow me to install somewhere other than Program Files. The way I achieve this is by setting it to "." (the current folder), which will override everything above it in the folder hierarchy as well. I do this in the UpdateMSIDirectoryTableToUseNewApplicationFolder method by changing the DefaultDir value in the Directory table.

step 2b: set the installable features in the MSI database

As I mentioned above, the Level value for each feature determines if it's installed by default of not. Anything that's a Level 1 will be installed. Anything higher will not. So if all you want to install is the Level 1 features, you can skip this step. Otherwise, we need to modify the level values in the MSI's database to change it to 1 for those features we want and to something higher than 1 for the features we don't. I do this in the UpdateMSIFeatureTableToSetInstallableFeatures method by changing the Level value in the Feature table.

step 2c: create a Session

Since we'll basically be using MSI/WiX to "install" these files, and they require a Session to run, we need to create our own session object. This is done using the OpenPackage method -- but we need to tell it to ignoreMachineState, otherwise it will look for this MSI in the system registry and get all confused. I also set the UI level to Silent to prevent the MSI's UI from showing. Make sure to store the old UI level so you can restore it when you're done.

You also need to set the Properties for the session. I do this in SetEngineVarsInSession by copying the properties I want from the Bootstrapper's StringVariable. Modify this as you need.

step 3c: the real work

Now we're ready to actually extract the files, which I do in ExtractPackageFiles. I do this by searching for all of the features in the Feature table with a Level value of 1 (see above about setting that value), then finding all of the entries in the FeatureComponents table for those features, then finding all of the entries in the File table for those components and keeping a list of everything that needs to be installed.

Then, I call InstallPackage.ExtractFiles with that list and BAM I have installed files!