'Make Wix to not uninstall common dll

I have Wix project in which I need a common used dll library to be installed if it's absent. If this dll exists I should not overwrite it. So, when I set DefaultVersion="0.0.0.0" this dll is not overwritten if it exists, its ok. But when I delete app, the dll is beeing removed. How do I prevent removing dll in the case when it existed before installation? I don't want to make it permanent because it should be removed if it didn't exist before installation.

<Component Id="myLib.dll" Permanent="no"  Directory="Shared_Dir">
            <File Name="myLib.dll" KeyPath="yes" 
                  Source="mySource\myLib.dll"
                  DefaultVersion="0.0.0.0"
                  />
wix


Solution 1:[1]

  1. Add reference to WixUtilExtension and xmlns:util="http://schemas.microsoft.com/wix/UtilExtension" attribute to Wix element in your code.

  2. Define <Property Id="Dll_Installed" Value="false"/> in Product element.

  3. Add child <Condition>NOT Dll_Installed</Condition> to component myLib.dll.

  4. Add that somewhere in your code:

     <Fragment>
     <util:FileSearch
               Id="Dll_Installed"
               Variable="Dll_Installed"
               Path="[Shared_Dir]myLib.dll"
               Result="exists"/>
     </Fragment>
    

DefaultVersion attribute is not necessary.

Solution 2:[2]

The feature you are describing is reference counting. The Windows Installer reference counts with Components. Components are identified by their GUID.

So the normal way to address this requirement is to put the File in a Component and make sure the GUID of the Component is stable. The WiX Toolset should do exactly that automatically if if you do not specify the Component/@Guid attribute.

So the default behavior should just work for you.

The only other piece of the puzzle is the Windows Installer will install the latest version of a file. If the file version is the same or less the file will not be installed but will be reference counted.

Based on the details in the question it seems like you should be just fine with:

<Component Directory="Shared_Dir">
 <File Source="mySource\myLib.dll" />
</Component>

One might ask why the Windows Installer use Components to reference count files. We'll, it allows you to group other resources, like registry keys, together and control their install as a unit. Much more important if you are installing COM servers than plain old files.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kirumata
Solution 2 Rob Mensching