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.
authormoel.mich
Fri, 21 May 2010 17:28:52 +0000
changeset 123912a06e2bd53
parent 122 3ef997c53b50
child 124 b68a6f3b82a3
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)
GUI/StartupManager.cs
     1.1 --- a/GUI/StartupManager.cs	Thu May 20 21:23:54 2010 +0000
     1.2 +++ b/GUI/StartupManager.cs	Fri May 21 17:28:52 2010 +0000
     1.3 @@ -38,6 +38,7 @@
     1.4  using System;
     1.5  using System.Collections.Generic;
     1.6  using System.IO;
     1.7 +using System.Security.Principal;
     1.8  using System.Windows.Forms;
     1.9  using Microsoft.Win32;
    1.10  using OpenHardwareMonitor.TaskScheduler;
    1.11 @@ -51,25 +52,39 @@
    1.12      private const string REGISTRY_RUN =
    1.13        @"Software\Microsoft\Windows\CurrentVersion\Run";
    1.14  
    1.15 +    private bool IsAdministrator() {
    1.16 +      try {
    1.17 +        WindowsIdentity identity = WindowsIdentity.GetCurrent();
    1.18 +        WindowsPrincipal principal = new WindowsPrincipal(identity);
    1.19 +        return principal.IsInRole(WindowsBuiltInRole.Administrator);
    1.20 +      } catch {
    1.21 +        return false;
    1.22 +      }
    1.23 +    }
    1.24 +
    1.25      public StartupManager() {
    1.26 -      try {
    1.27 -        scheduler = new TaskSchedulerClass();
    1.28 -        scheduler.Connect(null, null, null, null);
    1.29 -      } catch {
    1.30 +      if (IsAdministrator()) {
    1.31 +        try {
    1.32 +          scheduler = new TaskSchedulerClass();
    1.33 +          scheduler.Connect(null, null, null, null);
    1.34 +        } catch {
    1.35 +          scheduler = null;
    1.36 +        }
    1.37 +
    1.38 +        if (scheduler != null) {
    1.39 +          try {
    1.40 +            ITaskFolder folder = scheduler.GetFolder("\\Open Hardware Monitor");
    1.41 +            IRegisteredTask task = folder.GetTask("Startup");
    1.42 +            startup = task != null;
    1.43 +          } catch (IOException) {
    1.44 +            startup = false;
    1.45 +          } catch (UnauthorizedAccessException) {
    1.46 +            scheduler = null;
    1.47 +          }
    1.48 +        } 
    1.49 +      } else {
    1.50          scheduler = null;
    1.51        }
    1.52 -
    1.53 -      if (scheduler != null) {
    1.54 -        try {
    1.55 -          ITaskFolder folder = scheduler.GetFolder("\\Open Hardware Monitor");
    1.56 -          IRegisteredTask task = folder.GetTask("Startup");
    1.57 -          startup = task != null;
    1.58 -        } catch (IOException) {
    1.59 -          startup = false;
    1.60 -        } catch (UnauthorizedAccessException) {
    1.61 -          scheduler = null;
    1.62 -        }
    1.63 -      } 
    1.64              
    1.65        if (scheduler == null) {
    1.66          RegistryKey key = Registry.CurrentUser.OpenSubKey(REGISTRY_RUN);