mirror of
https://github.com/LHY0125/PathEditor.git
synced 2026-05-10 02:09:46 +08:00
a769a6b9b3
- 将原有扁平目录结构重构为 MVC 分层架构: * src/core/: 核心业务逻辑(Model),完全独立于 UI 框架 * src/ui/: 界面组件构建(View),负责纯视觉展示 * src/controller/: 用户交互处理(Controller),连接数据与界面 * src/utils/: 底层工具函数,专注于系统调用和字符串处理 - 引入 AppContext 结构体统一管理应用状态,替代原有的全局变量模式 - 重命名并重新组织头文件,按功能模块划分到对应子目录 - 更新 CMakeLists.txt 以适配新的目录结构 - 同步更新 README.md 文档,说明新的架构设计
144 lines
3.8 KiB
C
144 lines
3.8 KiB
C
#include "core/registry_service.h"
|
|
#include "utils/string_ext.h"
|
|
#include <windows.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
#define REG_PATH_SYS L"SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment"
|
|
#define REG_PATH_USER L"Environment"
|
|
#define REG_VALUE L"Path"
|
|
|
|
// 内部辅助函数:加载单个列表
|
|
static int load_single_path(HKEY hKeyRoot, const wchar_t *regPath, StringList *list)
|
|
{
|
|
clear_string_list(list);
|
|
|
|
HKEY hKey;
|
|
LONG res = RegOpenKeyExW(hKeyRoot, regPath, 0, KEY_READ, &hKey);
|
|
if (res != ERROR_SUCCESS)
|
|
{
|
|
return 0; // 失败
|
|
}
|
|
|
|
DWORD type, size;
|
|
res = RegQueryValueExW(hKey, REG_VALUE, NULL, &type, NULL, &size);
|
|
if (res == ERROR_SUCCESS)
|
|
{
|
|
wchar_t *buffer = (wchar_t *)malloc(size + 2);
|
|
if (buffer)
|
|
{
|
|
memset(buffer, 0, size + 2);
|
|
if (RegQueryValueExW(hKey, REG_VALUE, NULL, &type, (LPBYTE)buffer, &size) == ERROR_SUCCESS)
|
|
{
|
|
wchar_t *current = buffer;
|
|
wchar_t *next_semicolon = NULL;
|
|
|
|
while (*current)
|
|
{
|
|
next_semicolon = wcschr(current, L';');
|
|
if (next_semicolon)
|
|
*next_semicolon = L'\0';
|
|
|
|
if (wcslen(current) > 0)
|
|
{
|
|
char *utf8_str = wide_to_utf8(current);
|
|
if (utf8_str)
|
|
{
|
|
add_string_list(list, utf8_str);
|
|
free(utf8_str);
|
|
}
|
|
}
|
|
|
|
if (next_semicolon)
|
|
current = next_semicolon + 1;
|
|
else
|
|
break;
|
|
}
|
|
}
|
|
free(buffer);
|
|
}
|
|
}
|
|
RegCloseKey(hKey);
|
|
return 1; // 成功
|
|
}
|
|
|
|
// 加载系统环境变量路径
|
|
int load_system_paths(StringList *list)
|
|
{
|
|
return load_single_path(HKEY_LOCAL_MACHINE, REG_PATH_SYS, list);
|
|
}
|
|
|
|
// 加载用户环境变量路径
|
|
int 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)
|
|
{
|
|
if (!list) return 0;
|
|
|
|
// 计算大小
|
|
size_t total_len = 0;
|
|
for (int i = 0; i < list->count; i++)
|
|
{
|
|
if (list->items[i])
|
|
{
|
|
wchar_t *witem = utf8_to_wide(list->items[i]);
|
|
if (witem)
|
|
{
|
|
total_len += wcslen(witem) + 1;
|
|
free(witem);
|
|
}
|
|
}
|
|
}
|
|
total_len += 1;
|
|
|
|
wchar_t *buffer = (wchar_t *)malloc(total_len * sizeof(wchar_t));
|
|
if (!buffer)
|
|
return 0;
|
|
|
|
buffer[0] = L'\0';
|
|
for (int i = 0; i < list->count; i++)
|
|
{
|
|
if (list->items[i])
|
|
{
|
|
wchar_t *witem = utf8_to_wide(list->items[i]);
|
|
if (witem)
|
|
{
|
|
wcscat(buffer, witem);
|
|
if (i < list->count - 1)
|
|
wcscat(buffer, L";");
|
|
free(witem);
|
|
}
|
|
}
|
|
}
|
|
|
|
HKEY hKey;
|
|
int success = 0;
|
|
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;
|
|
}
|
|
RegCloseKey(hKey);
|
|
}
|
|
|
|
free(buffer);
|
|
return success;
|
|
}
|
|
|
|
// 保存系统环境变量路径
|
|
int save_system_paths(const StringList *list)
|
|
{
|
|
return save_single_path(HKEY_LOCAL_MACHINE, REG_PATH_SYS, list);
|
|
}
|
|
|
|
// 保存用户环境变量路径
|
|
int save_user_paths(const StringList *list)
|
|
{
|
|
return save_single_path(HKEY_CURRENT_USER, REG_PATH_USER, list);
|
|
} |