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