我无法编写我为我找到工作的代码。
现在:它在注册表中的 Interface 文件夹(注册表项)中添加了 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;
}