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