From 2eb89b549a913144827c23453f5719d062ba8e7b Mon Sep 17 00:00:00 2001 From: Ilpo Ruotsalainen Date: Sat, 3 Aug 2024 09:03:44 -0700 Subject: [PATCH] Fix sending URI to an already running app instance If there is already a running app instance, trying to initialize logging will fail since the logfile will already be open for writing. Reorder the program startup such that the check for already running instance is done before trying to initialize logging. This also moves the logging initialization after unhandled exception handler is added so that if similar bugs are introduced in future at least the user will get an error message. --- client-gui/MainWindow.cs | 4 +++- client-gui/ProgramMain.cs | 29 ++++++++++++++--------------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/client-gui/MainWindow.cs b/client-gui/MainWindow.cs index 11d52c2..1b57764 100644 --- a/client-gui/MainWindow.cs +++ b/client-gui/MainWindow.cs @@ -33,7 +33,9 @@ private void AddFromURI(string uri) { private void HandleCopyData(User32.COPYDATASTRUCT copydata) { switch (copydata.dwData) { case Constants.COPYDATA_SET_URI: - AddFromURI(Marshal.PtrToStringUTF8(copydata.lpData, (int)copydata.cbData)); + var uri = Marshal.PtrToStringUTF8(copydata.lpData, (int)copydata.cbData); + Logging.Log(Logging.Severity.DEBUG, $"Add URI request with payload {uri}"); + AddFromURI(uri); break; } diff --git a/client-gui/ProgramMain.cs b/client-gui/ProgramMain.cs index 2eb4a40..75896d9 100644 --- a/client-gui/ProgramMain.cs +++ b/client-gui/ProgramMain.cs @@ -52,12 +52,24 @@ private static void SendUriToWindow(IntPtr hWnd, string uri) { [STAThread] static void Main(string[] args) { - Logging.Init(Path.Join(GetMyFolder(), "logs")); - #if !DEBUG AppDomain.CurrentDomain.UnhandledException += UnhandledException; #endif + string? serverToAdd = null; + if (args.Length > 0) { + serverToAdd = args[0]; + } + + IntPtr? openWindowHandle = FindAlreadyOpenWindow(); + if (openWindowHandle != null && serverToAdd != null) { + Logging.Log(Logging.Severity.DEBUG, $"Sending URI {serverToAdd} to window {openWindowHandle}"); + SendUriToWindow(openWindowHandle.Value, serverToAdd); + return; + } + + Logging.Init(Path.Join(GetMyFolder(), "logs")); + var settings = Settings.Load(); if (settings == null) { var accPath = ACCHook.FindACCInstallDir(); @@ -72,21 +84,8 @@ Configure it manually via the settings dialog settings = new Settings { AccInstallPath = accPath }; } - ApplicationConfiguration.Initialize(); - string? serverToAdd = null; - if (args.Length > 0) { - serverToAdd = args[0]; - } - - IntPtr? openWindowHandle = FindAlreadyOpenWindow(); - if (openWindowHandle != null && serverToAdd != null) { - Logging.Log(Logging.Severity.INFO, $"Sending URI {serverToAdd} to window {openWindowHandle}"); - SendUriToWindow(openWindowHandle.Value, serverToAdd); - return; - } - var serverList = LoadServerList(); var serverDataLock = new object(); var serverData = Array.Empty();