2

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.

Community
  • 1
  • 1
Bob
  • 369
  • 1
  • 4
  • 24

1 Answers1

1

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?).

Kflexior
  • 297
  • 2
  • 9
  • Thanks for response! Yes, the properties are set only during the UI sequence, except for the hardcoded flag. It makes sense that they would get deleted during uninstall (except for the hardcoded flag). When I put in the condition that you suggest, I do get different behavior -- now, when I uninstall, it doesn't delete the values. VAL1,..., VALX are still there as well as the flag. However, that's not what I want to do. I want to remove the registry key entirely. – Bob Feb 16 '17 at 13:59
  • [This](http://stackoverflow.com/questions/320921/how-to-add-a-wix-custom-action-that-happens-only-on-uninstall-via-msi) conversation clears up a lot about conditions -- how to get custom actions to run when you want them to. Which I need to do: make my custom action run only during installation and repairs. _In addition_, during uninstallation I need to remove the registry key that the custom action creates. That's where I'm stuck. – Bob Feb 16 '17 at 14:20
  • Have you referenced the `Component_CleanRegistryOnUninstall` component inside your ? Also I would suggest logging an uninstallation and see what happens related to the component. Do `yourmsi.msi /x /l*v log.txt` – Kflexior Feb 17 '17 at 09:44
  • Yes, I do reference the component. I was wondering how to log an uninstall -- it must be the /x flag! I'll do that! – Bob Feb 17 '17 at 12:58