// RegAccess.cpp: implementation of the CRegAccess class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "RegAccess.h"
CRegAccess::CRegAccess()
: m_hKey(NULL)
{
}
CRegAccess::~CRegAccess()
{
if (m_hKey)
RegCloseKey(m_hKey);
}
bool CRegAccess::Open(HKEY mainKey, const char* pKey, bool bReadOnly)
{
if (!pKey)
return false;
DWORD Disposition;
return RegCreateKeyEx(mainKey, pKey, 0, REG_NONE, REG_OPTION_NON_VOLATILE, KEY_READ | (bReadOnly ? 0 : KEY_WRITE), NULL, &m_hKey, &Disposition) == ERROR_SUCCESS;
}
bool CRegAccess::WriteKey(const char* pSubKey, const char* pValue)
{
if (!pSubKey || !pValue)
return false;
return RegSetValueEx(m_hKey, pSubKey, 0, REG_SZ, (BYTE*)pValue, lstrlen(pValue)) == ERROR_SUCCESS;
}
bool CRegAccess::WriteKey(const char* pSubKey, DWORD nValue)
{
if (!pSubKey)
return false;
return RegSetValueEx(m_hKey, pSubKey, 0, REG_DWORD, (BYTE*)&nValue, sizeof(DWORD)) == ERROR_SUCCESS;
}
bool CRegAccess::ReadKey(const char* pSubKey, char* buffer, int nLen)
{
if (!pSubKey || !buffer || nLen <= 0)
return false;
*buffer = 0;
DWORD nLength = nLen;
DWORD nType = 0;
bool bRes = RegQueryValueEx(m_hKey, pSubKey, NULL, &nType, (BYTE*)buffer, &nLength) == ERROR_SUCCESS
&& nType == REG_SZ;
return bRes;
}
bool CRegAccess::ReadKey(const char* pSubKey, DWORD& nValue)
{
if (!pSubKey)
return false;
DWORD nLength = sizeof(DWORD);
DWORD nType = 0;
bool bRes = RegQueryValueEx(m_hKey, pSubKey, NULL, &nType, (BYTE*)&nValue, &nLength) == ERROR_SUCCESS
&& nType == REG_DWORD;
return bRes;
}