WiX RemoveRegistryKey element not behaving as adve

2019-07-23 07:09发布

I can't seem to delete a registry key when I uninstall. Note that this isn't a repeat of this question, as I don't think that I have the same problem. Or, if I do, I'd like some clarification as to why. This problem happens when I install on a fresh VM, so it's not like I'm installing on top of the same GUID.

What I'm doing is placing values from the user (from a UI) into some properties, importing those into some C# code through a CustomAction, where I then encrypt the values and put them into a registry key.

I don't use WiX to put the values into the registry key, I do it from the C# code. The reason for this is I can't seem to import Properties into a C# Custom Action and then export values from that same code back into WiX (I can do each separately no problem). But that's a different question...

Anyways, I get the values, encrypt them, and place them into a registry key just fine. I just can't seem to delete the registry key when uninstalling. Oddly enough, it does delete every value in the key except for one, but it doens't delete the entire key.

Here's the XML that should delete the key (but doesn't):

        <Component Id="Component_CleanRegistryOnUninstall"
               Directory="TARGETDIR"
               Guid="{86D04E28-2EF8-4A6C-BB9B-577EA1597BB5}" 
               KeyPath="yes">
        <RemoveRegistryKey Id="CleanupRegistry"
                           Root="HKLM"
                           Key="Software\...\...\InstallCfg"
                           Action="removeOnUninstall"/>
        </Component>

Here's the XML that creates the C# Custom Action:

    <Fragment>      
  <Property Id="VAL1" Hidden="yes"/>
  <Property Id="VAL2" Hidden="yes"/>
  <Property Id="VAL3" Hidden="yes"/>
  <Property Id="VAL4" Hidden="yes"/>

  <SetProperty Id="CustomAction_PassProperty"
               Value="VAL1=[VAL1];VAL2=[VAL2];VAL3=[VAL3];VAL4=[VAL4]"
               Sequence="execute"
               Before="CustomAction_PassProperty"/>

  <Binary Id="Binary_PassProps"
          SourceFile="$(var.CreateRegistryKey.TargetDir)CreateRegistryKey.CA.dll"/>

  <!-- Note that 'Impersonate="no"' elevates the privilege of the C# code, needed to create keys -->
  <CustomAction Id="CustomAction_PassProperty"
                BinaryKey="Binary_PassProps"
                DllEntry="CreateKeys"
                Execute="deferred"
                Impersonate="no"
                Return="check" 
                HideTarget="yes"/>

  <InstallExecuteSequence>
      <Custom Action="CustomAction_PassProperty"
              After="InstallInitialize"/>
  </InstallExecuteSequence>
</Fragment>

Here's the C# itself:

        [CustomAction]
    public static ActionResult CreateKeys(Session session)
    {
        // encrypt and set set the registry keys
        Registry.SetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\...\\...\\InstallCfg", "Val1", Encrypt(session.CustomActionData["VAL1"]));
        Registry.SetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\...\\...\\InstallCfg", "Val2", Encrypt(session.CustomActionData["VAL2"]));
        Registry.SetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\...\\...\\InstallCfg", "Val3", Encrypt(session.CustomActionData["VAL3"]));
        Registry.SetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\...\\...\\InstallCfg", "Val4", Encrypt(session.CustomActionData["VAL4"]));

        // also, set the "SettingsProcessed" key to false
        Registry.SetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\...\\...\\InstallCfg", "SettingsProcessed", "false");

        return ActionResult.Success;
    }

The final value that is set -- the one that is just passed in as "false" and not an encrypted value -- is a flag, and may be the key to the problem.

Here's the weird behavior: when I uninstall, the key doesn't get deleted, but it does delete all of the values except the one value that isn't passed into the C# function, the flag. It's not getting deleted. However, even if I create a property, give that property the value "false", and pass it into the C#, like this:

Registry.SetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\...\\...\\InstallCfg", "SettingsProcessed", session.CustomActionData["SETTINGSPROCESSED"]);

The same thing happens: every other value gets deleted except for it. I admit that it may be a Component issue and the answer may lie in this answer, but I can't figure it out.

To sum up: the registry key gets created but the <RemoveRegistryKey> doesn't delete it upon uninstallation.

EDIT::

Bob never got his <RemoveRegistryKey> element to work. He quit IT and moved to Omaha, where he runs a Lebanese/Dutch pizza shop and practices the banjo.

No, really, I never got it to work. I ended up creating another Custom Action that deletes the key. It's so frustrating it makes me want to move to Omaha.

1条回答
在下西门庆
2楼-- · 2019-07-23 07:32

You need to set a condition to not run CustomAction_PassProperty during uninstallation. Like this:

  <InstallExecuteSequence>
      <Custom Action="CustomAction_PassProperty"
              After="InstallInitialize>NOT REMOVE="ALL"</Custom>
  </InstallExecuteSequence>

That it "deletes all the values" except for the hardcoded one could be a result of you rewriting the values of VAL1, VAL2... during uninstall, at which time those properties likely are empty (they are set only during install in the ui sequence right?).

查看更多
登录 后发表回答