// RegKey.cpp: implementation of the CRegKey class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"

#include "RegKey.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CRegKey::CRegKey()
{
	m_hKey = NULL;
}

CRegKey::~CRegKey()
{
	Close();
}

BOOL CRegKey::Open(HKEY hKeyRoot, CString sPath)
{
	DWORD dw;

	m_sPath = sPath;
	int iRet = RegCreateKeyEx(hKeyRoot, sPath, 0, NULL,
		REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL,
		&m_hKey, &dw);
	return (iRet == ERROR_SUCCESS) ? TRUE : FALSE;
}

void CRegKey::Close()
{
	if (m_hKey)
	{
		RegCloseKey(m_hKey);
		m_hKey = NULL;
	}
}

BOOL CRegKey::Write(CString sKey, int iVal)
{
	ASSERT(m_hKey);
	ASSERT(sKey);
	int iRet = RegSetValueEx(m_hKey, sKey, 0, REG_DWORD,
		(CONST BYTE *)&iVal, sizeof(int));
	return (iRet == ERROR_SUCCESS) ? TRUE : FALSE;
}

BOOL CRegKey::Write(CString sKey, CString sVal)
{
	ASSERT(m_hKey);
	ASSERT(sKey);
	ASSERT(sVal);
	ASSERT(AfxIsValidAddress(sVal, sVal.GetLength(), FALSE));
	int iRet = RegSetValueEx(m_hKey, sKey, 0, REG_SZ,
		(BYTE *)LPCTSTR(sVal), sVal.GetLength() + 1);
	return (iRet == ERROR_SUCCESS) ? TRUE : FALSE;
}

BOOL CRegKey::Write(CString sKey, const BYTE * pData, int iLength)
{
	ASSERT(m_hKey);
	ASSERT(sKey);
	ASSERT(pData && iLength > 0);
	ASSERT(AfxIsValidAddress(pData, iLength, FALSE));
	int iRet = RegSetValueEx(m_hKey, sKey, 0, REG_BINARY,
		pData, iLength);
	return (iRet == ERROR_SUCCESS) ? TRUE : FALSE;
}

BOOL CRegKey::Read(CString sKey, int & iVal)
{
	ASSERT(m_hKey);
	ASSERT(sKey);
	DWORD dwType;
	DWORD dwSize = sizeof(DWORD);
	DWORD dwDest;

	int iRet = RegQueryValueEx(m_hKey, sKey, NULL,
		&dwType, (BYTE *)&dwDest, &dwSize);
	if (iRet == ERROR_SUCCESS)
		iVal = dwDest;
	return (iRet == ERROR_SUCCESS) ? TRUE : FALSE;
}

BOOL CRegKey::Read(CString sKey, CString & sVal)
{
	ASSERT(m_hKey);
	ASSERT(sKey);
	DWORD dwType;
	DWORD dwSize = 200;
	char string[200];

	int iRet = RegQueryValueEx(m_hKey, sKey, NULL,
		&dwType, (BYTE *)string, &dwSize);
	if (iRet == ERROR_SUCCESS)
		sVal = string;
	return (iRet == ERROR_SUCCESS) ? TRUE : FALSE;
}

BOOL CRegKey::Read(CString sKey, BYTE * pData, int & iLength)
{
	ASSERT(m_hKey);
	ASSERT(sKey);
	DWORD dwType;
	int iRet = RegQueryValueEx(m_hKey, sKey, NULL,
		&dwType, pData, (DWORD *)&iLength);
	return (iRet == ERROR_SUCCESS) ? TRUE : FALSE;
}

