public static ActionResult CloseGwupe(Session session) { session.Log("Begin terminate Gwupe"); // send messages to all blitsme agents OsUtils.PostMessage((IntPtr)OsUtils.HWND_BROADCAST, OsUtils.WM_SHUTDOWNGWUPE, #if DEBUG IntPtr.Zero, #else new IntPtr(1), #endif IntPtr.Zero); // now we kill if necessary Process[] prs = Process.GetProcesses(); foreach (Process pr in prs) { try { if (pr.ProcessName == "BlitsMe.Agent" && (OsUtils.ProgramFilesx86 + "\\BlitsMe" + BuildMarker + "\\BlitsMe.Agent.exe") .Equals(OsUtils.GetMainModuleFilepath(pr.Id))) { session.Log("Agent is still alive and kicking so we gonna kill 'im after 5 seconds"); if (!pr.WaitForExit(5000)) { session.Log("Agent: times up"); try { pr.Kill(); } catch (Exception e) { session.Log("CloseBlitsMeAgent caught exception : " + e.Message + "\n" + e); } } } if (pr.ProcessName == "Gwupe.Agent" && (OsUtils.ProgramFilesx86 + "\\Gwupe" + BuildMarker + "\\Gwupe.Agent.exe") .Equals(OsUtils.GetMainModuleFilepath(pr.Id))) { session.Log("Gwupe is still alive and kicking so we gonna kill 'im after 5 seconds"); if (!pr.WaitForExit(5000)) { session.Log("Gwupe: times up"); try { pr.Kill(); } catch (Exception e) { session.Log("CloseGwupe caught exception : " + e.Message + "\n" + e); } } } } catch (Exception e) { session.Log("Problem accessing " + pr.ProcessName + " : " + e.Message); } } return(ActionResult.Success); }
static void Main() { // never run as system user if (System.Environment.UserName.Equals("SYSTEM")) { Console.WriteLine("Cannot run as system user"); return; } Process[] prs = Process.GetProcesses(); try { foreach (Process pr in prs) { Console.WriteLine(pr.ProcessName); if (pr.ProcessName == "Gwupe.Agent" || pr.ProcessName == "BlitsMe.Agent") { // If Gwupe was started by an elevated process we can't get the module path. String path = OsUtils.GetMainModuleFilepath(pr.Id); String owner = OsUtils.GetProcessOwner(pr.Id); Console.WriteLine("Testing " + pr.ProcessName + ", " + path + " owned by " + owner); if (path != null) { if ( ((OsUtils.ProgramFilesx86 + "\\Gwupe" + BuildMarker + "\\Gwupe.Agent.exe").Equals(path) || (OsUtils.ProgramFilesx86 + "\\BlitsMe" + BuildMarker + "\\BlitsMe.Agent.exe").Equals( path)) && (Environment.UserDomainName + "\\" + Environment.UserName).Equals(owner)) { Console.WriteLine("Killing " + pr.Id + "(" + pr.ProcessName + ")"); try { if (!pr.WaitForExit(20000)) { pr.Kill(); } } catch (Exception e) { Console.WriteLine("Failed to kill " + pr.Id + " : " + e.Message); } } } else { // We have an elevated process, we need to wait for it to die (its quitting internally) Console.WriteLine("Elevated process found, we will wait for it to die just in case"); try { pr.WaitForExit(20000); } catch (Exception e) { Console.WriteLine("Failed to wait for process to die : " + e.Message); } } } } } catch (Exception e) { Console.WriteLine("Failed to kill other instances of BlitsMe or Gwupe : " + e.Message); } // Now start Gwupe try { //string fileName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location.Replace("BlitsMe", "Gwupe")) + "\\Gwupe.Agent.exe"; string fileName = OsUtils.ProgramFilesx86 + "\\Gwupe" + BuildMarker + "\\Gwupe.Agent.exe"; Process.Start(fileName); Console.WriteLine("Started " + fileName); } catch (Exception e) { Console.WriteLine("Failed to execute Gwupe.exe : " + e.Message); } }