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

@@ -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,7 +111,10 @@ 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);
@@ -128,6 +134,9 @@ public static class WinApi
[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);

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