private static void CheckIfAlreadyRunning() { try { if (ProcessUtils.IsAlreadyRunning("Global\\BlueStacks_DiskCompactor_Lockbgp")) { Logger.Info("Disk compaction is running in background"); foreach (string str in GetProcessExecutionPath.GetApplicationPath(Process.GetProcessesByName("DiskCompactionTool"))) { if (str.Equals(Path.Combine(RegistryStrings.InstallDir, "DiskCompactionTool.exe"), StringComparison.InvariantCultureIgnoreCase)) { CustomMessageWindow customMessageWindow = new CustomMessageWindow() { ImageName = "ProductLogo" }; customMessageWindow.TitleTextBlock.Text = string.Format((IFormatProvider)CultureInfo.InvariantCulture, "{0}", (object)LocaleStrings.GetLocalizedString("STRING_EXIT_BLUESTACKS_DUE_TO_DISK_COMPACTION_HEADING", "")); customMessageWindow.BodyTextBlock.Text = string.Format((IFormatProvider)CultureInfo.InvariantCulture, "{0}", (object)LocaleStrings.GetLocalizedString("STRING_EXIT_BLUESTACKS_DUE_TO_DISK_COMPACTION_MESSAGE", "")); customMessageWindow.AddButton(ButtonColors.Blue, "STRING_OK", (EventHandler)null, (string)null, false, (object)null, true); customMessageWindow.CloseButtonHandle((Predicate <object>)null, (object)null); customMessageWindow.ShowDialog(); Logger.Info("Disk compaction running for this instance. Exiting this instance"); App.ExitApplication(); } } } } catch (Exception ex) { Logger.Error("Failed to check if disk compaction is running: " + ex.Message); } string runningProcName; if (ProcessUtils.IsAnyInstallerProcesRunning(out runningProcName) && !string.IsNullOrEmpty(runningProcName)) { Logger.Info(runningProcName + " process is running. Exiting BlueStacks MultiInstance Manager"); Environment.Exit(-1); } if (!ProcessUtils.CheckAlreadyRunningAndTakeLock("Global\\BlueStacks_MultiInstanceManager_Lockbgp", out App.sMultiInstanceManagerLock)) { return; } Utils.BringToFront("BlueStacks Multi-Instance Manager"); App.ExitApplication(); }
public static void KillProcessByNameIgnoreDirectory(string name, string IgnoreDirectory) { foreach (Process process in Process.GetProcessesByName(name)) { string path1 = ""; try { if (SystemUtils.IsOSWinXP()) { if (SystemUtils.IsAdministrator()) { path1 = process.MainModule.FileName.ToString(); } } else { path1 = GetProcessExecutionPath.GetExecutablePathAboveVista(new UIntPtr((uint)process.Id)); } } catch (Win32Exception ex) { Logger.Error("got the excpetion {0}", (object)ex.ToString()); Logger.Error("NativeErrorCode {0}", (object)ex.NativeErrorCode); Logger.Error("Exception Message {0}", (object)ex.Message); Logger.Info("giving the exit code to start as admin"); Environment.Exit(2); } catch (Exception ex) { Logger.Error("got exception: err {0}", (object)ex.ToString()); } if (!string.IsNullOrEmpty(path1)) { string path2 = Directory.GetParent(path1).ToString(); Logger.Debug("The Process Dir is {0}", (object)path2); if (string.Compare(Path.GetFullPath(IgnoreDirectory).TrimEnd('\\'), Path.GetFullPath(path2).TrimEnd('\\'), StringComparison.InvariantCultureIgnoreCase) == 0) { Logger.Debug("process:{0} not killed since the process Dir:{1} and Ignore Dir:{2} are same", (object)process.ProcessName, (object)path2, (object)IgnoreDirectory); } else { if (name.Equals("BlueStacks")) { string path3 = ""; try { path3 = RegistryManager.Instance.ClientInstallDir; } catch { } if (!string.IsNullOrEmpty(path3)) { if (string.Compare(Path.GetFullPath(path3).TrimEnd('\\'), Path.GetFullPath(path2).TrimEnd('\\'), StringComparison.InvariantCultureIgnoreCase) == 0) { Logger.Debug("process:{0} not killed since the process Dir:{1} and Ignore Dir:{2} are same", (object)process.ProcessName, (object)path2, (object)IgnoreDirectory); continue; } } } Logger.Info("Killing PID " + process.Id.ToString() + " -> " + process.ProcessName); try { process.Kill(); } catch (Exception ex) { Logger.Error(ex.ToString()); continue; } if (!process.WaitForExit(5000)) { Logger.Info("Timeout waiting for process to die"); } } } } }
private static void CheckIfAlreadyRunning() { try { if (ProcessUtils.IsAlreadyRunning("Global\\BlueStacks_DiskCompactor_Lockbgp")) { Logger.Info("Disk compaction is running in background"); foreach (string str in GetProcessExecutionPath.GetApplicationPath(Process.GetProcessesByName("DiskCompactionTool"))) { if (str.Equals(Path.Combine(RegistryStrings.InstallDir, "DiskCompactionTool.exe"), StringComparison.InvariantCultureIgnoreCase)) { CustomMessageWindow customMessageWindow = new CustomMessageWindow(); customMessageWindow.ImageName = "ProductLogo"; customMessageWindow.TitleTextBlock.Text = LocaleStrings.GetLocalizedString("STRING_EXIT_BLUESTACKS_DUE_TO_DISK_COMPACTION_HEADING", ""); customMessageWindow.BodyTextBlock.Text = LocaleStrings.GetLocalizedString("STRING_EXIT_BLUESTACKS_DUE_TO_DISK_COMPACTION_MESSAGE", ""); customMessageWindow.AddButton(ButtonColors.Blue, "STRING_OK", (EventHandler)null, (string)null, false, (object)null, true); customMessageWindow.CloseButtonHandle((Predicate <object>)null, (object)null); customMessageWindow.ShowDialog(); Logger.Info("Disk compaction running for this instance. Exiting this instance"); App.ExitApplication(); } } } } catch (Exception ex) { Logger.Error("Failed to check if disk compaction is running: " + ex.Message); } string runningProcName; if (!Opt.Instance.force && ProcessUtils.IsAnyInstallerProcesRunning(out runningProcName) && !string.IsNullOrEmpty(runningProcName)) { Logger.Info(runningProcName + " process is running. Exiting BlueStacks"); App.ExitApplication(); } if (ProcessUtils.CheckAlreadyRunningAndTakeLock("Global\\BlueStacks_BlueStacksUI_Lockbgp", out App.mBluestacksUILock)) { try { Logger.Info("Relaunching client for vm : " + Opt.Instance.vmname); Dictionary <string, string> data = new Dictionary <string, string>() { { "vmname", Opt.Instance.vmname }, { "hidden", Opt.Instance.h.ToString((IFormatProvider)CultureInfo.InvariantCulture) } }; if (Opt.Instance.launchedFromSysTray) { data.Add("all", "True"); } if (!string.IsNullOrEmpty(Opt.Instance.Json)) { data.Add("json", Opt.Instance.Json); Logger.Debug("OpenPackage result: " + HTTPUtils.SendRequestToClient("openPackage", data, Opt.Instance.vmname, 0, (Dictionary <string, string>)null, false, 1, 0, "bgp")); } else { Logger.Debug("ShowWindow result: " + HTTPUtils.SendRequestToClient("showWindow", data, Opt.Instance.vmname, 0, (Dictionary <string, string>)null, false, 1, 0, "bgp")); } } catch (Exception ex) { Logger.Error(ex.ToString()); } Logger.Info("BlueStacksUI already running. Exiting this instance"); App.ExitApplication(); } else { try { Logger.Debug("Checking for existing process not exited"); List <Process> list = ((IEnumerable <Process>)Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName)).ToList <Process>(); if (!ProcessUtils.IsLockInUse("Global\\BlueStacks_BlueStacksUI_Closing_Lockbgp")) { return; } foreach (Process process in list) { if (process.Id != Process.GetCurrentProcess().Id) { process.Kill(); } } } catch (Exception ex) { Logger.Warning("Ignoring error closing previous instances" + ex.ToString()); } } }