How to read certain registry key from Wix managed bootstrapper or custom action?
Asked Answered
L

1

1

I am having trouble reading this key from my managed bootstrapper (.NET 4.5.2), and my custom action (tried .NET 2.0 and 4.0).

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\Packages

I tried running the bootstrapper and msi using cmd as Administrator. My test environment is Windows 7 64-bit. I am using Wix 3.11

I made a test command line app and it was able to access this key.

The testing code I used in the bootstrapper and custom action:

static RegistryKey GetHKLMKey(string registryPath) {
    var hklm64 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
    var registryKey64 = hklm64.OpenSubKey(registryPath);
    if ((registryKey64?.GetValueNames().Any()).GetValueOrDefault()) {
        return registryKey64;
    }

    var hklm32 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
        return hklm32.OpenSubKey(registryPath);
    }
}
...
var path = @"SOFTWARE";

foreach (var segment in @"Microsoft\Windows\CurrentVersion\Component Based Servicing\Packages".Split('\\')) {
    path += "\\"+segment;

    var j = GetHKLMKey(path);
    Log(path + ": " + j);

    var k = Registry.LocalMachine.OpenSubKey(path);
    Log(path + ": " + k);
    if (k == null) return true;
}

test output

SOFTWARE\Microsoft: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft
SOFTWARE\Microsoft: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft
SOFTWARE\Microsoft\Windows: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows
SOFTWARE\Microsoft\Windows: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows
SOFTWARE\Microsoft\Windows\CurrentVersion: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion
SOFTWARE\Microsoft\Windows\CurrentVersion: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion
SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing
SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing
SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\Packages: 
SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\Packages: 
Liking answered 4/9, 2018 at 23:14 Comment(0)
M
2

Debugging Managed Code Custom Actions: Not quite clear what the problem is? The read does not work as a custom action? Try showing a message box from the custom action and then attach the debugger to the rundll32.exe process running managed code. Then you can step through the code using Visual Studio in normal "debug fashion". Here is a nice video from Advanced Installer showing you how to do this: Debug C# Custom Actions.

Registry Read: Once you have debugability, it should be possible to work out what the cause is of whatever problem it is that you are seeing.

  • Suppressed Exception: Maybe there is an exception of some sort happening and you have set the error checking for the custom action to "ignore exit code"?
  • Bitness: The most common problem seems to be "bitness" (32-bit vs 64-bit) - in other words you read the wrong registry location, but I don't think this is the problem you are seeing.
  • Access Violation?: It could also be that you are running something that requires admin rights from the GUI sequence before the setup has elevated to admin rights. That might trigger an access rights exception.

Just some ideas off the top of my head. Please let us know what it was.

Micmac answered 5/9, 2018 at 6:57 Comment(2)
It was a bitness issue. My code had a problem here if ((registryKey64?.GetValueNames().Any()).GetValueOrDefault()) {. I changed to if(registryKey64 != null) { and it's working as intended. Your "Debug C# Custom Actions" was helpful.Liking
Great that you found it, thanks for letting us know what it was. Of course it was what I thought it was not. Naturally! :-). I have wasted many hours on bitness issues - this problem gets on everyone's nerves I think.Synchrocyclotron

© 2022 - 2024 — McMap. All rights reserved.