MFC中的CRegKey类,是注册表操作类,用法如下:
(1)创建键函数:
CRegKey key; long ret = key.Create(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Scale3DC"), REG_NONE, REG_OPTION_NON_VOLATILE, KEY_WRITE | KEY_READ | KEY_WOW64_64KEY); key.Close(); //在WIN10下须管理员权限
(2)打开键并写入值:
int iMaxNumber = 100; key.Open(HKEY_CURRENT_USER, _T("SOFTWARE\\TheSoft")); key.SetStringValue(_T("Password"), _T("123")); key.SetDWORDValue(_T("count"),iMaxNumber); key.Close(); long ret1 = key.Open(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\Scale3DC"), KEY_SET_VALUE | KEY_WOW64_64KEY); if (ret1 == ERROR_SUCCESS) { long ret =key.SetStringValue(_T("DisplayName"), _T("Scale3DC管理系统")); key.SetStringValue(_T("UninstallString"), GetApplicationPath() + _T("Uninstall.exe")); key.SetStringValue(_T("DisplayIcon"), GetApplicationPath() + _T("Scale3DC.exe")); key.SetStringValue(_T("DisplayVersion"), _T("v2018")); key.SetStringValue(_T("URLInfoAbout"), _T("http://www.zync.com")); key.SetStringValue(_T("Publisher"), _T("3DC科技有限公司")); } key.Close();
(3)读取注册表的信息:
DWORD dwNum; BOOL bOpenReg = key.Open(HKEY_CURRENT_USER, _T("SOFTWARE\\TheSoft")); //或者key.QueryValue(dwNum,_T("count")); key.QueryDWORDValue(_T("count"),dwNum);
(4)删除注册的信息:
key.DeleteValue(_T("count"));
用API删除注册表中的键
VOID CScale3DCControl::DeleteMainKey() { HKEY hKey; DWORD result; result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"), NULL, KEY_SET_VALUE|KEY_WOW64_64KEY, &hKey);//读取注册表串口信息 if (ERROR_SUCCESS == result) { long lResult = ::RegDeleteKey(hKey, _T("Scale3DC")); if (lResult == ERROR_SUCCESS) { AfxMessageBox(_T("ok1")); } } else { ::MessageBox(0, _T("DELETE ERROR!"), _T("Error"), 0); } RegCloseKey(hKey); }
Win10系统须管理员权限
(5)读注册表
HKEY hKey; DWORD result; DWORD dwType; DWORD sl = 255; TCHAR sz[255]; //存储键值 result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\abc"), NULL, KEY_ALL_ACCESS | KEY_WOW64_64KEY, &hKey); if (ERROR_SUCCESS == result) { result = RegQueryValueEx(hKey, _T("DisplayIcon"), NULL, &dwType, (LPBYTE)sz, &sl); if (result == ERROR_SUCCESS) { RegCloseKey(hKey); return sz; } } else { ::MessageBox(0, _T("DELETE ERROR!"), _T("Error"), 0); } RegCloseKey(hKey);