Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Slave.SlaveTrayIcon/Program.cs @ 6373

Last change on this file since 6373 was 6371, checked in by ascheibe, 13 years ago

#1233

  • code cleanups for slave review
  • added switch between privileged and unprivileged sandbox
  • removed childjob management because it's not used
File size: 2.7 KB
Line 
1using System;
2using System.Diagnostics;
3using System.Management;
4using System.Security.Principal;
5using System.Windows.Forms;
6using HeuristicLab.Clients.Hive.SlaveCore.Views;
7
8namespace 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}
Note: See TracBrowser for help on using the repository browser.