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

View file

@ -29,20 +29,53 @@ namespace Greenshot.Helpers {
public static class 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 LOCALMACHINE = @"HKEY_LOCAL_MACHINE\";
private const string CURRENTUSER = @"HKEY_CURRENT_USER\";
private const string APPLICATIONNAME = "Greenshot";
private static string getExecutablePath() {
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() {
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(RUNKEY, false)) {
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;
@ -51,23 +84,36 @@ namespace Greenshot.Helpers {
public static Object getRunUserValue() {
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(RUNKEY, false)) {
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;
}
public static bool checkRunAll() {
Object runValue = null;
public static bool hasRunAll() {
try {
runValue = getRunAllValue();
return getRunAllValue() != null;
} catch (Exception e) {
LOG.Error("Error retrieving RunAllValue", e);
}
return runValue != null;
return false;
}
public static bool checkRunUser() {
public static bool hasRunUser() {
Object runValue = null;
try {
runValue = getRunUserValue();
@ -76,13 +122,45 @@ namespace Greenshot.Helpers {
}
return runValue != null;
}
public static void deleteRunUser() {
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(RUNKEY, true)) {
public static void deleteRunAll() {
if (hasRunAll()) {
try {
if (checkRunUser()) {
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(RUNKEY, true)) {
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) {
LOG.Error("Error in deleteRunUser.", e);
}
@ -90,12 +168,12 @@ namespace Greenshot.Helpers {
}
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());
} catch (Exception e) {
LOG.Error("Error in setRunUser.", e);
}
} catch (Exception e) {
LOG.Error("Error in setRunUser.", e);
}
}
}