[5314] | 1 | using System;
|
---|
[5780] | 2 | using System.Diagnostics;
|
---|
[6116] | 3 | using System.Management;
|
---|
| 4 | using System.Security.Principal;
|
---|
[5314] | 5 | using System.Windows.Forms;
|
---|
[5599] | 6 | using HeuristicLab.Clients.Hive.SlaveCore.Views;
|
---|
[5314] | 7 |
|
---|
[5599] | 8 | namespace HeuristicLab.Clients.Hive.SlaveCore.SlaveTrayIcon {
|
---|
[5314] | 9 | static class Program {
|
---|
| 10 | /// <summary>
|
---|
| 11 | /// The main entry point for the application.
|
---|
| 12 | /// </summary>
|
---|
| 13 | [STAThread]
|
---|
[6257] | 14 | static void Main(string[] args) {
|
---|
| 15 | if (args.Length < 1 || (args.Length > 0 && args[0] != "showui")) {
|
---|
| 16 | KillOtherInstances();
|
---|
| 17 | }
|
---|
[5780] | 18 |
|
---|
[5314] | 19 | Application.EnableVisualStyles();
|
---|
| 20 | Application.SetCompatibleTextRenderingDefault(false);
|
---|
| 21 | MainWindow mw = new MainWindow();
|
---|
[6257] | 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 |
|
---|
[5314] | 28 | mw.Content = new SlaveItem();
|
---|
| 29 | Application.Run();
|
---|
| 30 | }
|
---|
[5780] | 31 |
|
---|
| 32 | /// <summary>
|
---|
[6116] | 33 | /// Gets the owner of a process
|
---|
| 34 | /// (based on http://www.codeproject.com/KB/cs/processownersid.aspx)
|
---|
| 35 | /// </summary>
|
---|
[6371] | 36 | private static string GetProcessOwner(int pid) {
|
---|
[6116] | 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>
|
---|
[6371] | 59 | /// Kill all other slave tray icons, we only want 1 running at a time
|
---|
[5780] | 60 | /// (and if a newer version is installed the older one should be killed)
|
---|
| 61 | /// </summary>
|
---|
| 62 | private static void KillOtherInstances() {
|
---|
[6116] | 63 | String currentUserSID = WindowsIdentity.GetCurrent().User.Value;
|
---|
[5780] | 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) {
|
---|
[6116] | 70 | String procUserSID = GetProcessOwner(p.Id);
|
---|
| 71 | if (procUserSID == currentUserSID) {
|
---|
| 72 | p.Kill();
|
---|
| 73 | }
|
---|
[5780] | 74 | }
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
[6116] | 77 | catch (InvalidOperationException) { }
|
---|
[5780] | 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 | }
|
---|
[5314] | 82 | }
|
---|
| 83 | }
|
---|