1 | #region License Information |
---|
2 | /* HeuristicLab |
---|
3 | * Copyright (C) 2002-2018 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.Management; |
---|
25 | using System.Security.Principal; |
---|
26 | using System.Windows.Forms; |
---|
27 | using HeuristicLab.Clients.Hive.SlaveCore.Views; |
---|
28 | using HeuristicLab.Clients.Hive.SlaveCore.Views.Properties; |
---|
29 | |
---|
30 | namespace HeuristicLab.Clients.Hive.SlaveCore.SlaveTrayIcon { |
---|
31 | static class Program { |
---|
32 | /// <summary> |
---|
33 | /// The main entry point for the application. |
---|
34 | /// </summary> |
---|
35 | [STAThread] |
---|
36 | static void Main(string[] args) { |
---|
37 | if (args.Length < 1 || (args.Length > 0 && args[0] != Settings.Default.ShowUICmd)) { |
---|
38 | KillOtherInstances(); |
---|
39 | } |
---|
40 | |
---|
41 | Application.EnableVisualStyles(); |
---|
42 | Application.SetCompatibleTextRenderingDefault(false); |
---|
43 | MainWindow mw = new MainWindow(); |
---|
44 | if (args.Length < 1 || (args.Length > 0 && args[0] != Settings.Default.ShowUICmd)) { |
---|
45 | mw.Hide(); |
---|
46 | } else if (args.Length > 0 && args[0] == Settings.Default.ShowUICmd) { |
---|
47 | mw.Show(); |
---|
48 | } |
---|
49 | |
---|
50 | mw.Content = new SlaveItem(); |
---|
51 | Application.Run(); |
---|
52 | } |
---|
53 | |
---|
54 | /// <summary> |
---|
55 | /// Gets the owner of a process |
---|
56 | /// (based on http://www.codeproject.com/KB/cs/processownersid.aspx) |
---|
57 | /// </summary> |
---|
58 | private static string GetProcessOwner(int pid) { |
---|
59 | string ownerSID = String.Empty; |
---|
60 | string processName = String.Empty; |
---|
61 | try { |
---|
62 | ObjectQuery sq = new ObjectQuery |
---|
63 | ("Select * from Win32_Process Where ProcessID = '" + pid + "'"); |
---|
64 | ManagementObjectSearcher searcher = new ManagementObjectSearcher(sq); |
---|
65 | if (searcher.Get().Count == 0) |
---|
66 | return ownerSID; |
---|
67 | foreach (ManagementObject oReturn in searcher.Get()) { |
---|
68 | string[] sid = new String[1]; |
---|
69 | oReturn.InvokeMethod("GetOwnerSid", (object[])sid); |
---|
70 | ownerSID = sid[0]; |
---|
71 | return ownerSID; |
---|
72 | } |
---|
73 | } |
---|
74 | catch { |
---|
75 | return ownerSID; |
---|
76 | } |
---|
77 | return ownerSID; |
---|
78 | } |
---|
79 | |
---|
80 | /// <summary> |
---|
81 | /// Kill all other slave tray icons, we only want 1 running at a time |
---|
82 | /// (and if a newer version is installed the older one should be killed) |
---|
83 | /// </summary> |
---|
84 | private static void KillOtherInstances() { |
---|
85 | String currentUserSID = WindowsIdentity.GetCurrent().User.Value; |
---|
86 | Process curProc = Process.GetCurrentProcess(); |
---|
87 | |
---|
88 | try { |
---|
89 | Process[] procs = Process.GetProcessesByName(curProc.ProcessName); |
---|
90 | foreach (Process p in procs) { |
---|
91 | if (p.Id != curProc.Id) { |
---|
92 | String procUserSID = GetProcessOwner(p.Id); |
---|
93 | if (procUserSID == currentUserSID) { |
---|
94 | p.Kill(); |
---|
95 | } |
---|
96 | } |
---|
97 | } |
---|
98 | } |
---|
99 | catch (InvalidOperationException) { } |
---|
100 | catch (Exception) { |
---|
101 | MessageBox.Show("There is another instance of the Hive Slave tray icon running which can't be closed.", "HeuristicLab Hive Slave"); |
---|
102 | } |
---|
103 | } |
---|
104 | } |
---|
105 | } |
---|