1
0
mirror of https://github.com/soarqin/DSP_Mods.git synced 2025-12-08 21:33:28 +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,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
}

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