65

So I have this setup project in Wix and wanted to have a shortcut on the desktop. This must be easy you might think. But that is not the case. All the code snippets found on the Internet did not work. After a few hours of struggling and reading the documentation I finally got it right, so I am sharing it with you here.

Stein Åsmul
  • 34,628
  • 23
  • 78
  • 140
Raymond Holmboe
  • 1,813
  • 1
  • 14
  • 15
  • this is the way i feel with so much microsoft "technology" these days. This comment comes almost three years later. so clearly the state of the art has not improved. – Ian Ray Jan 21 '18 at 05:59

6 Answers6

84

The shortcut is a non-advertised one, hope this helps someone. Remember to put the component in your feature tag.

<Directory Id="TARGETDIR" Name="SourceDir">
    <Directory Id="DesktopFolder" Name="Desktop">
        <Component Id="ApplicationShortcutDesktop" Guid="*">
            <Shortcut Id="ApplicationDesktopShortcut"
                Name="Text under your icon"
                Description="Comment field in your shortcut"
                Target="[MYAPPDIRPROPERTY]MyApp.exe"
                WorkingDirectory="MYAPPDIRPROPERTY"/>
            <RemoveFolder Id="DesktopFolder" On="uninstall"/>
            <RegistryValue
                Root="HKCU"
                Key="Software\MyCompany\MyApplicationName"
                Name="installed"
                Type="integer"
                Value="1"
                KeyPath="yes"/>
        </Component>
    </Directory>

    <Directory Id="ProgramFilesFolder" Name="PFiles">
        <Directory Id="MyCompany" Name="MyCompany">
            <Directory Id="MYAPPDIRPROPERTY" Name="MyAppName">
                <!-- main installation files -->
            </Directory>
        </Directory>
    </Directory>
</Directory>
Raymond Holmboe
  • 1,813
  • 1
  • 14
  • 15
  • You need backward slashes on your Registry Key – Stealth Rabbi May 14 '13 at 19:36
  • 11
    Don't post hard coded GUIDs. Put `Guid="*"` instead for Auto-generate guid, or use {PUT-GUID-HERE} – Stein Åsmul Aug 10 '14 at 19:13
  • 5
    what's the registry value for? – BatteryBackupUnit Jun 17 '15 at 11:59
  • 1
    @BatteryBackupUnit A shortcut cannot be a key path for a component, so the registry value serves that purpose. The same technique is used in the WiX documentation: http://wixtoolset.org/documentation/manual/v3/howtos/files_and_registry/create_start_menu_shortcut.html – vangelion Jul 24 '15 at 21:10
  • 3
    It's better to put the file id for the Target property instead of "[MYAPPDIRPROPERTY]MyApp.exe". – Hossein Shahdoost Oct 30 '15 at 09:45
  • Missing Shortcut !! Can you help me with the [MYAPPDIRPROPERTY] ? – Abdulsalam Elsharif Apr 16 '16 at 21:59
  • 4
    I had to add this under feature tag: – uriel Jun 07 '16 at 10:23
  • @HosseinShahdoost Using a file ID for Target fails ICE69, since the shortcut and file are in different components. They can't be in the same component because the shortcut is per user, but the component may be per machine. – Edward Brey May 15 '18 at 19:17
  • 1
    @SteinÅsmul if they use Guid="*" it can cause problems in future upgrades and uninstalls as it enables the possibility of two component guids pointing to the same file location. I'd always push {PUT-GUID-HERE} to prevent future headaches. See this guy: https://stackoverflow.com/questions/43062435/msi-fixing-different-component-guids-for-the-same-file – user922020 Oct 17 '18 at 16:28
  • No, it is the opposite - and this I am actually sure of. Using `Guid="*"` - or [just leaving out the whole attribute as described here](https://stackoverflow.com/a/24769965/129130) - enables automatic component GUIDs that are calculated from the component key path making them stable across releases. The problem occurs for people who generate a new component GUID manually and keep the file install location OR move the component to a different folder **without** creating a new component GUID. [This component GUID complexity is explained here](https://stackoverflow.com/a/1422121/129130). – Stein Åsmul Oct 17 '18 at 17:06
  • 1
    Works great. But I was getting error that `ApplicationShortcutDesktop` is not part on any feature. So don't forget to add `` in `Feature` tag – Riz Apr 19 '19 at 07:49
31

I think my way is easier, no need for you to create a registry key:

<Directory Id="TARGETDIR" Name="SourceDir">
  <Directory Id="DesktopFolder" SourceName="Desktop" />
  <Directory Id="MergeRedirectFolder">
    <Component Id="MyExeComponent" Guid="{PUT-GUID-HERE}">
      <File Id="MyExeFile" Source="$(var.ExeSourcePath)" KeyPath="yes">
        <Shortcut
          Id="DesktopShortcut"
          Directory="DesktopFolder"
          Name="$(var.ShortcutName)"
          WorkingDirectory="MergeRedirectFolder" />
      </File>
    </Component>
  </Directory>
</Directory>
Sean Hall
  • 6,676
  • 2
  • 26
  • 41
  • 1
    From [documentation](http://wixtoolset.org/documentation/manual/v3/howtos/files_and_registry/create_start_menu_shortcut.html) regarding the registry key: _"This is required as a Shortcut cannot serve as the KeyPath for a component when installing non-advertised shortcuts for the current users."_ – blins Aug 04 '17 at 16:33
  • 1
    the file component itself is the keypath, the shortcut "inherits" the file properties. This way could be a little confusing though when you try track down that desktop shortcut in amongst many files.. user preference as to compact versus traceability i think? – Chris Watts Jan 02 '18 at 13:55
  • In most cases, you wouldn't want to set the shortcut's working directory to the directory that the app is installed into. – Edward Brey May 15 '18 at 18:39
  • This answer shows how to create a valid shortcut per Windows Installer documentation. Wix will build a package (MSI) which will contain a Shortcut record with its `Directory_` field referring to the special `DesktopFolder` folder, and the `Target` field carrying value `[#]`. The only downside I see is that both the file and the shortcut are part of a single component, so cannot be installed one without the other. Obviously, it makes no sense to install the shortcut without the target file, but now you can't have a "Place shortcut on the desktop" option either. – amn Mar 18 '20 at 16:23
  • Another downside that I missed is that since the keypath is the file the shortcut links to, reparations of installation won't always behave as expected by user -- if the file is there, a missing shortcut won't be repaired (read: installed), since the component is considered present (the file is in place). – amn Apr 30 '20 at 11:22
16

Thanks for example. In WIX 3.8 it still raises: "Error 3 ICE43: Component ... has non-advertised shortcuts. It should use a registry key under HKCU as its KeyPath, not a file."

So I did this such way in a file with features:

   <Component Id="cmp79F6D61F01DD1060F418A05609A6DA70" 
              Directory="dirBin" Guid="*">
      <File Id="fil34B100315EFE9D878B5C2227CD1454E1" KeyPath="yes"
            Source="$(var.SourceDir)\FARMS.exe" >
        <Shortcut Id="DesktopShortcut"
                  Directory="DesktopFolder"
                  Name="FARMS $(var.FarmsVersion)"
                  Description="Local Land Services desktop application"
                  WorkingDirectory="INSTALLFOLDER"
                  Icon="FARMS.exe"
                  IconIndex="0"
                  Advertise="yes" >
           <Icon Id="FARMS.exe" SourceFile="$(var.SourceDir)\FARMS.exe" />
        </Shortcut>
        </File>
    </Component>

And mentioned desktop folder in a file with product definition:

  <Fragment>
    <Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="DesktopFolder" Name="Desktop" />

      <Directory Id="ProgramFilesFolder">
        <Directory Id="INSTALLFOLDER" Name="FARMS" >
        </Directory>
      </Directory>
    </Directory>
  </Fragment>
Stein Åsmul
  • 34,628
  • 23
  • 78
  • 140
Dimitry
  • 351
  • 1
  • 3
  • 9
7

It seems lot easier in this documentation.

First, you have to point your DesktopFolder,

   <Fragment>
    <Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="DesktopFolder" Name="Desktop"/>

Then you should create Shortcut component for file that you want to create shortcut of.

  <Component Id="PutYourComponentIdHere" Directory="FileDirectory" Guid="*">
    <File Id="NotYourComponentId" KeyPath="yes" Source="..\YourFileSource\YourExecutable.exe">
      <Shortcut Id="desktopServer" Directory="DesktopFolder" Name="YourShourtcutName" WorkingDirectory='WhereShouldYourShortcutPoint' Advertise="yes"/>
    </File>
  </Component>

It worked for me. I need to put icon but thats easy part. Hope it works.

2

After too much effort, I used this way:

<Product ...>
    <Feature Id="ProductFeature" Title="SetupProject" Level="1">
      ...
      ...
      <ComponentRef Id="cmpDesktopShortcut" />
    </Feature>

    <Component Id="cmpDesktopShortcut" Guid="PUT-GUID-HERE" Directory="DesktopFolder" >
        <Shortcut Id="MyDesktopShortcut" 
                  Name="Setup Project" 
                  Description="Opens the program." 
                  Directory="DesktopFolder" 
                  Target="[INSTALLFOLDER]App.exe"
                  WorkingDirectory="INSTALLFOLDER"/>
        <RegistryValue Root="HKCU" Key="Software\My Company\Sample Application" Name="installed" Type="integer" Value="1" KeyPath="yes" />
    </Component>
</Product>
Mohsen
  • 263
  • 3
  • 14
0

I believe that using a "Current User" (HKCU) registry key as Key Path causes problems on a multi-user machine tool. Because the registry key is only created for the current user and when a different user logs in, then the auto-repair of the installation kicks in.