mirror of
https://github.com/LHY0125/PathEditor.git
synced 2026-05-10 10:19:47 +08:00
refactor(registry): 使用ErrorCode类型替换int作为错误返回值
- 将registry_service.h/c中的函数返回值从int改为ErrorCode枚举 - 更新callbacks.c中的错误检查逻辑,使用ERR_OK常量进行比较 - 在内部辅助函数中返回具体的错误码(ERR_REGISTRY_FAILED等) - 提高代码类型安全性和错误处理可读性
This commit is contained in:
+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