Yet Another WiX Tutorial Part 2: Your First Installer

WiX Introduction

What is in this tutorial

In this part of the WiX tutorial i will guide you trough your first WiX installer and explain what the important concepts are when creating a WiX file.

Requirements for this part of the tutorial is that you installed the WiX toolkit as described in part 1 of this tutorial.

Structure of a WiX script

If we look at the structure of a MSI package you will see some principles that come back in the WiX script.

msi

  • A MSI has one or more features.
  • A feature has one or more components.
  • A component consists of one or more items to be installed.

A component is a group of items and actions that should be installed or executed when that component is selected to be installed. The most common items would be some files and a shortcut , and an action could be to copy a file.

Creating the project

The WiX project

We start by creating a new solution that contains a WiX project. The WiX Project template presents you with the following WiX script.

For a complete WiX schema I recommend going to the manual page of WiX.

  1: <?xml version="1.0" encoding="UTF-8"?>
  2: <Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
  3:   <Product Id="e06d9811-b2f8-4e11-81fc-0e82160dae0e" Name="WixTutorial" Language="1033"
  4:     Version="1.0.0.0" Manufacturer="Iguza.Net" UpgradeCode="cea73ef0-4496-4da4-8608-152baf455fec">
  5:     <Package InstallerVersion="200" Compressed="yes" />
  6: 
  7:     <Media Id="1" Cabinet="media1.cab" EmbedCab="yes" />
  8: 
  9:     <Directory Id="TARGETDIR" Name="SourceDir">
 10:       <Directory Id="ProgramFilesFolder">
 11:         <Directory Id="INSTALLLOCATION" Name="WixTutorial">
 12:           <Component Id="ProductComponent" Guid="b0789ba0-c0bb-49aa-8459-a5fac2da9cf4">
 13:             <!--TODO: Insert files, registry keys, and other resources here.-->
 14:           </Component>
 15:         </Directory>
 16:       </Directory>
 17:     </Directory>
 18: 
 19:     <Feature Id="ProductFeature" Title="WixTutorial ConsoleApp" Level="1">
 20:       <ComponentRef Id="ProductComponent" />
 21:     </Feature>
 22:   </Product>
 23: </Wix>

So what do we see here?.

We start out with a product node, which specifies the properties of out installer.

We skip the <Media> node for now. Second thing we see is a directory structure using <Direcory> nodes which defines the installation folder structure. Within this directory structure we insert <component> element in the location where the components should be installed. In our example we will be adding some files that need to be installed.

Adding Files

I started by adding a new console application project names “WixTutorial.ConsoleApp” and implement some simple code in the mail method.

To add this file to the installer add a <File> node in the component. Every item should have a unique ID. The WiX compiler will actually warn you about this beforehand.

  1: <Component Id="ProductComponent" Guid="b0789ba0-c0bb-49aa-8459-a5fac2da9cf4">
  2:   <File Id="FILE_CONSOLEAPP" Name="WixTutorial.ConsoleApp.exe" 
  3:         DiskId="1" Source="$(var.ConsoleFolder)\WixTutorial.ConsoleApp.exe" />
  4: </Component>

First we introduce a variable called $(var.ConsoleFolder). This will make the script more flexible because we can set the value, for instance during a build process. You could provide this variable as a command line parameter but for now we fill it hard coded. To do this, add the following snippet right after the <Wix> node and replace the <%FOLDER LOCATION%> placeholder with the root folder of your console build folder.

  1: <?define ConsoleFolder = "<%FOLDER LOCATION%>" ?>

We should now be able to build the project. After the build has finished we end up with a MSI file in the output folder of the WiX project.

Running this MSI will finish quickly and it will look like it didn’t do much. But is we look in the install folder “c:\program files\WixTutorial” we will actually see our file. Also, using the “Add/Remove Programs” console, we are able to uninstall the application. The reason for this is that our installer does not contain any definition for installation dialogs and simply installs without user interaction.

Adding Interfaces

To include some user interface we can use one of the precompiled UI sequences that come with the toolkit. For a list of these templates i refer you to a very good article at WiX tutorial.

To get started we need to first add a reference to the UI extensions library to our WiX project using the well know “Add Reference” dialog.

AddUIExtensions

Select the WixUIExtention.dll, click add and then OK.

After this reference has been set, we add the most complete user interface to the WiX script. So add the following as a child of the <Product> node.

  1: <UIRef Id="WixUI_Mondo"/>

Using this one line of code,  the following main dialogs are included:

The following dialogs are also included:

That’s it, we are done.

Now if we repeat the installation we should see actual screens. One of these would be the “Setup Type” selection dialog and when choosing “Custom” the feature selection dialog pops up.

CustomSetupDialog

Some Final Tweaks

This is all pretty neat, but why can’t we set the installation folder?. We need to add one more attribute to our selected feature node.

  1: <Feature Id="ProductFeature" Title="WixTutorial ConsoleApp" Level="1"
  2:          ConfigurableDirectory="INSTALLLOCATION">
  3:   <ComponentRef Id="ProductComponent" />
  4: </Feature>

The ConfigurableDirectory attribute is set to the folder id that we are allowed to change during the installation. If we run the MSI again after the build, we can select the installation folder.

CustomSetupDialogFolderSelect 

What’s in the next part

That’s it. Our first simple installer is finished. Take a look at the actual XML that was needed and you will see that it’s very straight foreword and easy to read.

The next tutorial will go deeper into tweaking the WiX script and working with variables during a build.

Happy Coding.

Comments

Popular posts from this blog

Yet Another WiX Tutorial Part 3: Customizing the UI dialogs

Encrypted Cookies using ASP.NET