Extracting WiX Bundled MSI Files Without Running The Installer

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.

Here’s an index into the articles in this 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:

  <Fragment>
    <Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="ProgramFilesFolder">
        <Directory Id="APPLICATIONFOLDER" Name="MyCompany">
          <Directory Id="ProductFolder" Name="Demo Product Installer">
            <Directory Id="Cmp1Folder" Name="Component1" />
            <Directory Id="Cmp2Folder" Name="Component2">
                <Directory Id="Cmp3Folder" Name="Component3" />
            </Directory>
          </Directory>
        </Directory>
      </Directory>
    </Directory>
  </Fragment>

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!

step 3: cleanup

We nuke the temp working folder to keep things tidy.

And here's the full source code:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.Deployment.WindowsInstaller;
using Microsoft.Deployment.WindowsInstaller.Package;
using Microsoft.Tools.WindowsInstallerXml;
using Microsoft.Tools.WindowsInstallerXml.Bootstrapper;
using FileAttributes = System.IO.FileAttributes;

/// <summary>
/// Action will extract the files in the provided MSIs into the provided folder.
/// </summary>    
internal class MsiFileExtractor 
{
    private readonly string _bundlePath;
    private readonly string _targetFolder;
    private readonly Dictionary<string, List<string>> _msiAndFeaturesToInstall;
    private readonly BootstrapperApplication _bootstrapperApplication;

    /// <summary>
    /// </summary>
    /// <param name="targetFolder">File path to the parent folder where files should be installed</param>
    /// <param name="msiAndFeaturesToInstall">collection of msi names and the features in each MSI to be installed</param>
    /// <param name="bootstrapperApplication">instance of the MBA</param>
    public MsiFileExtractor(string targetFolder, Dictionary<string, List<string>> msiAndFeaturesToInstall, BootstrapperApplication bootstrapperApplication)           
    {
        _bootstrapperApplication = bootstrapperApplication; 
        _bundlePath = _bootstrapperApplication.Engine.StringVariables["WixBundleOriginalSource"]; 
        _targetFolder = targetFolder;
        _msiAndFeaturesToInstall = msiAndFeaturesToInstall;        
    }

    /// <summary>
    /// Extract the bundle's embedded MSIs, extract the files from each and run 
    /// any custom actions
    /// </summary>        
    public void RunInstall()
    {
        var workingDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

        //step 1: extract MSIs from bundle
        ExtractComponentInstallersFromBundle(_bundlePath, workingDir);

        //step 2: explode MSIs
        foreach (var msiName in _msiAndFeaturesToInstall.Keys)
        {
            var featuresToInclude = _msiAndFeaturesToInstall[msiName];
            InstallPackage(workingDir, msiName, featuresToInclude);
        }

        //step 3: cleanup
        if (Directory.Exists(workingDir))
        {
            var di = new DirectoryInfo(workingDir);
            ClearReadOnly(di);

            Directory.Delete(workingDir, true);
        }

    }

    /// <summary>
    /// Recursively clear the ReadOnly attribute from all files and subfolders
    /// for the given folder.
    /// </summary>
    /// <param name="directory"></param>
    private void ClearReadOnly(DirectoryInfo directory)
    {
        directory.Attributes &= ~FileAttributes.ReadOnly;

        foreach (var file in directory.GetFiles())
        {
            file.Attributes &= ~FileAttributes.ReadOnly;
        }

        foreach (var subDir in directory.GetDirectories())
        {
            ClearReadOnly(subDir);
        }
    }

    /// <summary>
    /// For a single MSI, extract the files and run any custom actions
    /// </summary>
    private void InstallPackage(string workingDir, string msiName, List<string> featuresToInclude)
    {
        var msiPath = Path.Combine(workingDir, "AttachedContainer");
        var msiFilePath = Path.Combine(msiPath, msiName);

        Console.WriteLine("Extracting package: {0}", msiName);

        using (var msiPackage = new InstallPackage(msiFilePath, DatabaseOpenMode.Transact) { WorkingDirectory = _targetFolder })
        {
            UpdateMSIDirectoryTableToUseNewApplicationFolder(msiPackage);

            UpdateMSIFeatureTableToSetInstallableFeatures(msiPackage, featuresToInclude);

            //Set the InternalUI level on the MSI so that it doesn't show when we open the Session object
            var prevUILevel = Microsoft.Deployment.WindowsInstaller.Installer.SetInternalUI(InstallUIOptions.Silent);
            try
            {
                using (var session = Microsoft.Deployment.WindowsInstaller.Installer.OpenPackage(msiPackage, ignoreMachineState: true))
                {

                    SetEngineVarsInSession(session);
                    ExtractPackageFiles(session, msiPackage);

                }
            }
            finally
            {
                Microsoft.Deployment.WindowsInstaller.Installer.SetInternalUI(prevUILevel);
            }

            msiPackage.Close();
        }

    }

    private void ExtractPackageFiles(Session session, InstallPackage msiPackage)
    {

        //using a hashset to ensure distinct list
        var fileKeysToInstall = new HashSet<string>();
        foreach (var feature in session.Features)
        {
            var featureLevel = msiPackage.ExecuteIntegerQuery(
                string.Format("SELECT `Level` FROM `Feature` WHERE `Feature` = '{0}'",
                              feature.Name))
                .First();

            if (featureLevel != 1) continue;

            var featureComponents =
                msiPackage.ExecuteStringQuery(
                    string.Format("SELECT `Component_` FROM `FeatureComponents` WHERE `Feature_` = '{0}'",
                                  feature.Name));

            foreach (var installableComponent in session.Components
                .Where(cp => featureComponents.Contains(cp.Name)))
            {
                var componentFileKeys = msiPackage.ExecuteStringQuery(
                    string.Format("SELECT `File` FROM `File` WHERE `Component_` = '{0}'",
                        installableComponent.Name));

                foreach (var fileKey in componentFileKeys)
                {
                    fileKeysToInstall.Add(fileKey);
                }
            }
            msiPackage.ExtractFiles(fileKeysToInstall);
        }

        //temp folder is left behind, so we need to clean it up
        //see http://sourceforge.net/p/wix/bugs/2330/
        var witempPath = Path.Combine(_targetFolder, "WITEMP");
        if (Directory.Exists(witempPath))
        {
            ClearReadOnly(new DirectoryInfo(witempPath));
            Directory.Delete(witempPath, true);
        }
    }

    /// <summary>
    /// Override the APPLICATIONFOLDER in the MSI database so
    /// that files are installed to the target folder.
    /// </summary>        
    private void UpdateMSIDirectoryTableToUseNewApplicationFolder(InstallPackage msiPackage)
    {
        //Update folders
        msiPackage.WorkingDirectory = _targetFolder;

        //The "APPLICATIONFOLDER" special name is used to denote the application top-level folder.
        //In this case, the user has selected their own top-level folder, so we need to
        //replace the existing value with the "." special value, which denotes the extracted
        //folder.               
        var installFolderName = "APPLICATIONFOLDER";
        if (msiPackage.Directories.ContainsKey(installFolderName))
        {
            var record = new Record(".", installFolderName);
            msiPackage.Execute("UPDATE `Directory` SET `DefaultDir` = ? WHERE `Directory` = ?",
                               record);
        }

        msiPackage.UpdateDirectories();
        msiPackage.Commit();
    }

    private void UpdateMSIFeatureTableToSetInstallableFeatures(InstallPackage database, List<string> featuresToInstall)
    {
        bool installDefault = !featuresToInstall.Any();
        if (installDefault) return;   

        var packageFeatures = new List<string>();

        if (database.Tables.Contains("Feature"))
        {
            packageFeatures.AddRange(database.ExecuteStringQuery("SELECT `Feature` FROM `Feature`"));

            foreach (var feature in packageFeatures)
            {
                //a Level of "1" will be installed by default, anything higher than that will not,
                // so by setting all others to "100", they will not be installed/extracted
                int newInstallLevel = featuresToInstall.Contains(feature) ? 1 : 100;

                Console.WriteLine("Setting feature {0} to install level {1}", feature, newInstallLevel);

                using (var record = new Record(newInstallLevel, feature))
                {
                    database.Execute("UPDATE `Feature` SET `Level` = ? WHERE `Feature` = ?",
                                     record);
                }
            }
        }
        else
        {
            throw new Exception("Feature table not found");
        }
        database.Commit();

    }

    private void SetEngineVarsInSession(Session session)
    {
        var propertiesToCopy = new[] {"PROP1", "PROP2"};
        foreach (var property in propertiesToCopy)
        {
            var propValue = _bootstrapperApplication.Engine.StringVariables[property];
            session[property] = propValue;
        }
    }

    private void ExtractComponentInstallersFromBundle(string bundlePath, string tmpFolder)
    {
        Unbinder unbinder = null;

        try
        {
            unbinder = new Unbinder();
            unbinder.Message += MessageEventHandlerMethod;
            unbinder.Unbind(bundlePath, OutputType.Bundle, tmpFolder);
        }
        finally
        {
            if (null != unbinder)
                unbinder.DeleteTempFiles();
        }

    }

    private void MessageEventHandlerMethod(object sender, MessageEventArgs e)
    {
        Console.WriteLine("MESSAGE: ID: {0}, LEVEL: {1}, RESOURCE: {2}, LINES: {3}",
                   e.Id,
                   e.Level,
                   e.ResourceName,
                   e.SourceLineNumbers);
    }

}