Fix for #3580123. Note: if Greenshot is forced to 32-bit (e.g. while debugging) the 64-bit keys are not read/deleted, so debugging of this code should be done in AnyCPU mode under Visual Studio!!

git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@2204 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
RKrom 2012-10-26 14:13:32 +00:00
commit 5723f6fa5d
2 changed files with 115 additions and 32 deletions

View file

@ -352,14 +352,16 @@ namespace Greenshot {
numericUpDownWaitTime.Value = coreConfiguration.CaptureDelay >=0?coreConfiguration.CaptureDelay:0; numericUpDownWaitTime.Value = coreConfiguration.CaptureDelay >=0?coreConfiguration.CaptureDelay:0;
// If the run for all is set we disable and set the checkbox // Autostart checkbox logic.
if (StartupHelper.checkRunAll()) { if (StartupHelper.hasRunAll()) {
checkbox_autostartshortcut.Enabled = false; // Remove runUser if we already have a run under all
checkbox_autostartshortcut.Checked = true; StartupHelper.deleteRunUser();
checkbox_autostartshortcut.Enabled = StartupHelper.canWriteRunAll();
checkbox_autostartshortcut.Checked = StartupHelper.hasRunAll();
} else { } else {
// No run for all, enable the checkbox and set it to true if the current user has a key // No run for all, enable the checkbox and set it to true if the current user has a key
checkbox_autostartshortcut.Enabled = true; checkbox_autostartshortcut.Enabled = StartupHelper.canWriteRunUser();
checkbox_autostartshortcut.Checked = StartupHelper.checkRunUser(); checkbox_autostartshortcut.Checked = StartupHelper.hasRunUser();
} }
numericUpDown_daysbetweencheck.Value = coreConfiguration.UpdateCheckInterval; numericUpDown_daysbetweencheck.Value = coreConfiguration.UpdateCheckInterval;
@ -408,17 +410,20 @@ namespace Greenshot {
coreConfiguration.UpdateCheckInterval = (int)numericUpDown_daysbetweencheck.Value; coreConfiguration.UpdateCheckInterval = (int)numericUpDown_daysbetweencheck.Value;
try { try {
// Check if the Run for all is set if (checkbox_autostartshortcut.Checked) {
if(!StartupHelper.checkRunAll()) { // It's checked, so we set the RunUser if the RunAll isn't set.
// If not set the registry according to the settings // Do this every time, so the executable is correct.
if (checkbox_autostartshortcut.Checked) { if (!StartupHelper.hasRunAll()) {
StartupHelper.setRunUser(); StartupHelper.setRunUser();
} else {
StartupHelper.deleteRunUser();
} }
} else { } else {
// The run key for Greenshot is set for all users, delete the local version! // Delete both settings if it's unchecked
StartupHelper.deleteRunUser(); if (StartupHelper.hasRunAll()) {
StartupHelper.deleteRunAll();
}
if (StartupHelper.hasRunUser()) {
StartupHelper.deleteRunUser();
}
} }
} catch (Exception e) { } catch (Exception e) {
LOG.Warn("Problem checking registry, ignoring for now: ", e); LOG.Warn("Problem checking registry, ignoring for now: ", e);

View file

@ -29,20 +29,53 @@ namespace Greenshot.Helpers {
public static class StartupHelper { public static class StartupHelper {
private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(StartupHelper)); private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(StartupHelper));
private const string RUNKEY6432 = @"Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Run";
private const string RUNKEY = @"Software\Microsoft\Windows\CurrentVersion\Run"; private const string RUNKEY = @"Software\Microsoft\Windows\CurrentVersion\Run";
private const string LOCALMACHINE = @"HKEY_LOCAL_MACHINE\";
private const string CURRENTUSER = @"HKEY_CURRENT_USER\";
private const string APPLICATIONNAME = "Greenshot"; private const string APPLICATIONNAME = "Greenshot";
private static string getExecutablePath() { private static string getExecutablePath() {
return "\"" + Application.ExecutablePath + "\""; return "\"" + Application.ExecutablePath + "\"";
} }
public static bool canWriteRunAll() {
try {
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(RUNKEY, true)) {
}
} catch {
return false;
}
return true;
}
public static bool canWriteRunUser() {
try {
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(RUNKEY, true)) {
}
} catch {
return false;
}
return true;
}
public static Object getRunAllValue() { public static Object getRunAllValue() {
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(RUNKEY, false)) { using (RegistryKey key = Registry.LocalMachine.OpenSubKey(RUNKEY, false)) {
if (key != null) { if (key != null) {
return key.GetValue(APPLICATIONNAME); object runValue = key.GetValue(APPLICATIONNAME);
if (runValue != null) {
return runValue;
}
}
}
// for 64-bit systems we need to check the 32-bit keys too
if (IntPtr.Size == 8) {
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(RUNKEY6432, false)) {
if (key != null) {
object runValue = key.GetValue(APPLICATIONNAME);
if (runValue != null) {
return runValue;
}
}
} }
} }
return null; return null;
@ -51,23 +84,36 @@ namespace Greenshot.Helpers {
public static Object getRunUserValue() { public static Object getRunUserValue() {
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(RUNKEY, false)) { using (RegistryKey key = Registry.CurrentUser.OpenSubKey(RUNKEY, false)) {
if (key != null) { if (key != null) {
return key.GetValue(APPLICATIONNAME); object runValue = key.GetValue(APPLICATIONNAME);
if (runValue != null) {
return runValue;
}
}
}
// for 64-bit systems we need to check the 32-bit keys too
if (IntPtr.Size == 8) {
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(RUNKEY6432, false)) {
if (key != null) {
object runValue = key.GetValue(APPLICATIONNAME);
if (runValue != null) {
return runValue;
}
}
} }
} }
return null; return null;
} }
public static bool checkRunAll() { public static bool hasRunAll() {
Object runValue = null;
try { try {
runValue = getRunAllValue(); return getRunAllValue() != null;
} catch (Exception e) { } catch (Exception e) {
LOG.Error("Error retrieving RunAllValue", e); LOG.Error("Error retrieving RunAllValue", e);
} }
return runValue != null; return false;
} }
public static bool checkRunUser() { public static bool hasRunUser() {
Object runValue = null; Object runValue = null;
try { try {
runValue = getRunUserValue(); runValue = getRunUserValue();
@ -76,13 +122,45 @@ namespace Greenshot.Helpers {
} }
return runValue != null; return runValue != null;
} }
public static void deleteRunUser() { public static void deleteRunAll() {
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(RUNKEY, true)) { if (hasRunAll()) {
try { try {
if (checkRunUser()) { using (RegistryKey key = Registry.LocalMachine.OpenSubKey(RUNKEY, true)) {
key.DeleteValue(APPLICATIONNAME); key.DeleteValue(APPLICATIONNAME);
} }
} catch (Exception e) {
LOG.Error("Error in deleteRunAll.", e);
}
try {
// for 64-bit systems we need to delete the 32-bit keys too
if (IntPtr.Size == 8) {
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(RUNKEY6432, false)) {
key.DeleteValue(APPLICATIONNAME);
}
}
} catch (Exception e) {
LOG.Error("Error in deleteRunAll.", e);
}
}
}
public static void deleteRunUser() {
if (hasRunUser()) {
try {
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(RUNKEY, true)) {
key.DeleteValue(APPLICATIONNAME);
}
} catch (Exception e) {
LOG.Error("Error in deleteRunUser.", e);
}
try {
// for 64-bit systems we need to delete the 32-bit keys too
if (IntPtr.Size == 8) {
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(RUNKEY6432, false)) {
key.DeleteValue(APPLICATIONNAME);
}
}
} catch (Exception e) { } catch (Exception e) {
LOG.Error("Error in deleteRunUser.", e); LOG.Error("Error in deleteRunUser.", e);
} }
@ -90,12 +168,12 @@ namespace Greenshot.Helpers {
} }
public static void setRunUser() { public static void setRunUser() {
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(RUNKEY, true)) { try {
try { using (RegistryKey key = Registry.CurrentUser.OpenSubKey(RUNKEY, true)) {
key.SetValue(APPLICATIONNAME, getExecutablePath()); key.SetValue(APPLICATIONNAME, getExecutablePath());
} catch (Exception e) {
LOG.Error("Error in setRunUser.", e);
} }
} catch (Exception e) {
LOG.Error("Error in setRunUser.", e);
} }
} }
} }