From 328e7e569d8327bce20ec887d6d64b669cdfbcba Mon Sep 17 00:00:00 2001 From: Robin Krom Date: Sat, 23 Jan 2021 23:41:22 +0100 Subject: [PATCH] BUG-2693: This should fix an issue where Greenshot doesn't detect a MAPI client, we weren't looking at the 32 bit location. --- GreenshotPlugin/Core/EmailConfigHelper.cs | 50 +++++------------ GreenshotPlugin/Core/RegistryHelper.cs | 66 +++++++++++++++++++++++ 2 files changed, 80 insertions(+), 36 deletions(-) create mode 100644 GreenshotPlugin/Core/RegistryHelper.cs diff --git a/GreenshotPlugin/Core/EmailConfigHelper.cs b/GreenshotPlugin/Core/EmailConfigHelper.cs index 7da960b09..bbd816e96 100644 --- a/GreenshotPlugin/Core/EmailConfigHelper.cs +++ b/GreenshotPlugin/Core/EmailConfigHelper.cs @@ -1,6 +1,6 @@ /* * Greenshot - a free and open source screenshot tool - * Copyright (C) 2007-2020 Thomas Braun, Jens Klingen, Robin Krom + * Copyright (C) 2007-2021 Thomas Braun, Jens Klingen, Robin Krom * * For more information see: http://getgreenshot.org/ * The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot @@ -18,46 +18,25 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ + using System.IO; -using Microsoft.Win32; namespace GreenshotPlugin.Core { /// /// Description of EmailConfigHelper. /// public static class EmailConfigHelper { - private const string OutlookPathKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\OUTLOOK.EXE"; - private const string MapiClientKey = @"SOFTWARE\Clients\Mail"; - private const string MapiLocationKey = @"SOFTWARE\Microsoft\Windows Messaging Subsystem"; - private const string MapiKey = @"MAPI"; - public static string GetMapiClient() { - using (RegistryKey key = Registry.CurrentUser.OpenSubKey(MapiClientKey, false)) { - if (key != null) { - return (string)key.GetValue(string.Empty); - } - } - using (RegistryKey key = Registry.LocalMachine.OpenSubKey(MapiClientKey, false)) - { - return (string) key?.GetValue(string.Empty); - } - } - - public static bool HasMapi() - { - using RegistryKey key = Registry.LocalMachine.OpenSubKey(MapiLocationKey, false); - return key != null && "1".Equals(key.GetValue(MapiKey, "0")); - } + public static string GetMapiClient() => RegistryHelper.ReadLocalMachineSoftwareKey(@"Clients\Mail"); - public static string GetOutlookExePath() { - using (RegistryKey key = Registry.LocalMachine.OpenSubKey(OutlookPathKey, false)) { - if (key != null) { - // "" is the default key, which should point to the outlook location - return (string)key.GetValue(string.Empty); - } - } - return null; + public static bool HasMapi() + { + var value = RegistryHelper.ReadLocalMachineSoftwareKey(@"Microsoft\Windows Messaging Subsystem", "MAPI", "0"); + return "1".Equals(value); + } + + public static string GetOutlookExePath() => RegistryHelper.ReadLocalMachineSoftwareKey(@"Microsoft\Windows\CurrentVersion\App Paths\OUTLOOK.EXE"); /// /// Check if Outlook is installed @@ -65,12 +44,11 @@ namespace GreenshotPlugin.Core { /// Returns true if outlook is installed public static bool HasOutlook() { string outlookPath = GetOutlookExePath(); - if (outlookPath != null) { - if (File.Exists(outlookPath)) { - return true; - } + if (outlookPath == null) + { + return false; } - return false; + return File.Exists(outlookPath); } } } diff --git a/GreenshotPlugin/Core/RegistryHelper.cs b/GreenshotPlugin/Core/RegistryHelper.cs new file mode 100644 index 000000000..e5f068ab4 --- /dev/null +++ b/GreenshotPlugin/Core/RegistryHelper.cs @@ -0,0 +1,66 @@ +/* + * Greenshot - a free and open source screenshot tool + * Copyright (C) 2007-2021 Thomas Braun, Jens Klingen, Robin Krom + * + * For more information see: http://getgreenshot.org/ + * The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 1 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +using System; +using Microsoft.Win32; + +namespace GreenshotPlugin.Core +{ + /// + /// A helper class for accessing the registry + /// + public static class RegistryHelper + { + /// + /// Retrieve a registry value + /// + /// 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 ReadLocalMachineSoftwareKey(string keyName, string value = null, string defaultValue = null) + { + string result = null; + value ??= string.Empty; + if (Environment.Is64BitOperatingSystem) + { + using var key = Registry.LocalMachine.OpenSubKey($@"SOFTWARE\{keyName}", false); + + if (key != null) + { + result = (string)key.GetValue(value, defaultValue); + } + } + + if (string.IsNullOrEmpty(result)) + { + using var key = Registry.LocalMachine.OpenSubKey($@"SOFTWARE\wow6432node\{keyName}", false); + + if (key != null) + { + result = (string)key.GetValue(value, defaultValue); + } + } + + return result; + } + } +}