From eadd1a7cac0a77854608350d4d17a00e3665625e Mon Sep 17 00:00:00 2001 From: Robin Krom Date: Tue, 16 Mar 2021 15:56:03 +0100 Subject: [PATCH] BUG-2743: Made sure for the registry access that the default value not requested in the registry access itself, but returned if there is no value, as it will break the logic. Also there was a bug for 32 bit Windows versions. --- GreenshotPlugin/Core/EmailConfigHelper.cs | 7 ++- GreenshotPlugin/Core/RegistryKeyExtensions.cs | 49 +++++++++++++------ 2 files changed, 38 insertions(+), 18 deletions(-) diff --git a/GreenshotPlugin/Core/EmailConfigHelper.cs b/GreenshotPlugin/Core/EmailConfigHelper.cs index 354f02c33..9eb8a7bfe 100644 --- a/GreenshotPlugin/Core/EmailConfigHelper.cs +++ b/GreenshotPlugin/Core/EmailConfigHelper.cs @@ -28,16 +28,15 @@ namespace GreenshotPlugin.Core { /// public static class EmailConfigHelper { - public static string GetMapiClient() => Registry.LocalMachine.ReadKey64Or32(@"Clients\Mail"); + public static string GetMapiClient() => RegistryHive.LocalMachine.ReadKey64Or32(@"Clients\Mail"); public static bool HasMapi() { - var value = Registry.LocalMachine.ReadKey64Or32(@"Microsoft\Windows Messaging Subsystem", "MAPI", "0"); + var value = RegistryHive.LocalMachine.ReadKey64Or32(@"Microsoft\Windows Messaging Subsystem", "MAPI", "0"); return "1".Equals(value); - } - public static string GetOutlookExePath() => Registry.LocalMachine.ReadKey64Or32(@"Microsoft\Windows\CurrentVersion\App Paths\OUTLOOK.EXE"); + public static string GetOutlookExePath() => RegistryHive.LocalMachine.ReadKey64Or32(@"Microsoft\Windows\CurrentVersion\App Paths\OUTLOOK.EXE"); /// /// Check if Outlook is installed diff --git a/GreenshotPlugin/Core/RegistryKeyExtensions.cs b/GreenshotPlugin/Core/RegistryKeyExtensions.cs index 73fd66a47..bfdb63e10 100644 --- a/GreenshotPlugin/Core/RegistryKeyExtensions.cs +++ b/GreenshotPlugin/Core/RegistryKeyExtensions.cs @@ -32,36 +32,57 @@ namespace GreenshotPlugin.Core /// /// Retrieve a registry value /// - /// RegistryKey like Registry.LocalMachine + /// RegistryHive like RegistryHive.LocalMachine /// string with the name of the key /// string with the name of the value below the key, null will retrieve the default /// string with the default value to return /// string with the value - public static string ReadKey64Or32(this RegistryKey registryKey, string keyName, string value = null, string defaultValue = null) + public static string ReadKey64Or32(this RegistryHive registryHive, string keyName, string value = null, string defaultValue = null) { string result = null; value ??= string.Empty; - if (Environment.Is64BitOperatingSystem) + + using var registryKey32 = RegistryKey.OpenBaseKey(registryHive, RegistryView.Registry32); + // Logic for 32-bit Windows is just reading the key + if (!Environment.Is64BitOperatingSystem) { - using var key = registryKey.OpenSubKey($@"SOFTWARE\{keyName}", false); - + using var key = registryKey32.OpenSubKey($@"SOFTWARE\{keyName}", false); + if (key != null) { - result = (string)key.GetValue(value, defaultValue); + result = (string)key.GetValue(value); + } + if (string.IsNullOrEmpty(result)) + { + result = defaultValue; + } + return result; + } + using var registryKey64 = RegistryKey.OpenBaseKey(registryHive, RegistryView.Registry64); + // Logic for 64 bit Windows, is trying the key which is 64 bit + using (var key = registryKey64.OpenSubKey($@"SOFTWARE\{keyName}", false)) + { + if (key != null) + { + result = (string)key.GetValue(value); + } + if (!string.IsNullOrEmpty(result)) return result; + } + + // if there is no value use the wow6432node key which is 32 bit + using (var key = registryKey32.OpenSubKey($@"SOFTWARE\{keyName}", false)) + { + if (key != null) + { + result = (string)key.GetValue(value); } } if (string.IsNullOrEmpty(result)) { - using var key = registryKey.OpenSubKey($@"SOFTWARE\wow6432node\{keyName}", false); - - if (key != null) - { - result = (string)key.GetValue(value, defaultValue); - } + result = defaultValue; } - return result; } } -} +} \ No newline at end of file