views:

59

answers:

1

I'd like to build an MSI installer that installs a managed DLL and makes it accessible to users from Visual Studio's "Add References" menu in Solution Explorer. I believe I am supposed to add a key into the Windows registry but I cannot see how to tell the VS project for the MSI to do this at install.

How is this done and where should I look for tutorials on this kind of thing?

+1  A: 

Go to the following registry key:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\AssemblyFolders

Add a subkey (the name of which doesn't matter, but might as well be descriptive) whose default value is the folder containing your assemblies, and you should be good to go.

How to add the required key using WiX

This is a stripped-down fragment of WiX code for creating the required key and setting its default value to the folder in which your assemblies were installed. This is not a complete WiX installer and cannot be pasted directly -- I have removed Guid attributes and shown only the Directory, Component and Feature declarations. Once you've seen some complete WiX code it should be reasonably clear how to merge these bits in.

<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"&gt;
  <!-- Path from WiX file to the DLL(s) you're installing -->
  <?define BuildPath="..\Bin\Release" ?>
  <!-- The base key path - note no HKLM -->
  <?define AssemblyFolders = "Software\Microsoft\.NETFramework\AssemblyFolders" ?>

  <Product>
    <Directory Id="TARGETDIR" Name="SourceDir">
      <Directory Id="ProgramFilesFolder">
        <Directory Id="CompanyFolder" Name="YourCompany">
          <Directory Id="INSTALLLOCATION" Name="Your Product">
            <!-- Id="AssemblyFolder" captures the location where the DLL gets installed -->
            <Directory Id="AssemblyFolder" Name="Bin">
              <Component Id="AssemblyComponent">
                <!-- This is the bit that copies the assembly file -->
                <File Id="YourDll.dll" Checksum="yes" Source="$(var.BuildPath)YourDll.dll" />
              </Component>
              <Component Id="ControlRegistrationComponent">
                <!-- This is the bit that creates the registry key -->
                <RegistryKey Action="createAndRemoveOnUninstall" Root="HKLM" Key="$(var.AssemblyFolders)\Your Product Name">
                  <!-- Square brackets result in a reference -- so the install folder gets picked up -->
                  <RegistryValue Action="write" Value="[AssemblyFolder]" Type="string" />
                </RegistryKey>
              </Component>
            </Directory>
          </Directory>
        </Directory>
      </Directory>
    </Directory>

    <Feature Id="PRODUCTROOTFEATURE">
      <!-- This causes the assembly to be installed -->
      <ComponentRef Id="AssemblyComponent" />
      <!-- This causes the registry key to be created -->
      <ComponentRef Id="ControlRegistrationComponent" />
    </Feature>

  </Product>
</Wix>

To build the MSI once you've got a full WiX script, you can use a batch file, the core of which looks like this:

candle.exe "%WXS_NAME%.wxs" -out "%WXS_NAME%.wixobj"
light.exe "%WXS_NAME%.wixobj" -out "%WXS_NAME%.msi"

where %WXS_NAME% is the name of the WiX .wxs source file.

(I realise this probably looks a bit cryptic but (a) there are lots of WiX samples, tutorials and reference material out there to help you get started and (b) the Votive add-in for Visual Studio may shield you from some of the guts anyway.)

To automatically build the MSI, use the Votive add-in (part of WiX), or a post-build step, or a MSBuild build script. We use a build script because we usually want to build the MSI only as part of our automated build -- doing it in a post-build step would slow down development builds -- but your mileage may vary.

itowlson
I have edited the registry on the target machine, added the Microsoft, .NETFramework and AssemblyFolders to HKEY_LOCAL_MACHINE\SOFTWARE and then added a string in there with the installation location hardcoded in it (should I use an environment variable) but it still doesn't work: the DLL is not listed in Add References. Any idea what I'm doing wrong?
Jon Harrop
Don't forget you need to create a key (in regedit, a folder) *under* AssemblyFolders, and set the default value of that *subkey*. Take a look at the registry on your machine to see examples. And I'm a bit surprised you're having to create the base keys -- they should already be there (especially `HKLM\SOFTWARE\Microsoft`!). Please don't take this the wrong way, but are you sure you're adding the keys in the right place?
itowlson
Another location that contributes to the Add References dialog is `HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\v2.0.50727\AssemblyFoldersEx`.
itowlson
I see what you mean about the subkey from the registry on my machine but I also see that nothing is being added by my MSI at all. I created a "Setup project" from the MS template, went to its solution explorer, clicked the "Registry Editor" icon, browsed to HKEY_LOCAL_MACHINE\SOFTWARE but then it lists only "[Manufacturer]" and not "Microsoft". How am I supposed to get my MSI project to add registry keys? Also, shouldn't these registry keys be functions of the installed location and not hardcoded?
Jon Harrop
Re how to add registry keys: I can give you the answer in WiX, but I'm not familiar with VS Setup projects to help with those. Sorry! Re "shouldn't these [be] the installed location and not hardcoded" -- yes, they should. Again, I don't know how VS Setup projects represent the installed location, but I'd be happy to provide WiX code for this if that would be useful.
itowlson
The Setup project that Microsoft sells as part of VS seems to be crap. I'll take a look at WiX, thanks for the reference. How would you do this with WiX? Can I make it build an installer as a post-build part of the DLL?
Jon Harrop
Jon, I've edited the answer to include fragmentary WiX code, and added some basic info about your options for building the installer. But WiX is a big topic: you'll probably need to read up a bit on WiX before it will make sense. Good luck!
itowlson