# HG changeset patch # User moel.mich # Date 1274462932 0 # Node ID 912a06e2bd5313573ab352b90fae70e80e931059 # Parent 3ef997c53b501fc5c23d7d1de33b5b9a7c88e6b3 Fixed an UnauthorizedAccessException in the StartupManager. The exception occurred when the application was running without administrator rights. A task for the current user could be created manually, but RegisterTaskDefinition somehow always throws an UnauthorizedAccessException. The current fix restricts the TaskScheduler startup method to accounts with administrator rights. System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) at OpenHardwareMonitor.TaskScheduler.ITaskFolder.RegisterTaskDefinition(String Path, ITaskDefinition pDefinition, Int32 flags, Object UserId, Object password, TASK_LOGON_TYPE LogonType, Object sddl) at OpenHardwareMonitor.GUI.StartupManager.CreateSchedulerTask() at OpenHardwareMonitor.GUI.StartupManager.set_Startup(Boolean value) at OpenHardwareMonitor.GUI.MainForm.runOnWindowsStartupToolStripMenuItem_CheckedChanged(Object sender, EventArgs e) at System.Windows.Forms.ToolStripMenuItem.OnCheckedChanged(EventArgs e) at System.Windows.Forms.ToolStripMenuItem.set_CheckState(CheckState value) at System.Windows.Forms.ToolStripMenuItem.set_Checked(Boolean value) at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e) at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e) at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e) at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met) at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met) at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea) at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea) at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ScrollableControl.WndProc(Message& m) at System.Windows.Forms.ToolStrip.WndProc(Message& m) at System.Windows.Forms.ToolStripDropDown.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) diff -r 3ef997c53b50 -r 912a06e2bd53 GUI/StartupManager.cs --- a/GUI/StartupManager.cs Thu May 20 21:23:54 2010 +0000 +++ b/GUI/StartupManager.cs Fri May 21 17:28:52 2010 +0000 @@ -38,6 +38,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Security.Principal; using System.Windows.Forms; using Microsoft.Win32; using OpenHardwareMonitor.TaskScheduler; @@ -51,25 +52,39 @@ private const string REGISTRY_RUN = @"Software\Microsoft\Windows\CurrentVersion\Run"; + private bool IsAdministrator() { + try { + WindowsIdentity identity = WindowsIdentity.GetCurrent(); + WindowsPrincipal principal = new WindowsPrincipal(identity); + return principal.IsInRole(WindowsBuiltInRole.Administrator); + } catch { + return false; + } + } + public StartupManager() { - try { - scheduler = new TaskSchedulerClass(); - scheduler.Connect(null, null, null, null); - } catch { + if (IsAdministrator()) { + try { + scheduler = new TaskSchedulerClass(); + scheduler.Connect(null, null, null, null); + } catch { + scheduler = null; + } + + if (scheduler != null) { + try { + ITaskFolder folder = scheduler.GetFolder("\\Open Hardware Monitor"); + IRegisteredTask task = folder.GetTask("Startup"); + startup = task != null; + } catch (IOException) { + startup = false; + } catch (UnauthorizedAccessException) { + scheduler = null; + } + } + } else { scheduler = null; } - - if (scheduler != null) { - try { - ITaskFolder folder = scheduler.GetFolder("\\Open Hardware Monitor"); - IRegisteredTask task = folder.GetTask("Startup"); - startup = task != null; - } catch (IOException) { - startup = false; - } catch (UnauthorizedAccessException) { - scheduler = null; - } - } if (scheduler == null) { RegistryKey key = Registry.CurrentUser.OpenSubKey(REGISTRY_RUN);