From e58149e5f0e4c3f1c09c0e4b4d5d41e77ecb690c Mon Sep 17 00:00:00 2001 From: Latif Khalifa Date: Tue, 10 Dec 2013 11:49:16 +0100 Subject: [PATCH] Improve error message when running on systems with required libraries missing --- Programs/GridProxyGUI/Program.cs | 63 ++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 4 deletions(-) diff --git a/Programs/GridProxyGUI/Program.cs b/Programs/GridProxyGUI/Program.cs index 54a3b100..fa143bb7 100644 --- a/Programs/GridProxyGUI/Program.cs +++ b/Programs/GridProxyGUI/Program.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics; using Gtk; namespace GridProxyGUI @@ -7,10 +8,64 @@ namespace GridProxyGUI { public static void Main(string[] args) { - Application.Init(); - MainWindow win = new MainWindow(); - win.Show(); - Application.Run(); + try + { + StartGtkApp(); + } + catch (Exception ex) + { + if (ex is TypeInitializationException || ex is TypeLoadException || ex is System.IO.FileNotFoundException) + { + NativeApi.ExitWithMessage("Failed to start", ex.Message + "\n\nMake sure tha application install isn't missing accompanied files and that Gtk# is installed.", 1); + } + throw; + } + } + + static void StartGtkApp() + { + Gtk.Application.Init(); + MainWindow win = new MainWindow(); + win.Show(); + Application.Run(); + } + } + + public static class NativeApi + { + [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)] + public static extern int MessageBox(IntPtr hWnd, String text, String caption, int options); + + public static void LinuxMessageBox(string title, string msg, string type) + { + try + { + ProcessStartInfo p = new ProcessStartInfo("zenity", string.Format("--{0} --title=\"{1}\" --text=\"{2}\"", type, title.Replace("\"", "\\\""), msg.Replace("\"", "\\\""))); + p.CreateNoWindow = true; + p.ErrorDialog = false; + p.UseShellExecute = true; + var process = Process.Start(p); + process.WaitForExit(); + } + catch { } + } + + public static void ExitWithMessage(string title, string msg, int exitCode) + { + Console.Error.WriteLine(title + ": " + msg); + if (PlatformDetection.IsWindows) + { + MessageBox(IntPtr.Zero, msg, title, 0x10); + } + else if (PlatformDetection.IsMac) + { + } + else + { + LinuxMessageBox(title, msg + @" foo ""bar"" baz", "error"); + } + + Environment.Exit(exitCode); } } }