1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
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.BreakpointActive;
|
---|
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 | TimeSpan timeout = TimeSpan.FromMilliseconds(5000);
|
---|
129 |
|
---|
130 | ServiceController service = new ServiceController(serviceName);
|
---|
131 | try {
|
---|
132 | if (service.Status == ServiceControllerStatus.Running) {
|
---|
133 | service.Stop();
|
---|
134 | service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
|
---|
135 | }
|
---|
136 |
|
---|
137 | service.Start();
|
---|
138 | service.WaitForStatus(ServiceControllerStatus.Running, timeout);
|
---|
139 | }
|
---|
140 | catch (InvalidOperationException ex) {
|
---|
141 | MessageBox.Show("Error starting service: Hive Slave Service not found!" + Environment.NewLine + ex.ToString());
|
---|
142 | }
|
---|
143 | catch (Exception ex) {
|
---|
144 | MessageBox.Show("Error starting service, exception is: " + Environment.NewLine + ex.ToString());
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 | private void StopService() {
|
---|
149 | TimeSpan timeout = TimeSpan.FromMilliseconds(7000);
|
---|
150 |
|
---|
151 | ServiceController service = new ServiceController(serviceName);
|
---|
152 | try {
|
---|
153 | if (service.Status == ServiceControllerStatus.Running) {
|
---|
154 | service.Stop();
|
---|
155 | service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
|
---|
156 | }
|
---|
157 | }
|
---|
158 | catch (InvalidOperationException ex) {
|
---|
159 | MessageBox.Show("Error starting service: Hive Slave Service not found!" + Environment.NewLine + ex.ToString());
|
---|
160 | }
|
---|
161 | catch (Exception ex) {
|
---|
162 | MessageBox.Show("Error starting service, exception is: " + Environment.NewLine + ex.ToString());
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | protected override void OnContentChanged() {
|
---|
167 | base.OnContentChanged();
|
---|
168 | btnKill.Enabled = false;
|
---|
169 | }
|
---|
170 |
|
---|
171 | protected override void SetEnabledStateOfControls() {
|
---|
172 | base.SetEnabledStateOfControls();
|
---|
173 | }
|
---|
174 |
|
---|
175 | private void btnKill_Click(object sender, EventArgs e) {
|
---|
176 | if (CheckRunAsAdmin()) {
|
---|
177 | StopService();
|
---|
178 | } else {
|
---|
179 | ElevateApplication();
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 | protected override void btnStart_Click(object sender, EventArgs e) {
|
---|
184 | if (Content != null) {
|
---|
185 | if (lastSlaveDisplayStat == SlaveDisplayStat.Asleep) {
|
---|
186 | Content.RestartCore();
|
---|
187 | } else if (lastSlaveDisplayStat == SlaveDisplayStat.NoService) {
|
---|
188 | if (CheckRunAsAdmin()) {
|
---|
189 | StartService();
|
---|
190 | } else {
|
---|
191 | ElevateApplication();
|
---|
192 | }
|
---|
193 | }
|
---|
194 | }
|
---|
195 | }
|
---|
196 | }
|
---|
197 | }
|
---|