CRegistry::CRegistry() { m_bInit = false; } CRegistry::CRegistry(HKEY hKey) { m_bInit = false; m_OpenKey = hKey; } CRegistry::~CRegistry() { if(m_bInit) RegCloseKey(m_hKey); } void CRegistry::SetRoot(HKEY hKey) { if(m_bInit) { RegCloseKey(m_hKey); } m_bInit = false; m_OpenKey = hKey; } BOOL CRegistry::Open(LPCTSTR lpSubKey) { if(m_bInit) { RegCloseKey(m_hKey); m_bInit = false; } if(RegOpenKeyEx(m_OpenKey, lpSubKey, 0, KEY_ALL_ACCESS, &m_hKey) == ERROR_SUCCESS) { m_bInit = true; } return m_bInit; } void CRegistry::GetValue(LPCTSTR lpSubKey, CString& strVal) { if(!m_bInit) return; DWORD dwLen = 256; BYTE pData[256] = {0}; DWORD dwType = REG_SZ; if(RegQueryValueEx(m_hKey, lpSubKey, 0, &dwType, pData, &dwLen) == ERROR_SUCCESS) { strVal.Format("%s", pData); } } void CRegistry::GetValue(LPCTSTR lpSubKey, DWORD& dwVal) { if(!m_bInit) return; DWORD dwLen = 8; BYTE pData[8] = {0}; DWORD dwType = REG_DWORD; if(RegQueryValueEx(m_hKey, lpSubKey, 0, &dwType, pData, &dwLen) == ERROR_SUCCESS) { dwVal = *((DWORD*)pData); } } void CRegistry::SetValue(LPCTSTR lpSubKey, CString str) { RegSetValueEx(m_hKey, lpSubKey, 0, REG_SZ, (BYTE*)str.GetBuffer(0), str.GetLength()); str.ReleaseBuffer(); } CString CRegistry::GetValue(LPCTSTR lpSubKey) { CString strRet = ""; GetValue(lpSubKey, strRet); return strRet; }