C++ 将 DWORD 值写入某个注册表项的所有子项
C++ Writing a DWORD value to all subkeys of a certain registry key
我很难使我发现的这段代码对我有用。
现在:它在注册表中的接口文件夹(注册表项)中添加 2 个 DWORD 值。
希望:我希望它将这 2 个 DWORD 值添加到接口注册表项(文件夹)的所有子项(子文件夹)中。
我有这个伪代码:
- 使用 RegOpenKey 或 RegOpenKeyEx 打开父密钥
- 在循环中使用 RegEnumKey 或 RegEnumKeyEx 枚举父项的所有子键
- 对于每个子键,使用 RegSetValueEx 设置所需的值
- 使用RegCloseKey关闭父键
我会继续努力解决这个问题,但也许有人可以帮忙?
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <iostream>
using std::cout;
using std::endl;
HKEY OpenKey(HKEY hRootKey, wchar_t* strKey)
{
HKEY hKey;
LONG nError = RegOpenKeyEx(hRootKey, strKey, NULL, KEY_ALL_ACCESS, &hKey);
if(nError==ERROR_FILE_NOT_FOUND)
{
cout << "Creating registry key: " << strKey << endl;
nError = RegCreateKeyEx(hRootKey, strKey, NULL, NULL, REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL, &hKey, NULL);
}
if(nError)
{
cout << "Error: " << nError << " Could not find or create " << strKey << endl;
}
return hKey;
}
void SetVal(HKEY hKey, LPCTSTR lpValue, DWORD data)
{
LONG nError = RegSetValueEx(hKey, lpValue, NULL, REG_DWORD, (LPBYTE)&data, sizeof(DWORD));
if(nError)
{
cout << "Error: " << nError << " Could not set registry value: " << (char*)lpValue << endl;
}
}
DWORD GetVal(HKEY hKey, LPCTSTR lpValue)
{
DWORD data;
DWORD size = sizeof(data);
DWORD type = REG_DWORD;
LONG nError = RegQueryValueEx(hKey, lpValue, NULL, &type, (LPBYTE)&data, &size);
if(nError==ERROR_FILE_NOT_FOUND)
{
data = 0; // The value will be created and set to data next time SetVal() is called.
}
else if(nError)
{
cout << "Error: " << nError << " Could not get registry value " << (char*)lpValue << endl;
}
return data;
}
int main()
{
static DWORD v1, v2;
HKEY hKey = OpenKey(HKEY_LOCAL_MACHINE,L"SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces\");
v1 = GetVal(hKey, L"Registry Value1");
v2 = GetVal(hKey, L"Registry Value2");
v1 += 5;
v2 += 2;
SetVal(hKey, L"Registry Value1", v1);
SetVal(hKey, L"Registry Value2", v2);
RegCloseKey(hKey);
return 0;
}
这是一个没有任何额外内容的最低限度示例:
// open desired key whose subkeys shall be enumerated
HKEY hKey={0};
LPCTSTR path=TEXT("SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces");
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,path,0,KEY_ENUMERATE_SUB_KEYS,&hKey) != ERROR_SUCCESS)
return; // failed to open
DWORD index=0; // enumeration index
TCHAR keyName[256]={0}; // buffer to store enumerated subkey name
DWORD keyLen=256; // buffer length / number of TCHARs copied to keyName
// enumerate subkey names of hKey, result stored in keyName, keyLen set to strlen(keyName)
while(RegEnumKeyEx(hKey,index++,keyName,&keyLen,0,0,0,0) == ERROR_SUCCESS) {
keyLen=256; // reset buffer length (RegEnumKeyEx changes this value)
// open the subkey and set the desired value(s)
HKEY hSubKey={0};
if(RegOpenKeyEx(hKey,keyName,0,KEY_SET_VALUE,&hSubKey) == ERROR_SUCCESS) {
// set desired value(s):
DWORD myValue = 0xCAFEBABE;
//RegSetValueEx(hSubKey,TEXT("MyValueName"),0,REG_DWORD,(LPBYTE)&myValue,sizeof(DWORD));
RegCloseKey(hSubKey); // close sub key
}
// else: failed to open subkey
}
// RegEnumKeyEx either returns ERROR_SUCCESS, ERROR_NO_MORE_ITEMS, or a system error code
RegCloseKey(hKey); // close key
请注意,此示例不评估错误代码。它只是演示枚举子键和设置值的过程。RegOpenKeyEx访问权限设置为执行此任务所需的最低权限(将其设置为您希望使用打开的密钥执行的任何操作)。while 循环与ERROR_NO_MORE_ITEMS(一旦没有更多要枚举的子项)或实际错误没有区别。为了安全起见,RegSetValueEx被注释掉,其返回值被忽略。
相关文章:
- 有没有任务栏API可以立即应用注册表更改
- 检查注册表项是否链接到(或副本)另一个注册表项
- 禁用地址共享注册表不起作用
- 如何从注册表项中提取配置单元和注册表名称
- 注册表正在设置 1 个字符
- 清理"QSettings"注册表项的最佳方法(Windows上的Qt 5)
- 在 c++ 中编辑注册表项
- 某些进程的注册表限制
- 在编译时生成某种子类/类型注册表?
- 如何测试注册表项是否存在?
- 注册表-获取值(而不是键)上次更改的时间和键创建日期
- 从注册表中检索已注销用户的 sid
- 如何正确检查 c++ 是否存在注册表项?
- 如何在不使用任何数据库的情况下制作动态注册表单?
- 强制资源管理器重新加载注册表值
- 以编程方式锁定注册表项以避免并发问题
- 如何应用注册表模式使"select class depend on input"遵守开放封闭原则?
- 如何恢复注册表项的有效权限?C++
- 如何防止 C API 注册表中的 Lua 回调被垃圾回收?
- C++ 将 DWORD 值写入某个注册表项的所有子项