From 1e18939ccd1077bb4f7157db1e5a5377a1907aec Mon Sep 17 00:00:00 2001 From: Jon <> Date: Tue, 5 Nov 2024 13:05:25 +0000 Subject: [PATCH] Fixed issue preventing window title being set properly, probably because of different thread --- GW Launcher/Program.cs | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/GW Launcher/Program.cs b/GW Launcher/Program.cs index d18a276..d9c0aaa 100644 --- a/GW Launcher/Program.cs +++ b/GW Launcher/Program.cs @@ -5,8 +5,11 @@ using GW_Launcher.Forms; using GW_Launcher.Guildwars; using Octokit; +using PeNet.Header.Net.MetaDataTables; using Account = GW_Launcher.Classes.Account; using Application = System.Windows.Forms.Application; +using Assembly = System.Reflection.Assembly; +using File = System.IO.File; using FileMode = System.IO.FileMode; using ThreadState = System.Threading.ThreadState; @@ -29,10 +32,11 @@ internal static class Program private static string command_arg_launch_account_name = ""; - [DllImport("user32.dll", EntryPoint = "SetWindowText", CharSet = CharSet.Unicode)] - private static extern bool SetWindowText(IntPtr hwnd, string lpString); + [DllImport("user32.dll")] + public static extern int SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam); - [DllImport("user32.dll")] + + [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool SetForegroundWindow(IntPtr hWnd); @@ -157,11 +161,29 @@ static string GetAccountName(int account_index) memory.process.Kill(); return "Failed to wait for CharnamePtr after " + (timeout / 1000) + " seconds."; } - - if (memory.process.MainWindowTitle == "Guild Wars") - { - SetWindowText(memory.process.MainWindowHandle, account.Name); - } + timeout = 5000; + ok = WaitFor(() => + { + memory.process.Refresh(); + return memory.process.MainWindowTitle != ""; + }, timeout); + if (ok && memory.process.MainWindowTitle == "Guild Wars") + { + // NB: Window may not be ready for title change, or GW may be (re)setting window title as part of render process. + ok = WaitFor(() => + { + memory.process.Refresh(); + var chars = Marshal.StringToHGlobalAnsi(account.Name); + SendMessage(memory.process.MainWindowHandle, 0xc, 0, chars); + memory.process.Refresh(); + return memory.process.MainWindowTitle != "Guild Wars"; + }, timeout); + if (!ok) + { + memory.process.Kill(); + return "Failed to set window name after " + (timeout / 1000) + " seconds."; + } + } return null; }