diff --git a/src/Program.cs b/src/Program.cs index f78b9ef..d904658 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -1,16 +1,18 @@ using YaeAchievement; using static YaeAchievement.Utils; +InstallExitHook(); +TryDisableQuickEdit(); +InstallExceptionHook(); +CheckGenshinIsRunning(); + Console.WriteLine("----------------------------------------------------"); Console.WriteLine($"YaeAchievement - 原神成就导出工具 ({GlobalVars.AppVersionName})"); Console.WriteLine("https://github.com/HolographicHat/YaeAchievement"); Console.WriteLine("----------------------------------------------------"); -InstallExitHook(); -InstallExceptionHook(); -CheckGenshinIsRunning(); + LoadConfig(); CheckUpdate(); -TryDisableQuickEdit(); StartAndWaitResult(GlobalVars.GamePath, str => { GlobalVars.UnexpectedExit = false; var list = AchievementAllDataNotify.Parser.ParseFrom(Convert.FromBase64String(str)); diff --git a/src/Utils.cs b/src/Utils.cs index 6d21ef6..2347198 100644 --- a/src/Utils.cs +++ b/src/Utils.cs @@ -149,6 +149,7 @@ public static class Utils { private static string SelectGameExecutable() { var fnPtr = Marshal.AllocHGlobal(32768); + Native.RtlZeroMemory(fnPtr, 32768); var ofn = new OpenFileName { file = fnPtr, size = Marshal.SizeOf(), @@ -158,6 +159,19 @@ public static class Utils { filter = "国服/国际服主程序 (YuanShen/GenshinImpact.exe)\0YuanShen.exe;GenshinImpact.exe\0", maxFile = 32768 }; + new Thread(() => { + var handle = Native.FindWindow("#32770", "选择主程序"); + while (handle == IntPtr.Zero) { + handle = Native.FindWindow("#32770", "选择主程序"); + Thread.Sleep(1); + } + var currentThreadId = Native.GetCurrentThreadId(); + var foregroundThreadId = Native.GetWindowThreadProcessId(Native.GetForegroundWindow(), out _); + Native.AttachThreadInput(currentThreadId, foregroundThreadId, true); + Native.SetWindowPos(handle, new IntPtr(-1), 0, 0, 0, 0, 1 | 2); + Native.SetForegroundWindow(handle); + Native.AttachThreadInput(currentThreadId, foregroundThreadId, false); + }).Start(); if(!Native.GetOpenFileName(ofn)) { var err = Native.CommDlgExtendedError(); if (err != 0) { diff --git a/src/Win32/Native.cs b/src/Win32/Native.cs index 45525cc..336247e 100644 --- a/src/Win32/Native.cs +++ b/src/Win32/Native.cs @@ -113,4 +113,29 @@ public static class Native { [DllImport("user32.dll")] public static extern bool EmptyClipboard(); + + [DllImport("kernel32.dll")] + public static extern void RtlZeroMemory(IntPtr dst, ulong length); + + [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)] + public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); + + [DllImport("user32.dll", SetLastError = true)] + public static extern bool SetForegroundWindow(IntPtr hWnd); + + [DllImport("user32.dll", SetLastError = true)] + public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags); + + [DllImport("user32.dll", SetLastError = true)] + public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint pid); + + [DllImport("kernel32.dll")] + public static extern uint GetCurrentThreadId(); + + [DllImport("user32.dll", SetLastError = true)] + public static extern IntPtr GetForegroundWindow(); + + [DllImport("user32.dll", SetLastError = true)] + public static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach); + }