打开注册表
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) ;
}
设置值
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) ;
RegCloseKey ( hKey) ;