using System; using System.Diagnostics; using System.Management; using System.Security.Principal; using System.Windows.Forms; using HeuristicLab.Clients.Hive.SlaveCore.Views; using HeuristicLab.Clients.Hive.SlaveCore.Views.Properties; namespace HeuristicLab.Clients.Hive.SlaveCore.SlaveTrayIcon { static class Program { /// /// The main entry point for the application. /// [STAThread] static void Main(string[] args) { if (args.Length < 1 || (args.Length > 0 && args[0] != Settings.Default.ShowUICmd)) { KillOtherInstances(); } Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); MainWindow mw = new MainWindow(); if (args.Length < 1 || (args.Length > 0 && args[0] != Settings.Default.ShowUICmd)) { mw.MinimizeToTray(); } else if (args.Length > 0 && args[0] == Settings.Default.ShowUICmd) { mw.RestoreFromTray(); } mw.Content = new SlaveItem(); Application.Run(); } /// /// Gets the owner of a process /// (based on http://www.codeproject.com/KB/cs/processownersid.aspx) /// private static string GetProcessOwner(int pid) { string ownerSID = String.Empty; string processName = String.Empty; try { ObjectQuery sq = new ObjectQuery ("Select * from Win32_Process Where ProcessID = '" + pid + "'"); ManagementObjectSearcher searcher = new ManagementObjectSearcher(sq); if (searcher.Get().Count == 0) return ownerSID; foreach (ManagementObject oReturn in searcher.Get()) { string[] sid = new String[1]; oReturn.InvokeMethod("GetOwnerSid", (object[])sid); ownerSID = sid[0]; return ownerSID; } } catch { return ownerSID; } return ownerSID; } /// /// Kill all other slave tray icons, we only want 1 running at a time /// (and if a newer version is installed the older one should be killed) /// private static void KillOtherInstances() { String currentUserSID = WindowsIdentity.GetCurrent().User.Value; Process curProc = Process.GetCurrentProcess(); try { Process[] procs = Process.GetProcessesByName(curProc.ProcessName); foreach (Process p in procs) { if (p.Id != curProc.Id) { String procUserSID = GetProcessOwner(p.Id); if (procUserSID == currentUserSID) { p.Kill(); } } } } catch (InvalidOperationException) { } catch (Exception) { MessageBox.Show("There is another instance of the Hive Slave tray icon running which can't be closed.", "HeuristicLab Hive Slave"); } } } }