From 6ba7e702f255fef5874d70a0fc248871b8c3c74b Mon Sep 17 00:00:00 2001 From: LHY0125 <3364451258@qq.com> Date: Thu, 26 Mar 2026 13:02:14 +0800 Subject: [PATCH] =?UTF-8?q?refactor(registry):=20=E4=BD=BF=E7=94=A8ErrorCo?= =?UTF-8?q?de=E7=B1=BB=E5=9E=8B=E6=9B=BF=E6=8D=A2int=E4=BD=9C=E4=B8=BA?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E8=BF=94=E5=9B=9E=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将registry_service.h/c中的函数返回值从int改为ErrorCode枚举 - 更新callbacks.c中的错误检查逻辑,使用ERR_OK常量进行比较 - 在内部辅助函数中返回具体的错误码(ERR_REGISTRY_FAILED等) - 提高代码类型安全性和错误处理可读性 --- include/core/registry_service.h | 9 +++++---- src/controller/callbacks.c | 13 +++++++------ src/core/registry_service.c | 28 +++++++++++++++------------- 3 files changed, 27 insertions(+), 23 deletions(-) diff --git a/include/core/registry_service.h b/include/core/registry_service.h index 108c2d8..c2c0f7b 100644 --- a/include/core/registry_service.h +++ b/include/core/registry_service.h @@ -2,13 +2,14 @@ #define REGISTRY_SERVICE_H #include "utils/string_ext.h" +#include "utils/error_code.h" // 加载系统变量和用户变量到字符串列表 -int load_system_paths(StringList *list); -int load_user_paths(StringList *list); +ErrorCode load_system_paths(StringList *list); +ErrorCode load_user_paths(StringList *list); // 从字符串列表保存系统变量和用户变量 -int save_system_paths(const StringList *list); -int save_user_paths(const StringList *list); +ErrorCode save_system_paths(const StringList *list); +ErrorCode save_user_paths(const StringList *list); #endif // REGISTRY_SERVICE_H diff --git a/src/controller/callbacks.c b/src/controller/callbacks.c index 7943e5e..2584bae 100644 --- a/src/controller/callbacks.c +++ b/src/controller/callbacks.c @@ -6,6 +6,7 @@ #include "core/import_export.h" #include "utils/string_ext.h" #include "utils/os_env.h" +#include "utils/error_code.h" #include "ui/ui_utils.h" #include "ui/dialogs.h" #include @@ -323,23 +324,23 @@ int btn_ok_cb(Ihandle *self) backup_registry(); - int sys_ok = save_system_paths(&ctx->sys_paths); - int user_ok = save_user_paths(&ctx->user_paths); + ErrorCode sys_ok = save_system_paths(&ctx->sys_paths); + ErrorCode user_ok = save_user_paths(&ctx->user_paths); Ihandle *lbl_status = IupGetDialogChild(dlg, "LBL_STATUS"); - if (sys_ok && user_ok) + if (sys_ok == ERR_OK && user_ok == ERR_OK) { SendMessageTimeoutW(HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)L"Environment", SMTO_ABORTIFHUNG, 5000, NULL); IupMessage("成功", "系统和用户 PATH 环境变量均已更新!"); if (lbl_status) IupSetAttribute(lbl_status, "TITLE", lua_config_get_string("status", "saved")); } - else if (sys_ok) + else if (sys_ok == ERR_OK) { IupMessage("提示", "系统变量保存成功,但用户变量保存失败。"); } - else if (user_ok) + else if (user_ok == ERR_OK) { IupMessage("提示", "用户变量保存成功,但系统变量保存失败。"); } @@ -526,7 +527,7 @@ void load_all_paths(void) if (!ctx) return; - if (!load_system_paths(&ctx->sys_paths)) + if (load_system_paths(&ctx->sys_paths) != ERR_OK) { IupMessage("错误", "无法打开系统环境变量注册表键,请尝试以管理员身份运行。"); } diff --git a/src/core/registry_service.c b/src/core/registry_service.c index f498552..96f7b5a 100644 --- a/src/core/registry_service.c +++ b/src/core/registry_service.c @@ -1,5 +1,6 @@ #include "core/registry_service.h" #include "utils/string_ext.h" +#include "utils/error_code.h" #include #include #include @@ -9,7 +10,7 @@ #define REG_VALUE L"Path" // 内部辅助函数:加载单个列表 -static int load_single_path(HKEY hKeyRoot, const wchar_t *regPath, StringList *list) +static ErrorCode load_single_path(HKEY hKeyRoot, const wchar_t *regPath, StringList *list) { clear_string_list(list); @@ -17,7 +18,7 @@ static int load_single_path(HKEY hKeyRoot, const wchar_t *regPath, StringList *l LONG res = RegOpenKeyExW(hKeyRoot, regPath, 0, KEY_READ, &hKey); if (res != ERROR_SUCCESS) { - return 0; // 失败 + return ERR_REGISTRY_FAILED; } DWORD type, size; @@ -59,25 +60,26 @@ static int load_single_path(HKEY hKeyRoot, const wchar_t *regPath, StringList *l } } RegCloseKey(hKey); - return 1; // 成功 + return ERR_OK; } // 加载系统环境变量路径 -int load_system_paths(StringList *list) +ErrorCode load_system_paths(StringList *list) { return load_single_path(HKEY_LOCAL_MACHINE, REG_PATH_SYS, list); } // 加载用户环境变量路径 -int load_user_paths(StringList *list) +ErrorCode load_user_paths(StringList *list) { return load_single_path(HKEY_CURRENT_USER, REG_PATH_USER, list); } // 内部辅助函数:保存单个列表 -static int save_single_path(HKEY hKeyRoot, const wchar_t *regPath, const StringList *list) +static ErrorCode save_single_path(HKEY hKeyRoot, const wchar_t *regPath, const StringList *list) { - if (!list) return 0; + if (!list) + return ERR_NULL_PTR; // 计算大小 size_t total_len = 0; @@ -97,7 +99,7 @@ static int save_single_path(HKEY hKeyRoot, const wchar_t *regPath, const StringL wchar_t *buffer = (wchar_t *)malloc(total_len * sizeof(wchar_t)); if (!buffer) - return 0; + return ERR_OUT_OF_MEMORY; buffer[0] = L'\0'; for (int i = 0; i < list->count; i++) @@ -116,29 +118,29 @@ static int save_single_path(HKEY hKeyRoot, const wchar_t *regPath, const StringL } HKEY hKey; - int success = 0; + ErrorCode result = ERR_PERMISSION_DENIED; if (RegOpenKeyExW(hKeyRoot, regPath, 0, KEY_WRITE, &hKey) == ERROR_SUCCESS) { DWORD size = (DWORD)((wcslen(buffer) + 1) * sizeof(wchar_t)); if (RegSetValueExW(hKey, REG_VALUE, 0, REG_EXPAND_SZ, (LPBYTE)buffer, size) == ERROR_SUCCESS) { - success = 1; + result = ERR_OK; } RegCloseKey(hKey); } free(buffer); - return success; + return result; } // 保存系统环境变量路径 -int save_system_paths(const StringList *list) +ErrorCode save_system_paths(const StringList *list) { return save_single_path(HKEY_LOCAL_MACHINE, REG_PATH_SYS, list); } // 保存用户环境变量路径 -int save_user_paths(const StringList *list) +ErrorCode save_user_paths(const StringList *list) { return save_single_path(HKEY_CURRENT_USER, REG_PATH_USER, list); } \ No newline at end of file