Skip to content

Commit

Permalink
Fix sending URI to an already running app instance
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lonemeow committed Aug 3, 2024
1 parent 426d63a commit 2eb89b5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
4 changes: 3 additions & 1 deletion client-gui/MainWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
29 changes: 14 additions & 15 deletions client-gui/ProgramMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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<byte>();
Expand Down

0 comments on commit 2eb89b5

Please sign in to comment.