/********************************************************************
Created: 31/3/2004, 21:49
File name: D:\Projects\Exile\Exile\Registry.cpp
File path: D:\Projects\Exile\Exile
File base: Registry
File ext: cpp
Author: Gogolev Anton
*********************************************************************/
#include "StdAfx.h"
#include "Registry.h"
#include "WinReg.h"
#define CLASS_NAME_LENGTH 255;
CRegistry::CRegistry()
{
m_bDelayedWrite = FALSE;
m_hRootKey = HKEY_CURRENT_USER;
m_nLastError = ERROR_SUCCESS;
}
CRegistry::~CRegistry()
{
ClearKey();
}
BOOL CRegistry::ClearKey()
{
m_strCurrentPath.Empty();
m_bDelayedWrite = FALSE;
m_hRootKey = HKEY_CURRENT_USER;
return TRUE;
}
BOOL CRegistry::SetRootKey(HKEY hRootKey)
{
if((hRootKey != HKEY_CURRENT_USER) &&
(hRootKey != HKEY_LOCAL_MACHINE) &&
(hRootKey != HKEY_CLASSES_ROOT) &&
(hRootKey != HKEY_USERS))
return FALSE;
m_hRootKey = hRootKey;
return TRUE;
}
BOOL CRegistry::CreateKey(CString strKey)
{
if(strKey[0] == '\\')
return FALSE;
HKEY hKey;
DWORD dwDisposition = 0;
m_nLastError = ::RegCreateKeyEx(m_hRootKey, (LPCTSTR)strKey, 0, NULL,
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey,
&dwDisposition);
if(m_nLastError != ERROR_SUCCESS)
return FALSE;
if(!m_bDelayedWrite)
::RegFlushKey(hKey);
::RegCloseKey(hKey);
m_strCurrentPath = strKey;
return TRUE;
}
BOOL CRegistry::DeleteKey(CString strKey)
{
if((strKey[0] == '\\') || (strKey.GetLength() == 0))
return FALSE;
HKEY hKey;
m_nLastError = ::RegOpenKeyEx(m_hRootKey, (LPCTSTR)strKey, 0,
KEY_ALL_ACCESS, (PHKEY)&hKey);
if(m_nLastError != ERROR_SUCCESS)
return FALSE;
m_nLastError = ::RegDeleteKey(hKey, (LPCTSTR)strKey);
return (m_nLastError == ERROR_SUCCESS);
}
BOOL CRegistry::DeleteValue(CString strKey)
{
if(m_strCurrentPath.GetLength() < 0)
return FALSE;
HKEY hKey;
if(::RegOpenKeyEx(m_hRootKey, (LPCTSTR)m_strCurrentPath, 0,
KEY_SET_VALUE, (PHKEY)&hKey) != ERROR_SUCCESS)
return FALSE;
m_nLastError = ::RegDeleteValue(hKey, (LPCTSTR)strKey);
::RegCloseKey(hKey);
if(m_nLastError != ERROR_SUCCESS)
return FALSE;
else
return TRUE;
}
BOOL CRegistry::KeyExists(CString strKeyName, HKEY hRoot)
{
if(strKeyName[0] == '\\')
return FALSE;
HKEY hKey;
if(hRoot == NULL)
hRoot = m_hRootKey;
m_nLastError = ::RegOpenKeyEx(hRoot, (LPCTSTR)strKeyName, 0,
KEY_ALL_ACCESS, (PHKEY)&hKey);
::RegCloseKey(hKey);
return (m_nLastError == ERROR_SUCCESS);
}
BOOL CRegistry::SetKey(CString strKeyName, BOOL bCanCreate)
{
if(strKeyName[0] == '\\')
return FALSE;
HKEY hKey;
DWORD dwDisposition;
if(strKeyName.GetLength() == 0)
{
m_strCurrentPath.Empty();
return TRUE;
}
if(bCanCreate)
{
m_nLastError = ::RegCreateKeyEx(m_hRootKey, (LPCTSTR)strKeyName,
0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL,
(PHKEY)&hKey, &dwDisposition);
if(m_nLastError != ERROR_SUCCESS)
return FALSE;
m_strCurrentPath = strKeyName;
if(!m_bDelayedWrite)
::RegFlushKey(hKey);
::RegCloseKey(hKey);
return TRUE;
}
else
{
m_nLastError = ::RegOpenKeyEx(m_hRootKey, (LPCTSTR)strKeyName,
0, KEY_ALL_ACCESS, (PHKEY)&hKey);
if(m_nLastError != ERROR_SUCCESS)
return FALSE;
m_strCurrentPath = strKeyName;
if(!m_bDelayedWrite)
::RegFlushKey(hKey);
::RegCloseKey(hKey);
return TRUE;
}
}
double CRegistry::ReadDouble(CString strKeyName, double fDefault)
{
DWORD dwType = REG_BINARY;
double fRes;
DWORD dwSize = sizeof(fRes);
HKEY hKey;
ASSERT(m_strCurrentPath.GetLength() > 0);
if ((m_nLastError = ::RegOpenKeyEx(m_hRootKey, (LPCTSTR)m_strCurrentPath, 0,
KEY_READ, (PHKEY)&hKey)) != ERROR_SUCCESS) return fDefault;
if (::RegQueryValueEx(hKey, (LPCTSTR)strKeyName, NULL,
&dwType, (LPBYTE)&fRes, &dwSize) != ERROR_SUCCESS)
fRes = fDefault;
::RegCloseKey(hKey);
return fRes;
}
CString CRegistry::ReadString(CString strKeyName, CString strDefault)
{
DWORD dwType = REG_SZ;
DWORD dwSize = 255;
BOOL bSuccess = TRUE;
_TCHAR sz[255];
HKEY hKey;
ASSERT(m_strCurrentPath.GetLength() > 0);
m_nLastError = ::RegOpenKeyEx(m_hRootKey, (LPCTSTR)m_strCurrentPath, 0,
KEY_READ, (PHKEY)&hKey);
if (m_nLastError != ERROR_SUCCESS)
return strDefault;
m_nLastError = ::RegQueryValueEx(hKey, (LPCTSTR)strKeyName, NULL,
&dwType, (LPBYTE)sz, &dwSize);
if (m_nLastError != ERROR_SUCCESS)
bSuccess = FALSE;
::RegCloseKey(hKey);
if (!bSuccess)
return strDefault;
return CString((LPCTSTR)sz);
}
DWORD CRegistry::ReadDword(CString strKeyName, DWORD dwDefault)
{
DWORD dwType = REG_DWORD;
DWORD dwRes;
DWORD dwSize = sizeof(dwRes);
HKEY hKey;
ASSERT(m_strCurrentPath.GetLength() > 0);
if ((m_nLastError = ::RegOpenKeyEx(m_hRootKey, (LPCTSTR)m_strCurrentPath, 0,
KEY_READ, (PHKEY)&hKey)) != ERROR_SUCCESS)
return dwDefault;
if (::RegQueryValueEx(hKey, (LPCTSTR)strKeyName, NULL,
&dwType, (LPBYTE)&dwRes, &dwSize) != ERROR_SUCCESS)
dwRes = dwDefault;
::RegCloseKey(hKey);
return dwRes;
}
int CRegistry::ReadInt(CString strKeyName, int nDefault)
{
DWORD dwType = REG_BINARY;
int nRes;
DWORD dwSize = sizeof(nRes);
HKEY hKey;
ASSERT(m_strCurrentPath.GetLength() > 0);
if ((m_nLastError = ::RegOpenKeyEx(m_hRootKey, (LPCTSTR)m_strCurrentPath, 0,
KEY_READ, (PHKEY)&hKey)) != ERROR_SUCCESS)
return nDefault;
if (::RegQueryValueEx(hKey, (LPCTSTR)strKeyName, NULL,
&dwType, (LPBYTE)&nRes, &dwSize) != ERROR_SUCCESS)
nRes = nDefault;
::RegCloseKey(hKey);
return nRes;
}
BOOL CRegistry::ReadBool(CString strKeyName, BOOL bDefault)
{
DWORD dwType = REG_BINARY;
BOOL bRes;
DWORD dwSize = sizeof(bRes);
HKEY hKey;
ASSERT(m_strCurrentPath.GetLength() > 0);
if ((m_nLastError = ::RegOpenKeyEx(m_hRootKey, (LPCTSTR)m_strCurrentPath, 0,
KEY_READ, (PHKEY)&hKey)) != ERROR_SUCCESS)
return bDefault;
if (::RegQueryValueEx(hKey, (LPCTSTR)strKeyName, NULL,
&dwType, (LPBYTE)&bRes, &dwSize) != ERROR_SUCCESS)
bRes = bDefault;
::RegCloseKey(hKey);
return bRes;
}
BOOL CRegistry::WriteDouble(CString strKeyName, double fValue)
{
ASSERT(m_strCurrentPath.GetLength() > 0);
BOOL bSuccess = TRUE;
HKEY hKey;
if ((m_nLastError = ::RegOpenKeyEx(m_hRootKey, (LPCTSTR)m_strCurrentPath, 0,
KEY_WRITE, (PHKEY)&hKey)) != ERROR_SUCCESS)
return FALSE;
if (::RegSetValueEx(hKey, (LPCTSTR)strKeyName, 0,
REG_BINARY, (LPBYTE)&fValue, sizeof(fValue)) != ERROR_SUCCESS)
bSuccess = FALSE;
if (!m_bDelayedWrite)
::RegFlushKey(hKey);
::RegCloseKey(hKey);
return bSuccess;
}
BOOL CRegistry::WriteString(CString strKeyName, CString strValue)
{
ASSERT(m_strCurrentPath.GetLength() > 0);
BOOL bSuccess = TRUE;
HKEY hKey;
_TCHAR sz[255];
if (strValue.GetLength() > 254) return FALSE;
#ifdef _UNICODE
wstrcpy(sz, (LPCTSTR)strValue);
#else
strcpy(sz, (LPCTSTR)strValue);
#endif
if ((m_nLastError = ::RegOpenKeyEx(m_hRootKey, (LPCTSTR)m_strCurrentPath, 0,
KEY_WRITE, (PHKEY)&hKey)) != ERROR_SUCCESS)
return FALSE;
#ifdef _UNICODE
if (::RegSetValueEx(hKey, (LPCTSTR)strKeyName, 0,
REG_SZ, (LPBYTE)sz, wstrlen(sz) + 1) != ERROR_SUCCESS)
bSuccess = FALSE;
#else
if (::RegSetValueEx(hKey, (LPCTSTR)strKeyName, 0,
REG_SZ, (LPBYTE)sz, strlen(sz) + 1) != ERROR_SUCCESS)
bSuccess = FALSE;
#endif
if (!m_bDelayedWrite)
::RegFlushKey(hKey);
::RegCloseKey(hKey);
return bSuccess;
}
BOOL CRegistry::WriteDword(CString strKeyName, DWORD dwValue)
{
ASSERT(m_strCurrentPath.GetLength() > 0);
BOOL bSuccess = TRUE;
HKEY hKey;
if ((m_nLastError = ::RegOpenKeyEx(m_hRootKey, (LPCTSTR)m_strCurrentPath, 0,
KEY_WRITE, (PHKEY)&hKey)) != ERROR_SUCCESS)
return FALSE;
if (::RegSetValueEx(hKey, (LPCTSTR)strKeyName, 0,
REG_BINARY, (LPBYTE)&dwValue, sizeof(dwValue)) != ERROR_SUCCESS)
bSuccess = FALSE;
if (!m_bDelayedWrite)
::RegFlushKey(hKey);
::RegCloseKey(hKey);
return bSuccess;
}
BOOL CRegistry::WriteInt(CString strKeyName, int nValue)
{
ASSERT(m_strCurrentPath.GetLength() > 0);
BOOL bSuccess = TRUE;
HKEY hKey;
if ((m_nLastError = ::RegOpenKeyEx(m_hRootKey, (LPCTSTR)m_strCurrentPath, 0,
KEY_WRITE, (PHKEY)&hKey)) != ERROR_SUCCESS)
return FALSE;
if (::RegSetValueEx(hKey, (LPCTSTR)strKeyName, 0,
REG_BINARY, (LPBYTE)&nValue, sizeof(nValue)) != ERROR_SUCCESS)
bSuccess = FALSE;
if (!m_bDelayedWrite)
::RegFlushKey(hKey);
::RegCloseKey(hKey);
return bSuccess;
}
BOOL CRegistry::WriteBool(CString strKeyName, BOOL bValue)
{
ASSERT(m_strCurrentPath.GetLength() > 0);
BOOL bSuccess = TRUE;
HKEY hKey;
if ((m_nLastError = ::RegOpenKeyEx(m_hRootKey, (LPCTSTR)m_strCurrentPath, 0,
KEY_WRITE, (PHKEY)&hKey)) != ERROR_SUCCESS)
return FALSE;
if (::RegSetValueEx(hKey, (LPCTSTR)strKeyName, 0,
REG_BINARY, (LPBYTE)&bValue, sizeof(bValue)) != ERROR_SUCCESS)
bSuccess = FALSE;
if (!m_bDelayedWrite)
::RegFlushKey(hKey);
::RegCloseKey(hKey);
return bSuccess;
}
COleDateTime CRegistry::ReadDateTime(CString strKeyName, COleDateTime &dtDefault)
{
DWORD dwType = REG_BINARY;
int anDateTime[6];
DWORD dwSize = sizeof(anDateTime);
HKEY hKey;
COleDateTime dtRes;
ASSERT(m_strCurrentPath.GetLength() > 0);
if ((m_nLastError = ::RegOpenKeyEx(m_hRootKey, (LPCTSTR)m_strCurrentPath, 0,
KEY_READ, (PHKEY)&hKey)) != ERROR_SUCCESS)
return dtDefault;
if (::RegQueryValueEx(hKey, (LPCTSTR)strKeyName, NULL,
&dwType, (LPBYTE)&anDateTime, &dwSize) != ERROR_SUCCESS)
dtRes = dtDefault;
else
dtRes.SetDateTime(anDateTime[0], anDateTime[1], anDateTime[2], anDateTime[3], anDateTime[4],
anDateTime[5]);
::RegCloseKey(hKey);
return dtRes;
}
BOOL CRegistry::WriteDateTime(CString strKeyName, COleDateTime &dtValue)
{
ASSERT(m_strCurrentPath.GetLength() > 0);
BOOL bSuccess = TRUE;
HKEY hKey;
int anDateTime[6];
anDateTime[0] = dtValue.GetYear();
anDateTime[1] = dtValue.GetMonth();
anDateTime[2] = dtValue.GetDay();
anDateTime[3] = dtValue.GetHour();
anDateTime[4] = dtValue.GetMinute();
anDateTime[5] = dtValue.GetSecond();
if ((m_nLastError = ::RegOpenKeyEx(m_hRootKey, (LPCTSTR)m_strCurrentPath, 0,
KEY_WRITE, (PHKEY)&hKey)) != ERROR_SUCCESS)
return FALSE;
if (::RegSetValueEx(hKey, (LPCTSTR)strKeyName, 0,
REG_BINARY, (LPBYTE)&anDateTime, sizeof(anDateTime)) != ERROR_SUCCESS)
bSuccess = FALSE;
if (!m_bDelayedWrite)
::RegFlushKey(hKey);
::RegCloseKey(hKey);
return bSuccess;
}