打开注册表
void CREGDemoDlg::OnBnClickedBtnOpen()
{
//打开注册表
HKEY hKey;
if (ERROR_SUCCESS != RegOpenKeyEx(HKEY_CURRENT_USER, L"SOFTWARE\\Baidu.com", 0, KEY_ALL_ACCESS, &hKey))
{
MessageBox(L"打开注册表失败");
return;
}
}
对注册表的键的操作
创建子键
//创建子键
HKEY hKey;
DWORD dwDisposition;
if (ERROR_SUCCESS != RegCreateKeyEx(HKEY_CURRENT_USER, L"Software\\Baidu.com\\12345", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, &dwDisposition))
{
MessageBox(L"创建失败");
}
删除子键
//删除子键
RegDeleteKey(HKEY_CURRENT_USER, L"Software\\Baidu.com\\12345");
遍历子键
//打开注册表
HKEY hKey;
if (ERROR_SUCCESS != RegOpenKeyEx(HKEY_CURRENT_USER, L"Software\\2345.com", 0, KEY_ENUMERATE_SUB_KEYS, &hKey))
{
MessageBox(L"打开注册表失败");
return;
}
DWORD dwIndex = 0;
TCHAR szName[MAX_PATH];
LONG result;
while (1)
{
result = RegEnumKey(hKey, dwIndex, szName, MAX_PATH);
if (result == ERROR_NO_MORE_ITEMS || result == ERROR_INVALID_HANDLE)
break;
else
{
szName.Format(L"%s\r\n", szName);
int nLength = m_edit.GetWindowTextLength();
m_edit.SetSel(nLength, -1);
m_edit.ReplaceSel(szName);
}
dwIndex++;
}
RegCloseKey(hKey);
对注册表值的操作
读取值
CString str;
HKEY hKey;
if (ERROR_SUCCESS != RegOpenKeyEx(HKEY_CURRENT_USER, L"SOFTWARE\\Baidu.com\\BaiduYunGuanjia", 0, KEY_ALL_ACCESS, &hKey))
{
str.Format(L"打开注册表失败.错误代号:%d",GetLastError());
MessageBox(str);
return;
}
DWORD dwType = REG_SZ;
TCHAR szData[MAX_PATH] = {0};
DWORD dwSize = 0;
//操作两遍
RegQueryValueEx(hKey, L"Version", NULL, &dwType,NULL, &dwSize);
str.Format(L"第一次查询.错误代号:%d", GetLastError());
MessageBox(str);
RegQueryValueEx(hKey, L"Version", NULL, &dwType,(LPBYTE)szData, &dwSize);
UpdateMsg(szData);
RegCloseKey(hKey)
void CREGDemoDlg::UpdateMsg(CString str)
{
str.Format(L"%s\r\n", str);
int nLength = m_edit.GetWindowTextLength();
m_edit.SetSel(nLength, -1);
m_edit.ReplaceSel(str);
}
文章来源:https://www.toymoban.com/news/detail-720091.html
设置值
CString str;
HKEY hKey;
if (ERROR_SUCCESS != RegOpenKeyEx(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_ALL_ACCESS, &hKey))
{
str.Format(L"打开注册表失败.错误代号:%d",GetLastError());
MessageBox(str);
return;
}
TCHAR szValue[MAX_PATH] = {0};
GetModuleFileName(NULL, szValue, MAX_PATH);//设置开机自启动
//RegSetValueEx(hKey, L"张三", 0, REG_SZ, (const BYTE*)szValue, MAX_PATH);//删除
RegCloseKey(hKey);
文章来源地址https://www.toymoban.com/news/detail-720091.html
到了这里,关于MFC 注册表的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!