共用方式為


WIX: Search for Install path from registry

Technorati Tags: .Net,Windows Installer

Hi There,

I am Syam Pinnaka, Dev in IAM services team at Microsoft.

Windows Installer XML (WIX) is a great way to create windows installer packages. WIX makes it easy to create simple installer packages and its also possible to create an installer of any complexity with WIX. Today I would like to share a simple trick to set the installer path from a registry entry.

WIX has ‘RegistrySearch’ element which makes it possible to search for a registry element. After we find the required registry entry, we can set this path as a property value which can then be referenced as the install directory during the install.

Here is the code snippet to search the registry.

     <Property Id="INSTALLFOLDER">
      <RegistrySearch Id='InstallPathRegistry' Type='raw'
        Root='HKLM' Key='SOFTWARE\Microsoft\my software' Name='InstallPath' Win64='no'/><!--Win64='yes'-->
    </Property>

In the above code, HKLM is the root to start the search. ‘SOFTWARE\Microsoft\my software’ is the key that’s searched. ‘InstallPath’ is the name of the subkey that’s being searched. Use Win64 value ‘No’ to search on x32 bit systems. Win64 value ‘Yes’ can be used for x64 bit systems.

It’s a common scenario to fail the installer if lets say the install path is not found. We can accomplish this using the below XML.

 <Condition Message=''xxx' Install Folder not found'>INSTALLFOLDER</Condition>

Once after we find the correct install path, we would like to use it during the install. This can be achieved using the below XML.

         <Directory Id="TARGETDIR" Name="SourceDir">
                <Directory Id="INSTALLFOLDER" Name="My software" />
        </Directory>

Assuming that you have two .dlls to copy to the user computer and one registry entry to add, here is a sample XML which can be used with WIX.

 <?xml version="1.0" encoding="UTF-8"?>
<?define Version="01.01.00" ?>

<Wix xmlns="https://schemas.microsoft.com/wix/2006/wi">
    <Product Id="*" Name="My software" Language="1033" Version="$(var.Version)" Manufacturer="Awesome Corporation" 
 UpgradeCode="[[your guid here]]">
        <Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine" Platform="x86"/> <!--Platform="x64"-->

    <MajorUpgrade DowngradeErrorMessage="A newer version of 'My software' is already installed." />
        <MediaTemplate EmbedCab="yes" CabinetTemplate="media{0}.cab"/>

    <Property Id="INSTALLFOLDER">
      <RegistrySearch Id='InstallPathRegistry' Type='raw'
        Root='HKLM' Key='SOFTWARE\Microsoft\my software' Name='InstallPath' Win64='no'/><!--Win64='yes'-->
    </Property>
    <Condition Message=''xxx' Install Folder not found'>INSTALLFOLDER</Condition>   
   
    <Feature Id="my feature1" Title="my feature1" Level="1">
      <ComponentGroupRef Id="feature1" />
    </Feature>
    
  </Product>

    <Fragment>
        <Directory Id="TARGETDIR" Name="SourceDir">
                <Directory Id="INSTALLFOLDER" Name="my application" />
        </Directory>
    </Fragment>

  <Fragment>
    <ComponentGroup Id="MyApplicationBin" Directory="INSTALLFOLDER">
      <Component Id="MyDll1" Guid="YOUR GUID1 HERE">
        <File Id="mydll1.dll" KeyPath="yes" Source="Binaries\mydll1.dll" />
      </Component>
      <Component Id="MyDll2" Guid="YOUR GUID2 HERE">
        <File Id="mydll2.dll" KeyPath="yes" Source="Binaries\mydll2.dll" />
      </Component>
      <Component Id="MyApplicationRegistryEntries" Guid="YOUR GUID3 HERE" Win64="no"><!--Win64="yes"-->
        <RegistryKey Root="HKLM"
                      Key='SOFTWARE\Microsoft\my application\[[your guid here]]'
              Action="createAndRemoveOnUninstall">
          <RegistryValue Type="string" Name="BinaryName" Value="mydll1.dll"/>
        </RegistryKey>
      </Component>
    </ComponentGroup>
  </Fragment>
  
</Wix>

 

Please note that the above XML is just to illustrate as to how the complete WIX XML will look like for this scenario. The above code is not tested so it may not work as desired if you try to use it as is.

Thanks for reading and happy installing.

Comments

  • Anonymous
    June 29, 2013
    Please remove the valid GUIDs in your post.

  • Anonymous
    January 20, 2015
    Hi, Thanks for your post. But if I want to get the installed software version id of an external software,how can I do so? Suppose I want to check the installed apache server location through WIX. HKEY_LOCAL_MACHINE->Wow6432Node->Apache Software Foundation ->Apache ->2.2.25 And under this folder there is a registry key name ServerRoot and I need the value of that Server Root. Now the question is I don't know that which version of Apache has been installed in my system. Then how can I read the ServerRoot in this case ?

  • Anonymous
    March 15, 2015
    Wow, this really saved my time :) Thank you.