Saving a Wix checkbox value that defaults to check

2019-06-17 10:02发布

I have an installer authored with Wix. In the UI wizard, there's a checkbox that defaults to checked. I want to save the value of this checkbox to the registry for changes, repairs and upgrades using the (simpler version of the) "Remember Property" pattern described by Rob Mensching.

The checkbox implementation:

<Control Id="httpsCheckBox" Type="CheckBox" CheckBoxValue="true" X="30" Y="119" Width="139" Height="17" Text="Enable HTTPS services" Property="ENABLEHTTPS" />

The property definition:

    <Property Id="ENABLEHTTPS" value="true">
        <RegistrySearch Id="EnableHttpsRegistrySearch" Type="raw" Root="HKLM" Key="SOFTWARE\CompanyName\ProductName" Name="EnableHttps" />
    </Property>

And the property is written to the registry here:

    <Component Id="RegistryEntries">
        <RegistryKey Root="HKLM" Key="SOFTWARE\CompanyName\ProductName">
            <RegistryValue Name="EnableHttps" Value="[ENABLEHTTPS]" Type="string" />
        </RegistryKey>
    </Component>

The initial install works fine. The value in the registry is "true" if the checkbox is left checked or empty if it's unchecked.

The next time the installer is run, to install a new feature for example, the checkbox is always checked regardless of the value in the registry setting.

If I remove the default value from the property definition so that the checkbox is unchecked the first time the installer is run, everything works fine. The next time the installer is run the checkbox (and property) have the correct value from the registry.

It's like the RegistrySearch does not set the property if the registry value is empty.

Am I doing something wrong? Or is there a better way of doing this?

2条回答
Juvenile、少年°
2楼-- · 2019-06-17 10:38

Here's an example that also works with a property that also respects if the default property value is modified via an MSI Transform (MST), the command line or if the value already exists in the registry after being set via Group Policy (i.e. every edge case I could find!)

<Property Id='MYPROP' Secure="yes" Admin="yes" Value='-1'>
    <RegistrySearch Id='RegSearch_MYPROP' Root="HKLM"
                    Key="SOFTWARE\CompanyName\ProductName" 
                    Name='MYPROP' Type='raw' />
</Property>

<CustomAction Id='MYPROPSaveCmdLine' Property='CMDLINE_MYPROP' 
              Value='[MYPROP]' Execute='firstSequence' />
<CustomAction Id='MYPROPSetFromCmdLine' Property='MYPROP'
              Value='[CMDLINE_MYPROP]' Execute='firstSequence' />
<CustomAction Id='MYPROPClearCheckbox' Property='MYPROP'
              Value='{}' Execute='firstSequence'/>
<CustomAction Id='MYPROPSaveCheckboxOff' Property='MYPROP' Value='0' />
<CustomAction Id='MYPROPSaveCheckboxOn' Property='MYPROP' Value='1' />

<InstallUISequence>
    <Custom Action='MYPROPSaveCmdLine' Before='AppSearch'>MYPROP &lt;&gt; -1</Custom>
    <Custom Action='MYPROPSetFromCmdLine' After='AppSearch'>CMDLINE_MYPROP</Custom>
    <Custom Action='MYPROPClearCheckbox' After ='MYPROPSetFromCmdLine'>MYPROP=0</Custom>
</InstallUISequence>
<InstallExecuteSequence>
    <Custom Action='MYPROPSaveCmdLine' Before='AppSearch'>MYPROP &lt;&gt; -1</Custom>
    <Custom Action='MYPROPSetFromCmdLine' After='AppSearch'>CMDLINE_MYPROP</Custom>
    <Custom Action='MYPROPClearCheckbox' After ='MYPROPSetFromCmdLine'>MYPROP=0</Custom>
    <Custom Action='MYPROPSaveCheckboxOff' Before='InstallInitialize'>Not MYPROP Or MYPROP=0</Custom>
    <Custom Action='MYPROPSaveCheckboxOn' Before='InstallInitialize'>MYPROP And MYPROP &lt;&gt; 0</Custom>
</InstallExecuteSequence>
查看更多
劳资没心,怎么记你
3楼-- · 2019-06-17 10:50

Basically, the element will use the default value if the registry entry is not found or null, and that is what you are experiencing.

See the documentation here: http://wix.sourceforge.net/manual-wix3/wix_xsd_registrysearch.htm

Here is a solution to the problem: http://www.mail-archive.com/wix-users@lists.sourceforge.net/msg32524.html

    <Property Id="ENABLEHTTPS" >
         <RegistrySearch Id="EnableHttpsRegistrySearch" Type="raw" Root="HKLM" Key="SOFTWARE\CompanyName\ProductName" Name="EnableHttps" />
    </Property>

    <CustomAction Id="SetENABLEHTTPS" Property="ENABLEHTTPS" Value="1" Execute="firstSequence" />

    <Control Id="httpsCheckBox" Type="CheckBox" CheckBoxValue="1" X="30" Y="119" Width="139" Height="17" Text="Enable HTTPS services" Property="ENABLEHTTPS" />

    <InstallUISequence>
        <Custom Action="SetENABLEHTTPS" Before="AppSearch">NOT Installed AND NOT OLDERVERSIONDETECTED</Custom>
    </InstallUISequence>
    <InstallExecuteSequence>
        <Custom Action="SetENABLEHTTPS" Before="AppSearch">NOT Installed AND NOT OLDERVERSIONDETECTED</Custom>
    </InstallExecuteSequence> 
查看更多
登录 后发表回答