Useful WIX Snippets

This is an old post and doesn't necessarily reflect my current thinking on a topic, and some links or images may not work. The text is preserved here for posterity.

On a recent WPF project, I spent a little time setting up our installer using WiX. Here's a useful collection of WIX configuration settings that I want to hang on to.

Product tag

Every WiX project has a Product tag. Here's an example:

<Product 
  Id="*" 
  Name="My Application" 
  Language="1033" 
  Version="1.0.0" 
  Manufacturer="You" 
  UpgradeCode="e0848881-1930-4d96-945b-fa8ad2813978"
  >

Some things to note about the various parts:

  • The Id attribute should change each time you build the MSI - the asterisk means it will be auto-generated
  • The Version attribute should probably come from your CI server
  • The UpgradeCode is used to determine whether the product was previously installed - once you set this, don't ever change it.

Ensure the .NET Framework is installed

This snippet checks whether .NET Framework 4.0 (full, not client profile) is installed, and if not, tells the user to go and install it.

<PropertyRef Id="NETFRAMEWORK40FULL"/>
<Condition Message="This application requires Microsoft .NET Framework 4.0 Runtime in order to run. Please install the .NET Framework and then run this installer again.">
  <![CDATA[Installed OR NETFRAMEWORK40FULL]]>
</Condition>

More information on the properties is available in the WixNetFxExtension documentation.

WixUI resources

If you are using WixUI, there are some properties you can override.

<UIRef Id="WixUI_Minimal" />
<UIRef Id="WixUI_ErrorProgressText" />
<WixVariable Id="WixUILicenseRtf" Value="Resources\License.rtf" />
<WixVariable Id="WixUIBannerBmp" Value="Resources\Banner.bmp" />
<WixVariable Id="WixUIDialogBmp" Value="Resources\Dialog.bmp" />
<WixVariable Id="WixUIExclamationIco" Value="Resources\Warning.ico" />
<WixVariable Id="WixUIInfoIco" Value="Resources\Information.ico" />

For the current build of WixUI, Banner.bmp should be 493x58, and Dialog.bmp should be 500x314.

Auto-uninstall previous versions

Suppose you release version 2.0 of your product. Here's a snipper to uninstall all previous versions upon installing 2.0:

<Upgrade Id="e0848881-1930-4d96-945b-fa8ad2813978">
  <UpgradeVersion
    Property="PREVIOUSFOUND"
    Minimum="0.0.0.0" IncludeMinimum="yes"
    Maximum="2.0.0.0" IncludeMaximum="yes"
    />
</Upgrade>
<InstallExecuteSequence>
  <RemoveExistingProducts After="InstallInitialize"/>
</InstallExecuteSequence>

Note that if you release a new build (i.e., Product/@Id changes, but Product/@Version doesn't) of 2.0.0.0, this snippet will also uninstall that.

It's important that Upgrade/@Id matches Product/@UpgradeCode.

Event source

If you hope to write to the event log, you'll need to be an administrator to create the event source. The installer is the best time to do this. Here's an example:

<DirectoryRef Id="TARGETDIR">
  <Component Id="RegistryEntries" Guid="8C29D953-4D25-4EE0-A7F2-A9B34C9C65EF">
    <util:EventSource
      Name="Training Kiosk"
      Log="Application"
      EventMessageFile="[NETFRAMEWORK40FULLINSTALLROOTDIR]EventLogMessages.dll"
      />
  </Component>
</DirectoryRef>

Run the application when installer finishes

After you install your WPF application, it's nice to run it automatically. Here's how:

<Property Id="WixShellExecTarget" Value="[#YourEXEFile]" />
<CustomAction 
  Id="LaunchApplication"
  BinaryKey="WixCA"
  DllEntry="WixShellExec"
  Impersonate="yes" 
  />

<UI>
  <Publish 
    Dialog="ExitDialog"
    Control="Finish"
    Event="DoAction"
    Value="LaunchApplication">NOT Installed</Publish>
</UI>