[6983] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16057] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[6983] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Diagnostics;
|
---|
| 24 | using System.Runtime.InteropServices;
|
---|
| 25 | using System.Security.Principal;
|
---|
| 26 | using System.ServiceProcess;
|
---|
| 27 | using System.Windows.Forms;
|
---|
| 28 | using HeuristicLab.Clients.Hive.SlaveCore.Views.Properties;
|
---|
| 29 | using HeuristicLab.Common;
|
---|
| 30 | using HeuristicLab.MainForm;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.Clients.Hive.SlaveCore.Views {
|
---|
| 33 |
|
---|
| 34 | [View("HeuristicLab Slave Cmds with Kill View")]
|
---|
| 35 | [Content(typeof(SlaveItem), IsDefaultView = false)]
|
---|
| 36 | public partial class SlaveCmdsWithKill : SlaveCmdsBase {
|
---|
| 37 | private string serviceName = Settings.Default.ServiceName;
|
---|
| 38 |
|
---|
| 39 | private const UInt32 BCM_SETSHIELD = 0x160C;
|
---|
| 40 | [DllImport("user32", CharSet = CharSet.Auto, SetLastError = true)]
|
---|
| 41 | static extern int SendMessage(IntPtr hWnd, UInt32 Msg, int wParam, IntPtr lParam);
|
---|
| 42 |
|
---|
| 43 |
|
---|
| 44 | public new SlaveItem Content {
|
---|
| 45 | get { return (SlaveItem)base.Content; }
|
---|
| 46 | set {
|
---|
| 47 | if (base.Content != value) {
|
---|
| 48 | base.Content = value;
|
---|
| 49 | }
|
---|
| 50 | }
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | public SlaveCmdsWithKill() {
|
---|
| 54 | InitializeComponent();
|
---|
| 55 |
|
---|
| 56 | if (CheckRunAsAdmin()) {
|
---|
| 57 | btnKill.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
|
---|
| 58 | btnKill.Image = HeuristicLab.Common.Resources.VSImageLibrary.Stop;
|
---|
| 59 | } else {
|
---|
| 60 | this.btnKill.FlatStyle = FlatStyle.System;
|
---|
| 61 | SendMessage(btnKill.Handle, BCM_SETSHIELD, 0, (IntPtr)1);
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | #region Register Content Events
|
---|
| 66 | protected override void DeregisterContentEvents() {
|
---|
| 67 | base.DeregisterContentEvents();
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | protected override void RegisterContentEvents() {
|
---|
| 71 | base.RegisterContentEvents();
|
---|
| 72 | }
|
---|
| 73 | #endregion
|
---|
| 74 |
|
---|
| 75 | protected override void Content_SlaveDisplayStateChanged(object sender, EventArgs<SlaveDisplayStat> e) {
|
---|
| 76 | base.Content_SlaveDisplayStateChanged(sender, e);
|
---|
| 77 |
|
---|
| 78 | if (e.Value == SlaveDisplayStat.Asleep || e.Value == SlaveDisplayStat.NoService) {
|
---|
| 79 | btnKill.Enabled = false;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | if (e.Value == SlaveDisplayStat.Busy || e.Value == SlaveDisplayStat.Idle || e.Value == SlaveDisplayStat.Offline) {
|
---|
| 83 | btnKill.Enabled = true;
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | protected override void Content_CoreConnectionChanged(object sender, EventArgs<CoreConnection> e) {
|
---|
| 88 | base.Content_CoreConnectionChanged(sender, e);
|
---|
| 89 | if (e.Value == CoreConnection.Offline) {
|
---|
| 90 | btnKill.Enabled = false;
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | private bool CheckRunAsAdmin() {
|
---|
| 95 | bool isRunAsAdmin = false;
|
---|
| 96 | WindowsIdentity user = WindowsIdentity.GetCurrent();
|
---|
| 97 | WindowsPrincipal principal = new WindowsPrincipal(user);
|
---|
| 98 |
|
---|
| 99 | try {
|
---|
| 100 | isRunAsAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator);
|
---|
| 101 | }
|
---|
| 102 | catch { }
|
---|
| 103 | return isRunAsAdmin;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | /// <summary>
|
---|
| 107 | /// Shows the windows UAC dialog and restarts tray icon app
|
---|
| 108 | /// </summary>
|
---|
| 109 | private void ElevateApplication() {
|
---|
| 110 | // launch itself as administrator
|
---|
| 111 | ProcessStartInfo proc = new ProcessStartInfo(Application.ExecutablePath, Settings.Default.ShowUICmd);
|
---|
| 112 | proc.UseShellExecute = true;
|
---|
| 113 | proc.WorkingDirectory = Environment.CurrentDirectory;
|
---|
| 114 | proc.FileName = Application.ExecutablePath;
|
---|
| 115 | proc.Verb = "runas";
|
---|
| 116 |
|
---|
| 117 | try {
|
---|
| 118 | Process.Start(proc);
|
---|
| 119 | }
|
---|
| 120 | catch {
|
---|
| 121 | // user refused to allow privileges elevation
|
---|
| 122 | return;
|
---|
| 123 | }
|
---|
| 124 | Application.Exit();
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | private void StartService() {
|
---|
| 128 | ServiceController service = new ServiceController(serviceName);
|
---|
| 129 | try {
|
---|
| 130 | if (service.Status == ServiceControllerStatus.Running) {
|
---|
| 131 | service.Stop();
|
---|
[7135] | 132 | service.WaitForStatus(ServiceControllerStatus.Stopped, Settings.Default.ServiceStartStopTimeout);
|
---|
[6983] | 133 | }
|
---|
| 134 |
|
---|
| 135 | service.Start();
|
---|
[7135] | 136 | service.WaitForStatus(ServiceControllerStatus.Running, Settings.Default.ServiceStartStopTimeout);
|
---|
[6983] | 137 | }
|
---|
| 138 | catch (InvalidOperationException ex) {
|
---|
| 139 | MessageBox.Show("Error starting service: Hive Slave Service not found!" + Environment.NewLine + ex.ToString());
|
---|
| 140 | }
|
---|
| 141 | catch (Exception ex) {
|
---|
| 142 | MessageBox.Show("Error starting service, exception is: " + Environment.NewLine + ex.ToString());
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | private void StopService() {
|
---|
| 147 | ServiceController service = new ServiceController(serviceName);
|
---|
| 148 | try {
|
---|
| 149 | if (service.Status == ServiceControllerStatus.Running) {
|
---|
| 150 | service.Stop();
|
---|
[7135] | 151 | service.WaitForStatus(ServiceControllerStatus.Stopped, Settings.Default.ServiceStartStopTimeout);
|
---|
[6983] | 152 | }
|
---|
| 153 | }
|
---|
| 154 | catch (InvalidOperationException ex) {
|
---|
[7135] | 155 | MessageBox.Show("Error stopping service: Hive Slave Service not found!" + Environment.NewLine + ex.ToString());
|
---|
[6983] | 156 | }
|
---|
| 157 | catch (Exception ex) {
|
---|
[7135] | 158 | MessageBox.Show("Error stopping service, exception is: " + Environment.NewLine + ex.ToString());
|
---|
[6983] | 159 | }
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | protected override void OnContentChanged() {
|
---|
| 163 | base.OnContentChanged();
|
---|
| 164 | btnKill.Enabled = false;
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | protected override void SetEnabledStateOfControls() {
|
---|
| 168 | base.SetEnabledStateOfControls();
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | private void btnKill_Click(object sender, EventArgs e) {
|
---|
| 172 | if (CheckRunAsAdmin()) {
|
---|
| 173 | StopService();
|
---|
| 174 | } else {
|
---|
| 175 | ElevateApplication();
|
---|
| 176 | }
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | protected override void btnStart_Click(object sender, EventArgs e) {
|
---|
| 180 | if (Content != null) {
|
---|
| 181 | if (lastSlaveDisplayStat == SlaveDisplayStat.Asleep) {
|
---|
| 182 | Content.RestartCore();
|
---|
| 183 | } else if (lastSlaveDisplayStat == SlaveDisplayStat.NoService) {
|
---|
| 184 | if (CheckRunAsAdmin()) {
|
---|
| 185 | StartService();
|
---|
| 186 | } else {
|
---|
| 187 | ElevateApplication();
|
---|
| 188 | }
|
---|
| 189 | }
|
---|
| 190 | }
|
---|
| 191 | }
|
---|
| 192 | }
|
---|
| 193 | }
|
---|