mirror of
https://github.com/LHY0125/PathEditor.git
synced 2026-05-10 02:09:46 +08:00
refactor(registry): 使用ErrorCode类型替换int作为错误返回值
- 将registry_service.h/c中的函数返回值从int改为ErrorCode枚举 - 更新callbacks.c中的错误检查逻辑,使用ERR_OK常量进行比较 - 在内部辅助函数中返回具体的错误码(ERR_REGISTRY_FAILED等) - 提高代码类型安全性和错误处理可读性
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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 <string.h>
|
||||
@@ -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("错误", "无法打开系统环境变量注册表键,请尝试以管理员身份运行。");
|
||||
}
|
||||
|
||||
+15
-13
@@ -1,5 +1,6 @@
|
||||
#include "core/registry_service.h"
|
||||
#include "utils/string_ext.h"
|
||||
#include "utils/error_code.h"
|
||||
#include <windows.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -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);
|
||||
}
|
||||
Reference in New Issue
Block a user