34-读写注册表

读写注册表

vs以管理员权限打开

打开注册表:Win+ R 组合键 :regedit

1.写注册表

HKEY hKey;
DWORD dwAge = 39;
//创建注册表项 VS2019自带的调试器管理员权限运行 自己的生成是以用户的权限运行
int ret = ::RegCreateKeyW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Rock\\admin", &hKey);
if (ret != ERROR_SUCCESS)
{
TRACE("##RegCreateKeyW Failed ,ErrorCode = %d,ret = %d",GetLastError(),
ret);
MessageBox(L"创建注册表失败");
return;
}
//写注册表
ret = ::RegSetValueEx(hKey, L"age", 0, REG_DWORD, (CONST BYTE*) & dwAge, 4);
if (ret != ERROR_SUCCESS)
{
TRACE("##RegSetValueEx Failed ,ErrorCode = %d,ret = %d",
GetLastError(),ret);
MessageBox(L"写注册表失败");
return;
}
::RegCloseKey(hKey);

2.读注册表

HKEY hKey;
DWORD dwAge;
//创建注册表项 VS2019自带的调试器管理员权限运行 自己的生成是以用户的权限运行
int ret = ::RegOpenKeyW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Rock\\admin", &hKey);
if (ret != ERROR_SUCCESS)
{
TRACE("##RegOpenKeyW Failed ,ErrorCode = %d,ret = %d", GetLastError(), ret);
MessageBox(L"打开注册表失败");
return;
}
//写注册表
DWORD dwType;
DWORD dwValue;
ret = ::RegQueryValueEx(hKey, L"age", 0, &dwType, (LPBYTE) & dwAge, &dwValue);
if (ret != ERROR_SUCCESS)
{
TRACE("##RegQueryValueEx Failed ,ErrorCode = %d,ret = %d", GetLastError(),
ret);
MessageBox(L"读注册表失败");
return;
}
TRACE("###dwType = %d,dwValue = %d ,dwAge = %d", dwType, dwValue, dwAge);
::RegCloseKey(hKey);