Skip to content

Commit 28f062e

Browse files
committed
revert StartupManager changes (scheduler restored)
1 parent 57742b3 commit 28f062e

3 files changed

Lines changed: 1065 additions & 42 deletions

File tree

GUI/StartupManager.cs

Lines changed: 153 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,184 @@
11
using System;
2+
using System.IO;
3+
using System.Runtime.InteropServices;
24
using System.Security;
5+
using System.Security.Principal;
36
using System.Windows.Forms;
47
using Microsoft.Win32;
8+
using OpenHardwareMonitor.TaskScheduler;
59

610
namespace OpenHardwareMonitor.GUI {
7-
811
public class StartupManager {
912

13+
private TaskSchedulerClass scheduler;
1014
private bool startup;
15+
private bool isAvailable;
1116

12-
private const string REGISTRY_RUN = @"Software\Microsoft\Windows\CurrentVersion\Run";
17+
private const string REGISTRY_RUN =
18+
@"Software\Microsoft\Windows\CurrentVersion\Run";
1319

14-
// private bool IsAdministrator() {
15-
// try {
16-
// WindowsIdentity identity = WindowsIdentity.GetCurrent();
17-
// WindowsPrincipal principal = new WindowsPrincipal(identity);
18-
// return principal.IsInRole(WindowsBuiltInRole.Administrator);
19-
// } catch {
20-
// return false;
21-
// }
22-
// }
20+
private bool IsAdministrator() {
21+
try {
22+
WindowsIdentity identity = WindowsIdentity.GetCurrent();
23+
WindowsPrincipal principal = new WindowsPrincipal(identity);
24+
return principal.IsInRole(WindowsBuiltInRole.Administrator);
25+
} catch {
26+
return false;
27+
}
28+
}
2329

2430
public StartupManager() {
2531
if (Hardware.OperatingSystem.IsUnix) {
26-
IsAvailable = false;
32+
scheduler = null;
33+
isAvailable = false;
2734
return;
2835
}
2936

30-
try {
31-
using (var key = Registry.CurrentUser.OpenSubKey(REGISTRY_RUN)) {
32-
startup = false;
33-
var value = (string) key?.GetValue("OpenHardwareMonitor");
34-
if (value != null)
35-
startup = value == Application.ExecutablePath;
37+
if (IsAdministrator()) {
38+
try {
39+
scheduler = new TaskSchedulerClass();
40+
scheduler.Connect(null, null, null, null);
41+
} catch {
42+
scheduler = null;
3643
}
37-
IsAvailable = true;
38-
} catch (SecurityException) {
39-
IsAvailable = false;
44+
45+
if (scheduler != null) {
46+
try {
47+
try {
48+
// check if the taskscheduler is running
49+
IRunningTaskCollection collection = scheduler.GetRunningTasks(0);
50+
} catch (ArgumentException) { }
51+
52+
ITaskFolder folder = scheduler.GetFolder("\\Open Hardware Monitor");
53+
IRegisteredTask task = folder.GetTask("Startup");
54+
startup = (task != null) &&
55+
(task.Definition.Triggers.Count > 0) &&
56+
(task.Definition.Triggers[1].Type ==
57+
TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON) &&
58+
(task.Definition.Actions.Count > 0) &&
59+
(task.Definition.Actions[1].Type ==
60+
TASK_ACTION_TYPE.TASK_ACTION_EXEC) &&
61+
(task.Definition.Actions[1] as IExecAction != null) &&
62+
((task.Definition.Actions[1] as IExecAction).Path ==
63+
Application.ExecutablePath);
64+
65+
} catch (IOException) {
66+
startup = false;
67+
} catch (UnauthorizedAccessException) {
68+
scheduler = null;
69+
} catch (COMException) {
70+
scheduler = null;
71+
} catch (NotImplementedException) {
72+
scheduler = null;
73+
}
74+
}
75+
} else {
76+
scheduler = null;
77+
}
78+
79+
if (scheduler == null) {
80+
try {
81+
using (RegistryKey key =
82+
Registry.CurrentUser.OpenSubKey(REGISTRY_RUN)) {
83+
startup = false;
84+
if (key != null) {
85+
string value = (string)key.GetValue("OpenHardwareMonitor");
86+
if (value != null)
87+
startup = value == Application.ExecutablePath;
88+
}
89+
}
90+
isAvailable = true;
91+
} catch (SecurityException) {
92+
isAvailable = false;
93+
}
94+
} else {
95+
isAvailable = true;
4096
}
4197
}
4298

43-
private static void CreateRegistryRun() {
44-
var key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN);
45-
key?.SetValue("OpenHardwareMonitor", Application.ExecutablePath);
99+
private void CreateSchedulerTask() {
100+
ITaskDefinition definition = scheduler.NewTask(0);
101+
definition.RegistrationInfo.Description =
102+
"This task starts the Open Hardware Monitor on Windows startup.";
103+
definition.Principal.RunLevel =
104+
TASK_RUNLEVEL.TASK_RUNLEVEL_HIGHEST;
105+
definition.Settings.DisallowStartIfOnBatteries = false;
106+
definition.Settings.StopIfGoingOnBatteries = false;
107+
definition.Settings.ExecutionTimeLimit = "PT0S";
108+
109+
ILogonTrigger trigger = (ILogonTrigger)definition.Triggers.Create(
110+
TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON);
111+
112+
IExecAction action = (IExecAction)definition.Actions.Create(
113+
TASK_ACTION_TYPE.TASK_ACTION_EXEC);
114+
action.Path = Application.ExecutablePath;
115+
action.WorkingDirectory =
116+
Path.GetDirectoryName(Application.ExecutablePath);
117+
118+
ITaskFolder root = scheduler.GetFolder("\\");
119+
ITaskFolder folder;
120+
try {
121+
folder = root.GetFolder("Open Hardware Monitor");
122+
} catch (IOException) {
123+
folder = root.CreateFolder("Open Hardware Monitor", "");
124+
}
125+
folder.RegisterTaskDefinition("Startup", definition,
126+
(int)TASK_CREATION.TASK_CREATE_OR_UPDATE, null, null,
127+
TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN, "");
128+
}
129+
130+
private void DeleteSchedulerTask() {
131+
ITaskFolder root = scheduler.GetFolder("\\");
132+
try {
133+
ITaskFolder folder = root.GetFolder("Open Hardware Monitor");
134+
folder.DeleteTask("Startup", 0);
135+
} catch (IOException) { }
136+
try {
137+
root.DeleteFolder("Open Hardware Monitor", 0);
138+
} catch (IOException) { }
46139
}
47140

48-
private static void DeleteRegistryRun() {
49-
var key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN);
50-
key?.DeleteValue("OpenHardwareMonitor");
141+
private void CreateRegistryRun() {
142+
RegistryKey key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN);
143+
key.SetValue("OpenHardwareMonitor", Application.ExecutablePath);
51144
}
52145

53-
public bool IsAvailable { get; }
146+
private void DeleteRegistryRun() {
147+
RegistryKey key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN);
148+
key.DeleteValue("OpenHardwareMonitor");
149+
}
150+
151+
public bool IsAvailable {
152+
get { return isAvailable; }
153+
}
54154

55155
public bool Startup {
56-
get => startup;
156+
get {
157+
return startup;
158+
}
57159
set {
58-
if (startup == value) return;
59-
if (!IsAvailable)
60-
throw new InvalidOperationException();
61-
62-
try {
63-
if (value)
64-
CreateRegistryRun();
65-
else
66-
DeleteRegistryRun();
67-
startup = value;
68-
}
69-
catch (UnauthorizedAccessException) {
70-
throw new InvalidOperationException();
160+
if (startup != value) {
161+
if (isAvailable) {
162+
if (scheduler != null) {
163+
if (value)
164+
CreateSchedulerTask();
165+
else
166+
DeleteSchedulerTask();
167+
startup = value;
168+
} else {
169+
try {
170+
if (value)
171+
CreateRegistryRun();
172+
else
173+
DeleteRegistryRun();
174+
startup = value;
175+
} catch (UnauthorizedAccessException) {
176+
throw new InvalidOperationException();
177+
}
178+
}
179+
} else {
180+
throw new InvalidOperationException();
181+
}
71182
}
72183
}
73184
}

0 commit comments

Comments
 (0)