GUI/StartupManager.cs
author moel.mich
Tue, 18 May 2010 19:16:55 +0000
changeset 121 f7e492b43690
parent 104 4a9f8154d8f0
child 123 912a06e2bd53
permissions -rw-r--r--
Fixed an EntryPointNotFoundException in the NVAPI wrapper:

System.EntryPointNotFoundException: N?o ? poss?vel localizar um ponto de entrada denominado 'nvapi_QueryInterface' na DLL 'nvapi.dll'.
em PInvokeDelegateFactoryInternalWrapperType38.nvapi_QueryInterface(UInt32 id)
em OpenHardwareMonitor.Hardware.Nvidia.NVAPI.GetDelegate[T](UInt32 id, T& newDelegate)
em OpenHardwareMonitor.Hardware.Nvidia.NVAPI..cctor()
moel@82
     1
/*
moel@82
     2
  
moel@82
     3
  Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@82
     4
moel@82
     5
  The contents of this file are subject to the Mozilla Public License Version
moel@82
     6
  1.1 (the "License"); you may not use this file except in compliance with
moel@82
     7
  the License. You may obtain a copy of the License at
moel@82
     8
 
moel@82
     9
  http://www.mozilla.org/MPL/
moel@82
    10
moel@82
    11
  Software distributed under the License is distributed on an "AS IS" basis,
moel@82
    12
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@82
    13
  for the specific language governing rights and limitations under the License.
moel@82
    14
moel@82
    15
  The Original Code is the Open Hardware Monitor code.
moel@82
    16
moel@82
    17
  The Initial Developer of the Original Code is 
moel@82
    18
  Michael Möller <m.moeller@gmx.ch>.
moel@82
    19
  Portions created by the Initial Developer are Copyright (C) 2009-2010
moel@82
    20
  the Initial Developer. All Rights Reserved.
moel@82
    21
moel@82
    22
  Contributor(s):
moel@82
    23
moel@82
    24
  Alternatively, the contents of this file may be used under the terms of
moel@82
    25
  either the GNU General Public License Version 2 or later (the "GPL"), or
moel@82
    26
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@82
    27
  in which case the provisions of the GPL or the LGPL are applicable instead
moel@82
    28
  of those above. If you wish to allow use of your version of this file only
moel@82
    29
  under the terms of either the GPL or the LGPL, and not to allow others to
moel@82
    30
  use your version of this file under the terms of the MPL, indicate your
moel@82
    31
  decision by deleting the provisions above and replace them with the notice
moel@82
    32
  and other provisions required by the GPL or the LGPL. If you do not delete
moel@82
    33
  the provisions above, a recipient may use your version of this file under
moel@82
    34
  the terms of any one of the MPL, the GPL or the LGPL.
moel@82
    35
 
moel@82
    36
*/
moel@82
    37
moel@82
    38
using System;
moel@82
    39
using System.Collections.Generic;
moel@82
    40
using System.IO;
moel@82
    41
using System.Windows.Forms;
moel@82
    42
using Microsoft.Win32;
moel@82
    43
using OpenHardwareMonitor.TaskScheduler;
moel@82
    44
moel@82
    45
namespace OpenHardwareMonitor.GUI {
moel@82
    46
  public class StartupManager {
moel@82
    47
moel@82
    48
    private TaskSchedulerClass scheduler;
moel@82
    49
    private bool startup;
moel@82
    50
moel@82
    51
    private const string REGISTRY_RUN =
moel@82
    52
      @"Software\Microsoft\Windows\CurrentVersion\Run";
moel@82
    53
moel@82
    54
    public StartupManager() {
moel@82
    55
      try {
moel@82
    56
        scheduler = new TaskSchedulerClass();
moel@82
    57
        scheduler.Connect(null, null, null, null);
moel@104
    58
      } catch {
moel@82
    59
        scheduler = null;
moel@82
    60
      }
moel@82
    61
moel@82
    62
      if (scheduler != null) {
moel@82
    63
        try {
moel@82
    64
          ITaskFolder folder = scheduler.GetFolder("\\Open Hardware Monitor");
moel@82
    65
          IRegisteredTask task = folder.GetTask("Startup");
moel@82
    66
          startup = task != null;
moel@101
    67
        } catch (IOException) {
moel@82
    68
          startup = false;
moel@120
    69
        } catch (UnauthorizedAccessException) {
moel@120
    70
          scheduler = null;
moel@82
    71
        }
moel@120
    72
      } 
moel@120
    73
            
moel@120
    74
      if (scheduler == null) {
moel@82
    75
        RegistryKey key = Registry.CurrentUser.OpenSubKey(REGISTRY_RUN);
moel@82
    76
        startup = false;
moel@82
    77
        if (key != null) {
moel@82
    78
          string value = (string)key.GetValue("OpenHardwareMonitor");
moel@82
    79
          if (value != null)
moel@82
    80
            startup = value == Application.ExecutablePath;
moel@82
    81
        }
moel@82
    82
      }
moel@82
    83
    }
moel@82
    84
moel@82
    85
    private void CreateSchedulerTask() {
moel@82
    86
      ITaskDefinition definition = scheduler.NewTask(0);
moel@82
    87
      definition.RegistrationInfo.Description =
moel@82
    88
        "This task starts the Open Hardware Monitor on Windows startup.";
moel@82
    89
      definition.Principal.RunLevel =
moel@82
    90
        TASK_RUNLEVEL.TASK_RUNLEVEL_HIGHEST;
moel@82
    91
      definition.Settings.DisallowStartIfOnBatteries = false;
moel@82
    92
      definition.Settings.StopIfGoingOnBatteries = false;
moel@82
    93
      definition.Settings.ExecutionTimeLimit = "PT0S";
moel@82
    94
moel@82
    95
      ILogonTrigger trigger = (ILogonTrigger)definition.Triggers.Create(
moel@82
    96
        TASK_TRIGGER_TYPE2.TASK_TRIGGER_LOGON);
moel@82
    97
moel@82
    98
      IExecAction action = (IExecAction)definition.Actions.Create(
moel@82
    99
        TASK_ACTION_TYPE.TASK_ACTION_EXEC);
moel@82
   100
      action.Path = Application.ExecutablePath;
moel@82
   101
      action.WorkingDirectory =
moel@82
   102
        Path.GetDirectoryName(Application.ExecutablePath);
moel@82
   103
moel@82
   104
      ITaskFolder root = scheduler.GetFolder("\\");
moel@82
   105
      ITaskFolder folder;
moel@82
   106
      try {
moel@82
   107
        folder = root.GetFolder("Open Hardware Monitor");
moel@104
   108
      } catch (IOException) {
moel@82
   109
        folder = root.CreateFolder("Open Hardware Monitor", "");
moel@82
   110
      }
moel@82
   111
      folder.RegisterTaskDefinition("Startup", definition,
moel@82
   112
        (int)TASK_CREATION.TASK_CREATE_OR_UPDATE, null, null,
moel@82
   113
        TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN, "");
moel@82
   114
    }
moel@82
   115
moel@82
   116
    private void DeleteSchedulerTask() {
moel@82
   117
      ITaskFolder root = scheduler.GetFolder("\\");
moel@82
   118
      try {
moel@82
   119
        ITaskFolder folder = root.GetFolder("Open Hardware Monitor");
moel@82
   120
        folder.DeleteTask("Startup", 0);
moel@104
   121
      } catch (IOException) { }
moel@82
   122
      try {
moel@82
   123
        root.DeleteFolder("Open Hardware Monitor", 0);
moel@104
   124
      } catch (IOException) { }
moel@82
   125
    }
moel@82
   126
moel@82
   127
    private void CreateRegistryRun() {
moel@82
   128
      RegistryKey key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN);
moel@82
   129
      key.SetValue("OpenHardwareMonitor", Application.ExecutablePath);
moel@82
   130
    }
moel@82
   131
moel@82
   132
    private void DeleteRegistryRun() {
moel@82
   133
      RegistryKey key = Registry.CurrentUser.CreateSubKey(REGISTRY_RUN);
moel@82
   134
      key.DeleteValue("OpenHardwareMonitor");
moel@82
   135
    }
moel@82
   136
moel@82
   137
    public bool Startup {
moel@82
   138
      get {
moel@82
   139
        return startup;
moel@82
   140
      }
moel@82
   141
      set {
moel@82
   142
        if (startup != value) {
moel@82
   143
          startup = value;
moel@82
   144
          if (scheduler != null) {
moel@82
   145
            if (startup)
moel@82
   146
              CreateSchedulerTask();
moel@82
   147
            else
moel@82
   148
              DeleteSchedulerTask();
moel@82
   149
          } else {
moel@82
   150
            if (startup)
moel@82
   151
              CreateRegistryRun();
moel@82
   152
            else
moel@82
   153
              DeleteRegistryRun();
moel@82
   154
          }
moel@82
   155
        }
moel@82
   156
      }
moel@82
   157
    }
moel@82
   158
  }
moel@82
   159
moel@82
   160
 
moel@82
   161
moel@82
   162
 
moel@82
   163
moel@82
   164
}