创建注册表键 - RegistryCreateKey
函数简介
创建并打开注册表键,如果键已存在则直接打开。用于确保键存在的场景。
接口名称
RegistryCreateKey
DLL调用
int64_t RegistryCreateKey(int64_t instance, int32_t rootKey, const char* subKey)
参数定义:
instance(长整型数): OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。rootKey(整型数): 根键类型,可选值如下:- 0: HKEY_CLASSES_ROOT
- 1: HKEY_CURRENT_USER
- 2: HKEY_LOCAL_MACHINE
- 3: HKEY_USERS
- 4: HKEY_CURRENT_CONFIG
subKey(字符串): 子键路径,例如 "Software\OLAPlug"
示例:
// 创建OLA对象
int64_t instance = CreateCOLAPlugInterFace();
// 创建或打开注册表键
int64_t key = RegistryCreateKey(instance, 1, "Software\\OLAPlug\\Config");
if (key != 0) {
printf("成功创建/打开注册表键\n");
// 写入字符串值
RegistrySetString(instance, key, "AppName", "OLAPlug");
// 写入整型值
RegistrySetDword(instance, key, "Version", 100);
// 关闭注册表键
RegistryCloseKey(instance, key);
} else {
printf("创建/打开注册表键失败\n");
}
// 释放资源
DestroyCOLAPlugInterFace(instance);
返回值
长整型数:
- 成功: 返回注册表键句柄(非0值)
- 失败: 返回 0
注意事项
- 如果键已存在,则直接打开已有键,不会覆盖现有数据
- 使用完成后必须调用 RegistryCloseKey 释放句柄
- 创建系统级键(如 HKEY_LOCAL_MACHINE 下)可能需要管理员权限
- 如果仅需打开已存在的键,建议使用 RegistryOpenKey
