更新前关闭同套程序进程,避免 EXE 被占用导致覆盖失败

更新包同时包含 CrownServer.exe 和 CrownClient.exe,运行中的 EXE 无法被覆盖。CrownServer/CrownClient 拉起更新器前先结束对方进程,先投递 WM_CLOSE 给保存机会,超时后强制结束;Update 新增 Utils::CloseProcesses,手动运行更新器时同样关闭,进程名与超时见 Config.h。
This commit is contained in:
2026-10-18 18:41:33 +08:00
parent dee5c15faf
commit 5932fdcea3
6 changed files with 284 additions and 0 deletions
+29
View File
@@ -42,6 +42,24 @@ void CollectFiles(const std::wstring& dir, const std::wstring& relBase,
FindClose(handle);
}
std::vector<std::wstring> SplitProcessNames(const std::wstring& text) {
std::vector<std::wstring> 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<std::wstring> 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;