moel@82: /* moel@82: moel@82: Version: MPL 1.1/GPL 2.0/LGPL 2.1 moel@82: moel@82: The contents of this file are subject to the Mozilla Public License Version moel@82: 1.1 (the "License"); you may not use this file except in compliance with moel@82: the License. You may obtain a copy of the License at moel@82: moel@82: http://www.mozilla.org/MPL/ moel@82: moel@82: Software distributed under the License is distributed on an "AS IS" basis, moel@82: WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License moel@82: for the specific language governing rights and limitations under the License. moel@82: moel@82: The Original Code is the Open Hardware Monitor code. moel@82: moel@82: The Initial Developer of the Original Code is moel@82: Michael Möller . moel@82: Portions created by the Initial Developer are Copyright (C) 2009-2010 moel@82: the Initial Developer. All Rights Reserved. moel@82: moel@82: Contributor(s): moel@82: moel@82: Alternatively, the contents of this file may be used under the terms of moel@82: either the GNU General Public License Version 2 or later (the "GPL"), or moel@82: the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), moel@82: in which case the provisions of the GPL or the LGPL are applicable instead moel@82: of those above. If you wish to allow use of your version of this file only moel@82: under the terms of either the GPL or the LGPL, and not to allow others to moel@82: use your version of this file under the terms of the MPL, indicate your moel@82: decision by deleting the provisions above and replace them with the notice moel@82: and other provisions required by the GPL or the LGPL. If you do not delete moel@82: the provisions above, a recipient may use your version of this file under moel@82: the terms of any one of the MPL, the GPL or the LGPL. moel@82: moel@82: */ moel@82: moel@82: using System; moel@82: using System.Collections.Generic; moel@82: using System.IO; moel@139: using System.Runtime.InteropServices; moel@142: using System.Security; moel@123: using System.Security.Principal; moel@82: using System.Windows.Forms; moel@82: using Microsoft.Win32; moel@82: using OpenHardwareMonitor.TaskScheduler; moel@82: moel@82: namespace OpenHardwareMonitor.GUI { moel@82: public class StartupManager { moel@82: moel@82: private TaskSchedulerClass scheduler; moel@82: private bool startup; moel@142: private bool isAvailable; moel@82: moel@82: private const string REGISTRY_RUN = moel@82: @"Software\Microsoft\Windows\CurrentVersion\Run"; moel@82: moel@123: private bool IsAdministrator() { moel@123: try { moel@123: WindowsIdentity identity = WindowsIdentity.GetCurrent(); moel@123: WindowsPrincipal principal = new WindowsPrincipal(identity); moel@123: return principal.IsInRole(WindowsBuiltInRole.Administrator); moel@123: } catch { moel@123: return false; moel@123: } moel@123: } moel@123: moel@82: public StartupManager() { moel@142: int p = (int)System.Environment.OSVersion.Platform; moel@142: if ((p == 4) || (p == 128)) { moel@142: scheduler = null; moel@142: isAvailable = false; moel@142: return; moel@142: } moel@142: moel@123: if (IsAdministrator()) { moel@123: try { moel@123: scheduler = new TaskSchedulerClass(); moel@123: scheduler.Connect(null, null, null, null); moel@123: } catch { moel@123: scheduler = null; moel@123: } moel@123: moel@123: if (scheduler != null) { moel@123: try { moel@139: // check if the taskscheduler is running moel@139: IRunningTaskCollection collection = scheduler.GetRunningTasks(0); moel@139: moel@123: ITaskFolder folder = scheduler.GetFolder("\\Open Hardware Monitor"); moel@123: IRegisteredTask task = folder.GetTask("Startup"); moel@129: startup = (task != null) && moel@129: (task.Definition.Triggers.Count > 0) && moel@129: (task.Definition.Triggers[1].Type == moel@129: TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON) && moel@129: (task.Definition.Actions.Count > 0) && moel@129: (task.Definition.Actions[1].Type == moel@129: TASK_ACTION_TYPE.TASK_ACTION_EXEC) && moel@129: (task.Definition.Actions[1] as IExecAction != null) && moel@129: ((task.Definition.Actions[1] as IExecAction).Path == moel@129: Application.ExecutablePath); moel@129: moel@123: } catch (IOException) { moel@123: startup = false; moel@123: } catch (UnauthorizedAccessException) { moel@123: scheduler = null; moel@139: } catch (COMException) { moel@139: scheduler = null; moel@123: } moel@123: } moel@123: } else { moel@82: scheduler = null; moel@82: } moel@142: moel@120: if (scheduler == null) { moel@142: try { moel@142: using (RegistryKey key = moel@142: Registry.CurrentUser.OpenSubKey(REGISTRY_RUN)) { moel@142: startup = false; moel@142: if (key != null) { moel@142: string value = (string)key.GetValue("OpenHardwareMonitor"); moel@142: if (value != null) moel@142: startup = value == Application.ExecutablePath; moel@142: } moel@142: } moel@142: isAvailable = true; moel@142: } catch (SecurityException) { moel@142: isAvailable = false; moel@82: } moel@142: } else { moel@142: isAvailable = true; moel@82: } moel@82: } moel@82: moel@82: private void CreateSchedulerTask() { moel@82: ITaskDefinition definition = scheduler.NewTask(0); moel@82: definition.RegistrationInfo.Description = moel@82: "This task starts the Open Hardware Monitor on Windows startup."; moel@82: definition.Principal.RunLevel = moel@82: TASK_RUNLEVEL.TASK_RUNLEVEL_HIGHEST; moel@82: definition.Settings.DisallowStartIfOnBatteries = false; moel@82: definition.Settings.StopIfGoingOnBatteries = false; moel@82: definition.Settings.ExecutionTimeLimit = "PT0S"; moel@82: moel@82: ILogonTrigger trigger = (ILogonTrigger)definition.Triggers.Create( moel@82: TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON); moel@82: moel@82: IExecAction action = (IExecAction)definition.Actions.Create( moel@82: TASK_ACTION_TYPE.TASK_ACTION_EXEC); moel@82: action.Path = Application.ExecutablePath; moel@82: action.WorkingDirectory = moel@82: Path.GetDirectoryName(Application.ExecutablePath); moel@82: moel@82: ITaskFolder root = scheduler.GetFolder("\\"); moel@82: ITaskFolder folder; moel@82: try { moel@82: folder = root.GetFolder("Open Hardware Monitor"); moel@104: } catch (IOException) { moel@82: folder = root.CreateFolder("Open Hardware Monitor", ""); moel@82: } moel@82: folder.RegisterTaskDefinition("Startup", definition, moel@82: (int)TASK_CREATION.TASK_CREATE_OR_UPDATE, null, null, moel@82: TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN, ""); moel@82: } moel@82: moel@82: private void DeleteSchedulerTask() { moel@82: ITaskFolder root = scheduler.GetFolder("\\"); moel@82: try { moel@82: ITaskFolder folder = root.GetFolder("Open Hardware Monitor"); moel@82: folder.DeleteTask("Startup", 0); moel@104: } catch (IOException) { } moel@82: try { moel@82: root.DeleteFolder("Open Hardware Monitor", 0); moel@104: } catch (IOException) { } moel@82: } moel@82: moel@82: private void CreateRegistryRun() { moel@185: RegistryKey key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN); moel@82: key.SetValue("OpenHardwareMonitor", Application.ExecutablePath); moel@82: } moel@82: moel@82: private void DeleteRegistryRun() { moel@82: RegistryKey key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN); moel@82: key.DeleteValue("OpenHardwareMonitor"); moel@82: } moel@82: moel@142: public bool IsAvailable { moel@142: get { return isAvailable; } moel@142: } moel@142: moel@82: public bool Startup { moel@82: get { moel@82: return startup; moel@82: } moel@82: set { moel@185: if (startup != value) { moel@142: if (isAvailable) { moel@142: if (scheduler != null) { moel@185: if (value) moel@142: CreateSchedulerTask(); moel@142: else moel@142: DeleteSchedulerTask(); moel@185: startup = value; moel@142: } else { moel@185: try { moel@185: if (value) moel@185: CreateRegistryRun(); moel@185: else moel@185: DeleteRegistryRun(); moel@185: startup = value; moel@185: } catch (UnauthorizedAccessException) { moel@185: throw new InvalidOperationException(); moel@185: } moel@142: } moel@185: } else { moel@185: throw new InvalidOperationException(); moel@82: } moel@82: } moel@82: } moel@82: } moel@82: } moel@82: moel@82: }