1
0
mirror of https://github.com/soarqin/DSP_Mods.git synced 2025-12-09 02:53:29 +08:00

try to fix multi-instance bug

This commit is contained in:
2024-11-12 02:45:56 +08:00
parent 54f04eb5d7
commit 6048db8069
2 changed files with 34 additions and 14 deletions

View File

@@ -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;
}
}