diff --git a/UXAssist/Common/WinApi.cs b/UXAssist/Common/WinApi.cs index 2fd181e..5cc4150 100644 --- a/UXAssist/Common/WinApi.cs +++ b/UXAssist/Common/WinApi.cs @@ -101,6 +101,9 @@ public static class WinApi public delegate IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam); + [DllImport("kernel32", ExactSpelling = true)] + public static extern int GetLastError(); + [DllImport("user32", CharSet = CharSet.Unicode)] public static extern int GetWindowLong(IntPtr hwnd, int nIndex); @@ -108,39 +111,45 @@ public static class WinApi public static extern int SetWindowLong(IntPtr hwnd, int nIndex, int dwNewLong); [DllImport("user32", CharSet = CharSet.Unicode)] - public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); - + public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, string lpWindowName); + + [DllImport("user32", ExactSpelling = true, SetLastError = true)] + public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int lpdwProcessId); + [DllImport("user32", ExactSpelling = true)] public static extern bool GetWindowRect(IntPtr hwnd, out Rect lpRect); - + [DllImport("user32", ExactSpelling = true)] public static extern bool SetWindowPos(IntPtr hwnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, int flags); - + [DllImport("user32", CharSet = CharSet.Unicode)] public static extern bool SetWindowText(IntPtr hwnd, string lpString); - + [DllImport("user32", ExactSpelling = true)] public static extern bool MoveWindow(IntPtr hWnd, int x, int y, int nWidth, int nHeight, bool bRepaint); [DllImport("kernel32", ExactSpelling = true, SetLastError = true)] public static extern bool GetProcessAffinityMask(IntPtr hProcess, out ulong lpProcessAffinityMask, out ulong lpSystemAffinityMask); - + [DllImport("kernel32", ExactSpelling = true, SetLastError = true)] public static extern IntPtr GetCurrentProcess(); + [DllImport("kernel32", ExactSpelling = true)] + public static extern int GetCurrentProcessId(); + [DllImport("kernel32", ExactSpelling = true, SetLastError = true)] public static extern bool SetProcessAffinityMask(IntPtr hProcess, ulong dwProcessAffinityMask); - + // GetPriorityClass and SetPriorityClass [DllImport("kernel32", ExactSpelling = true, SetLastError = true)] public static extern int GetPriorityClass(IntPtr hProcess); - + [DllImport("kernel32", ExactSpelling = true, SetLastError = true)] public static extern bool SetPriorityClass(IntPtr hProcess, int dwPriorityClass); - + [DllImport("user32", CharSet = CharSet.Unicode)] public static extern IntPtr SetWindowLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong); - + [DllImport("user32", CharSet = CharSet.Unicode)] public static extern IntPtr CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam); @@ -280,5 +289,5 @@ public static class WinApi } } - #endregion + #endregion } \ No newline at end of file diff --git a/UXAssist/Functions/WindowFunctions.cs b/UXAssist/Functions/WindowFunctions.cs index 53fb9f4..47da706 100644 --- a/UXAssist/Functions/WindowFunctions.cs +++ b/UXAssist/Functions/WindowFunctions.cs @@ -226,7 +226,7 @@ public static class WindowFunctions if (index < 0) break; arg = arg.Substring(index + profileSuffix.Length); - var wnd = WinApi.FindWindow(GameWindowClass, _gameWindowTitle); + var wnd = FindGameWindow(); if (wnd == IntPtr.Zero) return; ProfileName = arg; _gameWindowTitle = $"Dyson Sphere Program - {arg}"; @@ -237,8 +237,19 @@ public static class WindowFunctions public static IntPtr FindGameWindow() { - if (_gameWindowHandle == IntPtr.Zero) - _gameWindowHandle = WinApi.FindWindow(GameWindowClass, _gameWindowTitle); + if (_gameWindowHandle != IntPtr.Zero) + return _gameWindowHandle; + var wnd = IntPtr.Zero; + while (true) + { + wnd = WinApi.FindWindowEx(IntPtr.Zero, wnd, GameWindowClass, _gameWindowTitle); + if (wnd == IntPtr.Zero) + return IntPtr.Zero; + WinApi.GetWindowThreadProcessId(wnd, out var pid); + if (pid == WinApi.GetCurrentProcessId()) + break; + } + _gameWindowHandle = wnd; return _gameWindowHandle; } }