Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6362 was 6257, checked in by ascheibe, 14 years ago

#1233

  • added UAC self elevation for start/stop of windows service
  • added slave states and simplified ui commands
File size: 2.8 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
29      mw.Content = new SlaveItem();
30
31      Application.Run();
32    }
33
34    /// <summary>
35    /// Gets the owner of a process
36    /// (based on http://www.codeproject.com/KB/cs/processownersid.aspx)
37    /// </summary>   
38    public static string GetProcessOwner(int pid) {
39      string ownerSID = String.Empty;
40      string processName = String.Empty;
41      try {
42        ObjectQuery sq = new ObjectQuery
43            ("Select * from Win32_Process Where ProcessID = '" + pid + "'");
44        ManagementObjectSearcher searcher = new ManagementObjectSearcher(sq);
45        if (searcher.Get().Count == 0)
46          return ownerSID;
47        foreach (ManagementObject oReturn in searcher.Get()) {
48          string[] sid = new String[1];
49          oReturn.InvokeMethod("GetOwnerSid", (object[])sid);
50          ownerSID = sid[0];
51          return ownerSID;
52        }
53      }
54      catch {
55        return ownerSID;
56      }
57      return ownerSID;
58    }
59
60    /// <summary>
61    /// kill all other slave tray icons, we only want 1 running at a time
62    /// (and if a newer version is installed the older one should be killed)
63    /// </summary>
64    private static void KillOtherInstances() {
65      String currentUserSID = WindowsIdentity.GetCurrent().User.Value;
66      Process curProc = Process.GetCurrentProcess();
67
68      try {
69        Process[] procs = Process.GetProcessesByName(curProc.ProcessName);
70        foreach (Process p in procs) {
71          if (p.Id != curProc.Id) {
72            String procUserSID = GetProcessOwner(p.Id);
73            if (procUserSID == currentUserSID) {
74              p.Kill();
75            }
76          }
77        }
78      }
79      catch (InvalidOperationException) { }
80      catch (Exception) {
81        MessageBox.Show("There is another instance of the Hive Slave tray icon running which can't be closed.", "HeuristicLab Hive Slave");
82      }
83    }
84  }
85}
Note: See TracBrowser for help on using the repository browser.