public BugReportForm(Exception ex) { InitializeComponent(); lang = Language.GetInstance(); UpdateUI(); this.textBoxDescription.Text = EnvironmentInfo.EnvironmentToString() + "\r\n" + EnvironmentInfo.ExceptionToString(ex); }
/// <summary> /// Shutdown / cleanup /// </summary> public void Exit() { Log.Info().WriteLine("Exit: " + EnvironmentInfo.EnvironmentToString(false)); ImageOutput.RemoveTmpFiles(); // make the icon invisible otherwise it stays even after exit!! if (notifyIcon != null) { notifyIcon.Visible = false; notifyIcon.Dispose(); notifyIcon = null; } }
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { try { switch (keyData) { case Keys.Escape: DialogResult = DialogResult.Cancel; break; case Keys.E: MessageBox.Show(EnvironmentInfo.EnvironmentToString(true)); break; case Keys.L: try { if (File.Exists(MainForm.LogFileLocation)) { using (Process.Start("\"" + MainForm.LogFileLocation + "\"")) { // nothing to do, just using dispose to cleanup } } else { MessageBox.Show("Greenshot can't find the logfile, it should have been here: " + MainForm.LogFileLocation); } } catch (Exception) { MessageBox.Show("Couldn't open the greenshot.log, it's located here: " + MainForm.LogFileLocation, "Error opening greeenshot.log", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } break; case Keys.I: try { using (Process.Start("\"" + IniConfig.ConfigLocation + "\"")) { } } catch (Exception) { MessageBox.Show("Couldn't open the greenshot.ini, it's located here: " + IniConfig.ConfigLocation, "Error opening greeenshot.ini", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } break; default: return(base.ProcessCmdKey(ref msg, keyData)); } } catch (Exception ex) { LOG.Error(string.Format("Error handling key '{0}'", keyData), ex); } return(true); }
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { try { switch (keyData) { case Keys.Escape: DialogResult = DialogResult.OK; break; case Keys.E: MessageBox.Show(EnvironmentInfo.EnvironmentToString(true)); break; case Keys.L: try { if (File.Exists(MainForm.LogFileLocation)) { System.Diagnostics.Process.Start("\"" + MainForm.LogFileLocation + "\""); } else { MessageBox.Show("Greenshot can't write to logfile, otherwise it would be here: " + MainForm.LogFileLocation); } } catch (Exception) { MessageBox.Show("Couldn't open the greenshot.log, it's located here: " + MainForm.LogFileLocation, "Error opening greeenshot.log", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } break; case Keys.I: try { System.Diagnostics.Process.Start("\"" + IniFile.IniConfig.ConfigLocation + "\""); } catch (Exception) { MessageBox.Show("Couldn't open the greenshot.ini, it's located here: " + IniFile.IniConfig.ConfigLocation, "Error opening greeenshot.ini", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } break; default: return(base.ProcessCmdKey(ref msg, keyData)); } } catch (Exception) { } return(true); }
public static void Start(string[] args) { bool isAlreadyRunning = false; List <string> filesToOpen = new List <string>(); // Init Log4NET LogFileLocation = LogHelper.InitializeLog4NET(); // Get logger LOG = log4net.LogManager.GetLogger(typeof(MainForm)); Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException); AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); // Log the startup LOG.Info("Starting: " + EnvironmentInfo.EnvironmentToString(false)); IniConfig.Init(); AppConfig.UpgradeToIni(); // Read configuration conf = IniConfig.GetIniSection <CoreConfiguration>(); try { // Fix for Bug 2495900, Multi-user Environment // check whether there's an local instance running already try { // Added Mutex Security, hopefully this prevents the UnauthorizedAccessException more gracefully // See an example in Bug #3131534 SecurityIdentifier sid = new SecurityIdentifier(WellKnownSidType.WorldSid, null); MutexSecurity mutexsecurity = new MutexSecurity(); mutexsecurity.AddAccessRule(new MutexAccessRule(sid, MutexRights.FullControl, AccessControlType.Allow)); mutexsecurity.AddAccessRule(new MutexAccessRule(sid, MutexRights.ChangePermissions, AccessControlType.Deny)); mutexsecurity.AddAccessRule(new MutexAccessRule(sid, MutexRights.Delete, AccessControlType.Deny)); bool created = false; // 1) Create Mutex applicationMutex = new Mutex(false, @"Local\F48E86D3-E34C-4DB7-8F8F-9A0EA55F0D08", out created, mutexsecurity); // 2) Get the right to it, this returns false if it's already locked if (!applicationMutex.WaitOne(0, false)) { LOG.Debug("Greenshot seems already to be running!"); isAlreadyRunning = true; // Clean up applicationMutex.Close(); applicationMutex = null; } } catch (AbandonedMutexException e) { // Another Greenshot instance didn't cleanup correctly! // we can ignore the exception, it happend on the "waitone" but still the mutex belongs to us LOG.Warn("Greenshot didn't cleanup correctly!", e); } catch (UnauthorizedAccessException e) { LOG.Warn("Greenshot is most likely already running for a different user in the same session, can't create mutex due to error: ", e); isAlreadyRunning = true; } catch (Exception e) { LOG.Warn("Problem obtaining the Mutex, assuming it was already taken!", e); isAlreadyRunning = true; } if (args.Length > 0 && LOG.IsDebugEnabled) { StringBuilder argumentString = new StringBuilder(); for (int argumentNr = 0; argumentNr < args.Length; argumentNr++) { argumentString.Append("[").Append(args[argumentNr]).Append("] "); } LOG.Debug("Greenshot arguments: " + argumentString.ToString()); } for (int argumentNr = 0; argumentNr < args.Length; argumentNr++) { string argument = args[argumentNr]; // Help if (argument.ToLower().Equals("/help")) { // Try to attach to the console bool attachedToConsole = Kernel32.AttachConsole(Kernel32.ATTACHCONSOLE_ATTACHPARENTPROCESS); // If attach didn't work, open a console if (!attachedToConsole) { Kernel32.AllocConsole(); } StringBuilder helpOutput = new StringBuilder(); helpOutput.AppendLine(); helpOutput.AppendLine("Greenshot commandline options:"); helpOutput.AppendLine(); helpOutput.AppendLine(); helpOutput.AppendLine("\t/help"); helpOutput.AppendLine("\t\tThis help."); helpOutput.AppendLine(); helpOutput.AppendLine(); helpOutput.AppendLine("\t/exit"); helpOutput.AppendLine("\t\tTries to close all running instances."); helpOutput.AppendLine(); helpOutput.AppendLine(); helpOutput.AppendLine("\t/reload"); helpOutput.AppendLine("\t\tReload the configuration of Greenshot."); helpOutput.AppendLine(); helpOutput.AppendLine(); helpOutput.AppendLine("\t/language [language code]"); helpOutput.AppendLine("\t\tSet the language of Greenshot, e.g. greenshot /language en-US."); helpOutput.AppendLine(); helpOutput.AppendLine(); helpOutput.AppendLine("\t[filename]"); helpOutput.AppendLine("\t\tOpen the bitmap files in the running Greenshot instance or start a new instance"); Console.WriteLine(helpOutput.ToString()); // If attach didn't work, wait for key otherwise the console will close to quickly if (!attachedToConsole) { Console.ReadKey(); } FreeMutex(); return; } if (argument.ToLower().Equals("/exit")) { // unregister application on uninstall (allow uninstall) try { LOG.Info("Sending all instances the exit command."); // Pass Exit to running instance, if any SendData(new CopyDataTransport(CommandEnum.Exit)); } catch (Exception e) { LOG.Warn("Exception by exit.", e); } FreeMutex(); return; } // Reload the configuration if (argument.ToLower().Equals("/reload")) { // Modify configuration LOG.Info("Reloading configuration!"); // Update running instances SendData(new CopyDataTransport(CommandEnum.ReloadConfig)); FreeMutex(); return; } // Stop running if (argument.ToLower().Equals("/norun")) { // Make an exit possible FreeMutex(); return; } // Language if (argument.ToLower().Equals("/language")) { conf.Language = args[++argumentNr]; IniConfig.Save(); continue; } // Files to open filesToOpen.Add(argument); } // Finished parsing the command line arguments, see if we need to do anything CopyDataTransport transport = new CopyDataTransport(); if (filesToOpen.Count > 0) { foreach (string fileToOpen in filesToOpen) { transport.AddCommand(CommandEnum.OpenFile, fileToOpen); } } if (MainForm.instance == null) { MainForm.instance = new MainForm(transport); } // if language is not set, show language dialog if (string.IsNullOrEmpty(conf.Language)) { LanguageDialog languageDialog = LanguageDialog.GetInstance(); languageDialog.ShowDialog(); conf.Language = languageDialog.SelectedLanguage; IniConfig.Save(); } // Check if it's the first time launch? if (conf.IsFirstLaunch) { conf.IsFirstLaunch = false; IniConfig.Save(); transport.AddCommand(CommandEnum.FirstLaunch); } } catch (Exception ex) { LOG.Error("Exception in startup.", ex); Application_ThreadException(MainForm.ActiveForm, new ThreadExceptionEventArgs(ex)); } }
/// <summary> /// Shutdown / cleanup /// </summary> public void Exit() { LOG.Info("Exit: " + EnvironmentInfo.EnvironmentToString(false)); // Close all open forms (except this), use a separate List to make sure we don't get a "InvalidOperationException: Collection was modified" List <Form> formsToClose = new List <Form>(); foreach (Form form in Application.OpenForms) { if (form.Handle != this.Handle && !form.GetType().Equals(typeof(Greenshot.ImageEditorForm))) { formsToClose.Add(form); } } foreach (Form form in formsToClose) { try { LOG.InfoFormat("Closing form: {0}", form.Name); this.Invoke((MethodInvoker) delegate { form.Close(); }); } catch (Exception e) { LOG.Error("Error closing form!", e); } } // Now the sound isn't needed anymore try { SoundHelper.Deinitialize(); } catch (Exception e) { LOG.Error("Error deinitializing sound!", e); } // Inform all registed plugins try { PluginHelper.instance.Shutdown(); } catch (Exception e) { LOG.Error("Error shutting down plugins!", e); } // Gracefull shutdown try { Application.DoEvents(); Application.Exit(); } catch (Exception e) { LOG.Error("Error closing application!", e); } ImageOutput.RemoveTmpFiles(); // Store any open configuration changes try { IniConfig.Save(); } catch (Exception e) { LOG.Error("Error storing configuration!", e); } // Remove the application mutex FreeMutex(); }
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { try { switch (keyData) { case Keys.Escape: DialogResult = DialogResult.Cancel; break; case Keys.E: MessageBox.Show(EnvironmentInfo.EnvironmentToString(true)); break; case Keys.L: // TODO: Open the log file /* * try * { * if (File.Exists(MainForm.LogFileLocation)) * { * using (Process.Start("\"" + MainForm.LogFileLocation + "\"")) * { * // nothing to do, just using dispose to cleanup * } * } * else * { * MessageBox.Show("Greenshot can't find the logfile, it should have been here: " + MainForm.LogFileLocation); * } * } * * catch (Exception) * { * MessageBox.Show("Couldn't open the greenshot.log, it's located here: " + MainForm.LogFileLocation, "Error opening greeenshot.log", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); * } */ break; case Keys.I: try { using (Process.Start("\"" + IniConfig.Current.IniLocation + "\"")) { // Ignore } } catch (Exception) { MessageBox.Show("Couldn't open the greenshot.ini, it's located here: " + IniConfig.Current.IniLocation, "Error opening greeenshot.ini", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } break; default: return(base.ProcessCmdKey(ref msg, keyData)); } } catch (Exception ex) { Log.Error().WriteLine(ex, $"Error handling key '{keyData}'"); } return(true); }
/// <summary> /// Shutdown / cleanup /// </summary> public void exit() { ClipboardHelper.DeregisterClipboardViewer(this.Handle); LOG.Info("Exit: " + EnvironmentInfo.EnvironmentToString(false)); // Close all open forms (except this), use a separate List to make sure we don't get a "InvalidOperationException: Collection was modified" List <Form> formsToClose = new List <Form>(); foreach (Form form in Application.OpenForms) { if (form.Handle != this.Handle && !form.GetType().Equals(typeof(Greenshot.ImageEditorForm))) { formsToClose.Add(form); } } foreach (Form form in formsToClose) { try { LOG.InfoFormat("Closing form: {0}", form.Name); this.Invoke((MethodInvoker) delegate { form.Close(); }); } catch (Exception e) { LOG.Error("Error closing form!", e); } } // Make sure hotkeys are disabled try { HotkeyControl.UnregisterHotkeys(); } catch (Exception e) { LOG.Error("Error unregistering hotkeys!", e); } // Now the sound isn't needed anymore try { SoundHelper.Deinitialize(); } catch (Exception e) { LOG.Error("Error deinitializing sound!", e); } // Inform all registed plugins try { PluginHelper.instance.Shutdown(); } catch (Exception e) { LOG.Error("Error shutting down plugins!", e); } // Gracefull shutdown try { Application.DoEvents(); Application.Exit(); } catch (Exception e) { LOG.Error("Error closing application!", e); } // Store any open configuration changes try { IniConfig.Save(); } catch (Exception e) { LOG.Error("Error storing configuration!", e); } // Remove the application mutex FreeMutex(); // make the icon invisible otherwise it stays even after exit!! if (notifyIcon != null) { notifyIcon.Visible = false; notifyIcon.Dispose(); notifyIcon = null; } }