Skip to content

Commit

Permalink
Fixed issue preventing window title being set properly, probably beca…
Browse files Browse the repository at this point in the history
…use of different thread
  • Loading branch information
Jon committed Nov 5, 2024
1 parent a4b8afd commit 1e18939
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions GW Launcher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);

Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 1e18939

Please sign in to comment.