From 5932fdcea386b55bfa7e5cf21b59f10980cd808f Mon Sep 17 00:00:00 2001 From: admin Date: Sun, 18 Oct 2026 18:41:33 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=89=8D=E5=85=B3=E9=97=AD?= =?UTF-8?q?=E5=90=8C=E5=A5=97=E7=A8=8B=E5=BA=8F=E8=BF=9B=E7=A8=8B=EF=BC=8C?= =?UTF-8?q?=E9=81=BF=E5=85=8D=20EXE=20=E8=A2=AB=E5=8D=A0=E7=94=A8=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E8=A6=86=E7=9B=96=E5=A4=B1=E8=B4=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更新包同时包含 CrownServer.exe 和 CrownClient.exe,运行中的 EXE 无法被覆盖。CrownServer/CrownClient 拉起更新器前先结束对方进程,先投递 WM_CLOSE 给保存机会,超时后强制结束;Update 新增 Utils::CloseProcesses,手动运行更新器时同样关闭,进程名与超时见 Config.h。 --- CrownClient/main.cpp | 78 ++++++++++++++++++++++++++++++++++++++++ CrownServer/main.cpp | 78 ++++++++++++++++++++++++++++++++++++++++ Update/Config.h | 11 ++++++ Update/Utils.cpp | 84 ++++++++++++++++++++++++++++++++++++++++++++ Update/Utils.h | 4 +++ Update/Worker.cpp | 29 +++++++++++++++ 6 files changed, 284 insertions(+) diff --git a/CrownClient/main.cpp b/CrownClient/main.cpp index 6aa6697..aac0e8b 100644 --- a/CrownClient/main.cpp +++ b/CrownClient/main.cpp @@ -1,3 +1,8 @@ +#define NOMINMAX +#define WIN32_LEAN_AND_MEAN +#include +#include + #include "CrownClient.h" #include #include "Global.h" @@ -17,6 +22,75 @@ namespace { +// 更新前结束同套程序的其它进程。 +// Windows 下运行中的 EXE 无法被覆盖,而更新包同时包含 CrownServer.exe 和 CrownClient.exe, +// 必须让两者都先退出,否则更新时复制文件会失败 +void CloseSiblingApps() +{ + const DWORD selfId = GetCurrentProcessId(); + const wchar_t* names[] = { L"CrownServer.exe", L"CrownClient.exe" }; + + for (const wchar_t* name : names) { + QList pids; + HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); + if (snap != INVALID_HANDLE_VALUE) { + PROCESSENTRY32W pe; + pe.dwSize = sizeof(pe); + if (Process32FirstW(snap, &pe)) { + do { + if (lstrcmpiW(pe.szExeFile, name) == 0 && pe.th32ProcessID != selfId) { + pids.append(pe.th32ProcessID); + } + } while (Process32NextW(snap, &pe)); + } + CloseHandle(snap); + } + if (pids.isEmpty()) { + continue; + } + + // 先投递 WM_CLOSE,给对方保存数据的机会 + for (DWORD pid : pids) { + EnumWindows([](HWND hwnd, LPARAM lparam) -> BOOL { + DWORD owner = 0; + GetWindowThreadProcessId(hwnd, &owner); + if (owner == static_cast(lparam)) { + PostMessageW(hwnd, WM_CLOSE, 0, 0); + } + return TRUE; + }, static_cast(pid)); + } + + // 等待其自行退出,最多 5 秒 + for (int waited = 0; waited < 5000; waited += 100) { + bool alive = false; + for (DWORD pid : pids) { + HANDLE h = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid); + if (h) { + DWORD code = 0; + if (GetExitCodeProcess(h, &code) && code == STILL_ACTIVE) { + alive = true; + } + CloseHandle(h); + } + } + if (!alive) { + break; + } + Sleep(100); + } + + // 仍未退出的强制结束 + for (DWORD pid : pids) { + HANDLE h = OpenProcess(PROCESS_TERMINATE, FALSE, pid); + if (h) { + TerminateProcess(h, 0); + CloseHandle(h); + } + } + } +} + // 更新服务应用 AppKey,取自 verify 后台 -> 应用 -> AppKey const char* kUpdaterAppKey = "abab3280114bfe824acfdbc6255538fb"; @@ -62,6 +136,10 @@ bool CheckAndRunUpdater() } args << "--launch" << exeName; + // 更新包同时包含 CrownServer.exe 和 CrownClient.exe, + // 必须让另一个进程先退出,否则它的 EXE 被占用,覆盖会失败 + CloseSiblingApps(); + QProcess::startDetached(updater, args, appDir); return false; } diff --git a/CrownServer/main.cpp b/CrownServer/main.cpp index cce4b49..e539df9 100644 --- a/CrownServer/main.cpp +++ b/CrownServer/main.cpp @@ -1,5 +1,10 @@ +#define NOMINMAX +#define WIN32_LEAN_AND_MEAN +#include +#include + #include #include @@ -22,6 +27,75 @@ namespace { +// 更新前结束同套程序的其它进程。 +// Windows 下运行中的 EXE 无法被覆盖,而更新包同时包含 CrownServer.exe 和 CrownClient.exe, +// 必须让两者都先退出,否则更新时复制文件会失败 +void CloseSiblingApps() +{ + const DWORD selfId = GetCurrentProcessId(); + const wchar_t* names[] = { L"CrownServer.exe", L"CrownClient.exe" }; + + for (const wchar_t* name : names) { + QList pids; + HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); + if (snap != INVALID_HANDLE_VALUE) { + PROCESSENTRY32W pe; + pe.dwSize = sizeof(pe); + if (Process32FirstW(snap, &pe)) { + do { + if (lstrcmpiW(pe.szExeFile, name) == 0 && pe.th32ProcessID != selfId) { + pids.append(pe.th32ProcessID); + } + } while (Process32NextW(snap, &pe)); + } + CloseHandle(snap); + } + if (pids.isEmpty()) { + continue; + } + + // 先投递 WM_CLOSE,给对方保存数据的机会 + for (DWORD pid : pids) { + EnumWindows([](HWND hwnd, LPARAM lparam) -> BOOL { + DWORD owner = 0; + GetWindowThreadProcessId(hwnd, &owner); + if (owner == static_cast(lparam)) { + PostMessageW(hwnd, WM_CLOSE, 0, 0); + } + return TRUE; + }, static_cast(pid)); + } + + // 等待其自行退出,最多 5 秒 + for (int waited = 0; waited < 5000; waited += 100) { + bool alive = false; + for (DWORD pid : pids) { + HANDLE h = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid); + if (h) { + DWORD code = 0; + if (GetExitCodeProcess(h, &code) && code == STILL_ACTIVE) { + alive = true; + } + CloseHandle(h); + } + } + if (!alive) { + break; + } + Sleep(100); + } + + // 仍未退出的强制结束 + for (DWORD pid : pids) { + HANDLE h = OpenProcess(PROCESS_TERMINATE, FALSE, pid); + if (h) { + TerminateProcess(h, 0); + CloseHandle(h); + } + } + } +} + // 更新服务应用 AppKey,取自 verify 后台 -> 应用 -> AppKey const char* kUpdaterAppKey = "abab3280114bfe824acfdbc6255538fb"; @@ -67,6 +141,10 @@ bool CheckAndRunUpdater() } args << "--launch" << exeName; + // 更新包同时包含 CrownServer.exe 和 CrownClient.exe, + // 必须让另一个进程先退出,否则它的 EXE 被占用,覆盖会失败 + CloseSiblingApps(); + QProcess::startDetached(updater, args, appDir); return false; } diff --git a/Update/Config.h b/Update/Config.h index 0d8115b..0fc8ba9 100644 --- a/Update/Config.h +++ b/Update/Config.h @@ -65,6 +65,17 @@ #define UPDATER_WAIT_EXIT_SEC 10 #endif +// 更新前需要主动关闭的进程名,逗号或分号分隔(留空表示不主动关闭)。 +// 更新包同时包含多个 EXE,正在运行的无法被覆盖,手动运行更新器时尤其必要 +#ifndef UPDATER_CLOSE_PROCESSES +#define UPDATER_CLOSE_PROCESSES L"CrownServer.exe,CrownClient.exe" +#endif + +// 关闭上述进程时等待其自行退出的秒数,超时后强制结束 +#ifndef UPDATER_CLOSE_TIMEOUT_SEC +#define UPDATER_CLOSE_TIMEOUT_SEC 5 +#endif + // 网络超时(秒) #define UPDATER_TIMEOUT_SEC 30 diff --git a/Update/Utils.cpp b/Update/Utils.cpp index 77c79e2..60119ea 100644 --- a/Update/Utils.cpp +++ b/Update/Utils.cpp @@ -323,6 +323,90 @@ bool Utils::WaitForProcessExit(const std::wstring& processName, int timeoutSec) return false; } +namespace { + +// 向属于指定 PID 的窗口投递 WM_CLOSE,让程序有机会保存数据后自行退出 +BOOL CALLBACK PostCloseToProcessWindows(HWND hwnd, LPARAM lparam) { + DWORD pid = 0; + GetWindowThreadProcessId(hwnd, &pid); + if (pid != 0 && pid == static_cast(lparam)) { + PostMessageW(hwnd, WM_CLOSE, 0, 0); + } + return TRUE; +} + +std::vector FindProcessIds(const std::wstring& processName) { + std::vector pids; + HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); + if (snap == INVALID_HANDLE_VALUE) return pids; + PROCESSENTRY32W pe = {0}; + pe.dwSize = sizeof(pe); + if (Process32FirstW(snap, &pe)) { + do { + if (_wcsicmp(pe.szExeFile, processName.c_str()) == 0) { + pids.push_back(pe.th32ProcessID); + } + } while (Process32NextW(snap, &pe)); + } + CloseHandle(snap); + return pids; +} + +bool IsProcessAlive(DWORD pid) { + HANDLE h = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid); + if (!h) return false; + DWORD code = 0; + bool alive = (GetExitCodeProcess(h, &code) != FALSE && code == STILL_ACTIVE); + CloseHandle(h); + return alive; +} + +} // namespace + +bool Utils::CloseProcesses(const std::wstring& processName, int timeoutSec) { + std::vector pids = FindProcessIds(processName); + if (pids.empty()) return true; + + // 先请它们自己退出,给保存数据的机会 + for (size_t i = 0; i < pids.size(); ++i) { + EnumWindows(PostCloseToProcessWindows, static_cast(pids[i])); + } + + DWORD ticks = GetTickCount(); + DWORD limit = static_cast(timeoutSec) * 1000; + while (GetTickCount() - ticks < limit) { + bool alive = false; + for (size_t i = 0; i < pids.size(); ++i) { + if (IsProcessAlive(pids[i])) { alive = true; break; } + } + if (!alive) return true; + Sleep(100); + } + + // 超时仍未退出的强制结束:运行中的 EXE 无法被覆盖, + // 不结束掉的话整个更新会在复制文件时失败 + for (size_t i = 0; i < pids.size(); ++i) { + if (!IsProcessAlive(pids[i])) continue; + HANDLE h = OpenProcess(PROCESS_TERMINATE, FALSE, pids[i]); + if (h) { + TerminateProcess(h, 0); + CloseHandle(h); + } + } + + // 强制结束后再确认一次 + ticks = GetTickCount(); + while (GetTickCount() - ticks < 3000) { + bool alive = false; + for (size_t i = 0; i < pids.size(); ++i) { + if (IsProcessAlive(pids[i])) { alive = true; break; } + } + if (!alive) return true; + Sleep(100); + } + return false; +} + bool Utils::ReplaceInUse(const std::wstring& srcTmp, const std::wstring& target) { if (MoveFileExW(srcTmp.c_str(), target.c_str(), MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH) != FALSE) { diff --git a/Update/Utils.h b/Update/Utils.h index 4eb1dd6..d54c9e2 100644 --- a/Update/Utils.h +++ b/Update/Utils.h @@ -43,6 +43,10 @@ public: const std::wstring& args, const std::wstring& workDir); static bool WaitForProcessExit(const std::wstring& processName, int timeoutSec); + + // 关闭指定名称的进程:先投递 WM_CLOSE 请求其自行退出,超时后强制结束。 + // 运行中的 EXE 无法被覆盖,更新前必须让它们先退出 + static bool CloseProcesses(const std::wstring& processName, int timeoutSec); static bool ReplaceInUse(const std::wstring& srcTmp, const std::wstring& target); }; diff --git a/Update/Worker.cpp b/Update/Worker.cpp index e8f5622..81d2496 100644 --- a/Update/Worker.cpp +++ b/Update/Worker.cpp @@ -42,6 +42,24 @@ void CollectFiles(const std::wstring& dir, const std::wstring& relBase, FindClose(handle); } +std::vector SplitProcessNames(const std::wstring& text) { + std::vector names; + std::wstring current; + for (size_t i = 0; i < text.size(); ++i) { + wchar_t c = text[i]; + if (c == L',' || c == L';') { + std::wstring name = Utils::Trim(current); + if (!name.empty()) names.push_back(name); + current.clear(); + } else { + current += c; + } + } + std::wstring name = Utils::Trim(current); + if (!name.empty()) names.push_back(name); + return names; +} + bool SameHash(const std::wstring& expected, const std::string& actual) { std::string want = Utils::WideToUtf8(expected); if (want.empty()) return true; @@ -185,6 +203,17 @@ void Worker::Run() { return; } + // 更新包同时包含多个 EXE,正在运行的无法被覆盖。 + // 手动运行更新器时这些进程可能还在跑,必须先让它们退出 + std::vector closeNames = SplitProcessNames(UPDATER_CLOSE_PROCESSES); + for (size_t i = 0; i < closeNames.size(); ++i) { + if (cancel_.load()) { Finish(false, L"已取消"); return; } + Emit(8, L"正在关闭 " + closeNames[i] + L"..."); + if (!Utils::CloseProcesses(closeNames[i], UPDATER_CLOSE_TIMEOUT_SEC)) { + Emit(8, L"无法关闭 " + closeNames[i] + L",更新可能无法完成"); + } + } + // 选择要下载的包:全量模式取完整包,补丁模式优先取补丁包 std::wstring url; unsigned long long size = 0;