WiX Tutorials – Add shortcuts and registry to MSI

Our MSI is starting to look better and better with each post, and more complex. In the previous posts we have seen how to create a basic MSI and how to add features, components and files to the MSI.

Let’s continue the series and in this tutorial, we will add a shortcut to the Trace32.exe file added in the MSI, and also a HKEY_LOCAL_MACHINE registry in another component.

As we all know, per-machine shortcuts are located in the MSI in the [ProgramMenuFolder]. So let’s create a directory called My First Installer in the Start Menu. After we create the desired directory, we add a new component which contains the shortcut.


<Directory Id="ProgramMenuFolder">
<Directory Id="MyShortcut" Name="My First Installer">
<Component Id="Shortcuts" Guid="11111112-2222-3333-4444-555555555555">
<Shortcut Id="Shortcut1" Name="Trace32" Description="Trace32"

Target="[INSTALLDIR]Trace32.exe" WorkingDirectory="INSTALLDIR"/>
<RegistryValue Root="HKCU" Key="Software\My First Installer"

Name="Installed" Type="integer" Value="1" KeyPath="yes"/>
<RemoveFolder Id="MyShortcut" On="uninstall"/>
</Component>
</Directory>
</Directory>
</Directory>

 

 

As you can see, the shortcut target will point to Trace32.exe and the working directory will be in the [INSTALLDIR]. As a best practice we also add a HKCU registry in the same component.

 

But wait, you said we add a HKLM registry!

Yes, indeed. So let’s create another separate component for this one:


<DirectoryRef Id="TARGETDIR">
<Component Id="Regs">
<RegistryValue Root="HKLM" Key="Software\My First Installer"
Name="Installed" Type="integer" Value="1" KeyPath="yes"/>
</Component>
</DirectoryRef>

 

Wait, what is DirectoryRef?

Well, because we already created the TARGETDIR previously, it is not necessary to create it again. So instead, we use its reference for the destination of the Component. We then create a HKLM REG_DWORD key.

 

In the end, lets not forget to add the two extra components to our Feature:


<Feature Id="Feature1" Level="1" Title="First feature" Description="This is the one and only feature in this installation">
<ComponentRef Id="Files"/>
<ComponentRef Id="Shortcuts"/>
<ComponentRef Id="Regs"/>
</Feature>

 

The full code for the updated example1.wxs is:


<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Product Id="*" UpgradeCode="12345678-1111-2222-3333-666666666666"

Name="My First Installer" Version="1.0.0.0" Manufacturer="My Company" Language="1033">
<Package InstallerVersion="200" Compressed="yes" Comments="Hello, this is my first installer"/>
<Media Id="1" Cabinet="product.cab" EmbedCab="yes"/>

<Directory Id="TARGETDIR" Name="SourceDir">
<Directory Id="ProgramFilesFolder">
<Directory Id="INSTALLDIR" Name="MyFirstInstaller">
<Component Id="Files" Guid="11111111-2222-3333-4444-555555555555">
<File Id="File1" Source="Trace32.exe"/>
</Component>
</Directory>
</Directory>

<Directory Id="ProgramMenuFolder">
<Directory Id="MyShortcut" Name="My First Installer">
<Component Id="Shortcuts" Guid="11111112-2222-3333-4444-555555555555">
<Shortcut Id="Shortcut1" Name="Trace32" Description="Trace32"

Target="[INSTALLDIR]Trace32.exe" WorkingDirectory="INSTALLDIR"/>
<RegistryValue Root="HKCU" Key="Software\My First Installer"

Name="Installed" Type="integer" Value="1" KeyPath="yes"/>
<RemoveFolder Id="MyShortcut" On="uninstall"/>
</Component>
</Directory>
</Directory>
</Directory>

<DirectoryRef Id="TARGETDIR">
<Component Id="Regs">
<RegistryValue Root="HKLM" Key="Software\My First Installer"
Name="Installed" Type="integer" Value="1" KeyPath="yes"/>
</Component>
</DirectoryRef>

<Feature Id="Feature1" Level="1" Title="First feature" Description="This is the one and only feature in this installation">
<ComponentRef Id="Files"/>
<ComponentRef Id="Shortcuts"/>
<ComponentRef Id="Regs"/>
</Feature>
</Product>
</Wix>

 

Sweet, so let’s now double-click our make_msi.bat created previously. The operation should go without errors:

Now let’s open up the MSI in Advanced Installer and see if we have everything ok.

Perfect! Our shortcut is created as desired.

And the components exactly how they should be!

 

That is it for this tutorial.

For more information and documentation about WiX, check out this page.

Leave a comment

Your email address will not be published. Required fields are marked *

twenty + 13 =