|
//写注册表
void CMFCFileView::OnWriteReg()
{
HKEY hKey;
DWORD dwAge = 20;
//创建注册表项
int ret = ::RegCreateKeyW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Doraemon\\admin", &hKey);//创建失败,以管理员方式运行。
if (ret)
{
TRACE("RegCreateKeyW Failed,ErrorCode = %d", GetLastError());
MessageBox(L"创建注册表失败");
return;
}
//写注册表
ret = ::RegSetValueEx(hKey, L"age", 0, REG_DWORD, (CONST BYTE*) & dwAge, 4);
if (ret != ERROR_SUCCESS)
{
TRACE("RegCreateKeyW Failed,ErrorCode = %d", GetLastError());
MessageBox(L"写注册表失败");
return;
}
::RegCloseKey(hKey);
}
//读注册表
void CMFCFileView::OnReadReg()
{
HKEY hKey;
DWORD dwAge;
//打开注册表项
int ret = ::RegCreateKeyW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Doraemon\\admin", &hKey);//创建失败,以管理员方式运行。
if (ret)
{
TRACE("RegOpenKeyW Failed,ErrorCode = %d", GetLastError());
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", GetLastError());
MessageBox(L"读注册表失败");
return;
}
TRACE("dwType = %d,dwValue = %d,dwAge = %d",dwType,dwValue,dwAge);
::RegCloseKey(hKey);
} |
|