Added the source code of the WinRing0 device driver.
3 This Source Code Form is subject to the terms of the Mozilla Public
4 License, v. 2.0. If a copy of the MPL was not distributed with this
5 file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 Copyright (C) 2009-2010 Michael Möller <mmoeller@openhardwaremonitor.org>
12 using System.Collections.Generic;
14 using System.Runtime.InteropServices;
15 using System.Security;
16 using System.Security.Principal;
17 using System.Windows.Forms;
18 using Microsoft.Win32;
19 using OpenHardwareMonitor.TaskScheduler;
21 namespace OpenHardwareMonitor.GUI {
22 public class StartupManager {
24 private TaskSchedulerClass scheduler;
26 private bool isAvailable;
28 private const string REGISTRY_RUN =
29 @"Software\Microsoft\Windows\CurrentVersion\Run";
31 private bool IsAdministrator() {
33 WindowsIdentity identity = WindowsIdentity.GetCurrent();
34 WindowsPrincipal principal = new WindowsPrincipal(identity);
35 return principal.IsInRole(WindowsBuiltInRole.Administrator);
41 public StartupManager() {
42 int p = (int)System.Environment.OSVersion.Platform;
43 if ((p == 4) || (p == 128)) {
49 if (IsAdministrator()) {
51 scheduler = new TaskSchedulerClass();
52 scheduler.Connect(null, null, null, null);
57 if (scheduler != null) {
59 // check if the taskscheduler is running
60 IRunningTaskCollection collection = scheduler.GetRunningTasks(0);
62 ITaskFolder folder = scheduler.GetFolder("\\Open Hardware Monitor");
63 IRegisteredTask task = folder.GetTask("Startup");
64 startup = (task != null) &&
65 (task.Definition.Triggers.Count > 0) &&
66 (task.Definition.Triggers[1].Type ==
67 TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON) &&
68 (task.Definition.Actions.Count > 0) &&
69 (task.Definition.Actions[1].Type ==
70 TASK_ACTION_TYPE.TASK_ACTION_EXEC) &&
71 (task.Definition.Actions[1] as IExecAction != null) &&
72 ((task.Definition.Actions[1] as IExecAction).Path ==
73 Application.ExecutablePath);
75 } catch (IOException) {
77 } catch (UnauthorizedAccessException) {
79 } catch (COMException) {
87 if (scheduler == null) {
89 using (RegistryKey key =
90 Registry.CurrentUser.OpenSubKey(REGISTRY_RUN)) {
93 string value = (string)key.GetValue("OpenHardwareMonitor");
95 startup = value == Application.ExecutablePath;
99 } catch (SecurityException) {
107 private void CreateSchedulerTask() {
108 ITaskDefinition definition = scheduler.NewTask(0);
109 definition.RegistrationInfo.Description =
110 "This task starts the Open Hardware Monitor on Windows startup.";
111 definition.Principal.RunLevel =
112 TASK_RUNLEVEL.TASK_RUNLEVEL_HIGHEST;
113 definition.Settings.DisallowStartIfOnBatteries = false;
114 definition.Settings.StopIfGoingOnBatteries = false;
115 definition.Settings.ExecutionTimeLimit = "PT0S";
117 ILogonTrigger trigger = (ILogonTrigger)definition.Triggers.Create(
118 TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON);
120 IExecAction action = (IExecAction)definition.Actions.Create(
121 TASK_ACTION_TYPE.TASK_ACTION_EXEC);
122 action.Path = Application.ExecutablePath;
123 action.WorkingDirectory =
124 Path.GetDirectoryName(Application.ExecutablePath);
126 ITaskFolder root = scheduler.GetFolder("\\");
129 folder = root.GetFolder("Open Hardware Monitor");
130 } catch (IOException) {
131 folder = root.CreateFolder("Open Hardware Monitor", "");
133 folder.RegisterTaskDefinition("Startup", definition,
134 (int)TASK_CREATION.TASK_CREATE_OR_UPDATE, null, null,
135 TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN, "");
138 private void DeleteSchedulerTask() {
139 ITaskFolder root = scheduler.GetFolder("\\");
141 ITaskFolder folder = root.GetFolder("Open Hardware Monitor");
142 folder.DeleteTask("Startup", 0);
143 } catch (IOException) { }
145 root.DeleteFolder("Open Hardware Monitor", 0);
146 } catch (IOException) { }
149 private void CreateRegistryRun() {
150 RegistryKey key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN);
151 key.SetValue("OpenHardwareMonitor", Application.ExecutablePath);
154 private void DeleteRegistryRun() {
155 RegistryKey key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN);
156 key.DeleteValue("OpenHardwareMonitor");
159 public bool IsAvailable {
160 get { return isAvailable; }
163 public bool Startup {
168 if (startup != value) {
170 if (scheduler != null) {
172 CreateSchedulerTask();
174 DeleteSchedulerTask();
183 } catch (UnauthorizedAccessException) {
184 throw new InvalidOperationException();
188 throw new InvalidOperationException();