/////////////////////////////////////////////////////////////////////////////
// Copyright (C) 1998 by Shane Martin
// All rights reserved
//
// Distribute freely, except: don't remove my name from the source or
// documentation (don't take credit for my work), mark your changes (don't
// get me blamed for your possible bugs), don't alter or remove this
// notice.
// No warrantee of any kind, express or implied, is included with this
// software; use at your own risk, responsibility for damages (if any) to
// anyone resulting from the use of this software rests entirely with the
// user.
//
// Send bug reports, bug fixes, enhancements, requests, flames, etc., and
// I'll try to keep a version up to date. I can be reached as follows:
// [email protected]
/////////////////////////////////////////////////////////////////////////////
// last revised: 24 Apr 98
// Registry.cpp : implementation file
//
// Description:
// CRegistryEx is a wrapper for the Windows Registry API. It allows
// easy modification of the Registry with easy to remember terms like
// Read, Write, Open, and Close.
// *********************************************
// Seain B. Conover 1 Jan 1999
// added ::getprofilestring and ::findkey
#include "stdafx.h"
#include "RegistryEx.h"
CRegistryEx::CRegistryEx(HKEY hKeyRoot)
{
m_hKey = hKeyRoot;
}
CRegistryEx::~CRegistryEx()
{
Close();
}
BOOL CRegistryEx::VerifyKey (HKEY hKeyRoot, LPCTSTR pszPath)
{
ASSERT (hKeyRoot);
ASSERT (pszPath);
LONG ReturnValue = RegOpenKeyEx (hKeyRoot, pszPath, 0L,
KEY_ALL_ACCESS, &m_hKey);
if(ReturnValue == ERROR_SUCCESS)
return TRUE;
m_Info.lMessage = ReturnValue;
m_Info.dwSize = 0L;
m_Info.dwType = 0L;
return FALSE;
}
BOOL CRegistryEx::VerifyKey (LPCTSTR pszPath)
{
ASSERT (m_hKey);
LONG ReturnValue = RegOpenKeyEx (m_hKey, pszPath, 0L,
KEY_ALL_ACCESS, &m_hKey);
m_Info.lMessage = ReturnValue;
m_Info.dwSize = 0L;
m_Info.dwType = 0L;
if(ReturnValue == ERROR_SUCCESS)
return TRUE;
return FALSE;
}
BOOL CRegistryEx::VerifyValue (LPCTSTR pszValue)
{
ASSERT(m_hKey);
LONG lReturn = RegQueryValueEx(m_hKey, pszValue, NULL,
NULL, NULL, NULL);
m_Info.lMessage = lReturn;
m_Info.dwSize = 0L;
m_Info.dwType = 0L;
if(lReturn == ERROR_SUCCESS)
return TRUE;
return FALSE;
}
BOOL CRegistryEx::CreateKey (HKEY hKeyRoot, LPCTSTR pszPath)
{
DWORD dw;
LONG ReturnValue = RegCreateKeyEx (hKeyRoot, pszPath, 0L, NULL,
REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL,
&m_hKey, &dw);
m_Info.lMessage = ReturnValue;
m_Info.dwSize = 0L;
m_Info.dwType = 0L;
if(ReturnValue == ERROR_SUCCESS)
return TRUE;
return FALSE;
}
BOOL CRegistryEx::Open (HKEY hKeyRoot, LPCTSTR pszPath)
{
m_sPath = pszPath;
LONG ReturnValue = RegOpenKeyEx (hKeyRoot, pszPath, 0L,
KEY_ALL_ACCESS, &m_hKey);
m_Info.lMessage = ReturnValue;
m_Info.dwSize = 0L;
m_Info.dwType = 0L;
if(ReturnValue == ERROR_SUCCESS)
return TRUE;
return FALSE;
}
void CRegistryEx::Close()
{
if (m_hKey)
{
RegCloseKey (m_hKey);
m_hKey = NULL;
}
}
BOOL CRegistryEx::Write (LPCTSTR pszKey, int iVal)
{
DWORD dwValue;
ASSERT(m_hKey);
ASSERT(pszKey);
dwValue = (DWORD)iVal;
LONG ReturnValue = RegSetValueEx (m_hKey, pszKey, 0L, REG_DWORD,
(CONST BYTE*) &dwValue, sizeof(DWORD));
m_Info.lMessage = ReturnValue;
m_Info.dwSize = sizeof(DWORD);
m_Info.dwType = REG_DWORD;
if(ReturnValue == ERROR_SUCCESS)
return TRUE;
return FALSE;
}
BOOL CRegistryEx::Write (LPCTSTR pszKey, DWORD dwVal)
{
ASSERT(m_hKey);
ASSERT(pszKey);
return RegSetValueEx (m_hKey, pszKey, 0L, REG_DWORD,
(CONST BYTE*) &dwVal, sizeof(DWORD));
}
BOOL CRegistryEx::Write (LPCTSTR pszKey, LPCTSTR pszData)
{
ASSERT(m_hKey);
ASSERT(pszKey);
ASSERT(pszData);
ASSERT(AfxIsValidAddress(pszData, strlen(pszData), FALSE));
LONG ReturnValue = RegSetValueEx (m_hKey, pszKey, 0L, REG_SZ,
(CONST BYTE*) pszData, strlen(pszData) + 1);
m_Info.lMessage = ReturnValue;
m_Info.dwSize = strlen(pszData) + 1;
m_Info.dwType = REG_SZ;
if(ReturnValue == ERROR_SUCCESS)
return TRUE;
return FALSE;
}
BOOL CRegistryEx::Write (LPCTSTR pszKey, CStringList& scStringList)
{
ASSERT(m_hKey);
ASSERT(pszKey);
const int iMaxChars = 4096;
BYTE* byData = (BYTE*)::calloc(iMaxChars, sizeof(TCHAR));
ASSERT(byData);
CMemFile file(byData, iMaxChars, 16);
CArchive ar(&file, CArchive::store);
ASSERT(scStringList.IsSerializable());
scStringList.Serialize(ar);
ar.Close();
const DWORD dwLen = file.GetLength();
ASSERT(dwLen < iMaxChars);
LONG lReturn = RegSetValueEx(m_hKey, pszKey, 0, REG_BINARY,
file.Detach(), dwLen);
m_Info.lMessage = lReturn;
m_Info.dwSize = dwLen;
m_Info.dwType = REG_BINARY;
if(byData)
{
free(byData);
byData = NULL;
}
if(lReturn == ERROR_SUCCESS)
return TRUE;
return FALSE;
}
BOOL CRegistryEx::Write (LPCTSTR pszKey, CByteArray& bcArray)
{
ASSERT(m_hKey);
ASSERT(pszKey);
const int iMaxChars = 4096;
BYTE* byData = (BYTE*)::calloc(iMaxChars, sizeof(TCHAR));
ASSERT(byData);
CMemFile file(byData, iMaxChars, 16);
CArchive ar(&file, CArchive::store);
ASSERT(bcArray.IsSerializable());
bcArray.Serialize(ar);
ar.Close();
const DWORD dwLen = file.GetLength();
ASSERT(dwLen < iMaxChars);
LONG lReturn = RegSetValueEx(m_hKey, pszKey, 0, REG_BINARY,
file.Detach(), dwLen);
m_Info.lMessage = lReturn;
m_Info.dwSize = dwLen;
m_Info.dwType = REG_BINARY;
if(byData)
{
free(byData);
byData = NULL;
}
if(lReturn == ERROR_SUCCESS)
return TRUE;
return FALSE;
}
BOOL CRegistryEx::Write (LPCTSTR pszKey, CDWordArray& dwcArray)
{
ASSERT(m_hKey);
ASSERT(pszKey);
const int iMaxChars = 4096;
BYTE* byData = (BYTE*)::calloc(iMaxChars, sizeof(TCHAR));
ASSERT(byData);
CMemFile file(byData, iMaxChars, 16);
CArchive ar(&file, CArchive::store);
ASSERT(dwcArray.IsSerializable());
dwcArray.Serialize(ar);
ar.Close();
const DWORD dwLen = file.GetLength();
ASSERT(dwLen < iMaxChars);
LONG lReturn = RegSetValueEx(m_hKey, pszKey, 0, REG_BINARY,
file.Detach(), dwLen);
m_Info.lMessage = lReturn;
m_Info.dwSize = dwLen;
m_Info.dwType = REG_BINARY;
if(byData)
{
free(byData);
byData = NULL;
}
if(lReturn == ERROR_SUCCESS)
return TRUE;
return FALSE;
}
BOOL CRegistryEx::Write (LPCTSTR pszKey, CWordArray& wcArray)
{
ASSERT(m_hKey);
ASSERT(pszKey);
const int iMaxChars = 4096;
BYTE* byData = (BYTE*)::calloc(iMaxChars, sizeof(TCHAR));
ASSERT(byData);
CMemFile file(byData, iMaxChars, 16);
CArchive ar(&file, CArchive::store);
ASSERT(wcArray.IsSerializable());
wcArray.Serialize(ar);
ar.Close();
const DWORD dwLen = file.GetLength();
ASSERT(dwLen < iMaxChars);
LONG lReturn = RegSetValueEx(m_hKey, pszKey, 0, REG_BINARY,
file.Detach(), dwLen);
m_Info.lMessage = lReturn;
m_Info.dwSize = dwLen;
m_Info.dwType = REG_BINARY;
if(byData)
{
free(byData);
byData = NULL;
}
if(lReturn == ERROR_SUCCESS)
return TRUE;
return FALSE;
}
BOOL CRegistryEx::Write (LPCTSTR pszKey, CStringArray& scArray)
{
ASSERT(m_hKey);
ASSERT(pszKey);
const int iMaxChars = 4096;
BYTE* byData = (BYTE*)::calloc(iMaxChars, sizeof(TCHAR));
ASSERT(byData);
CMemFile file(byData, iMaxChars, 16);
CArchive ar(&file, CArchive::store);
ASSERT(scArray.IsSerializable());
scArray.Serialize(ar);
ar.Close();
const DWORD dwLen = file.GetLength();
ASSERT(dwLen < iMaxChars);
LONG lReturn = RegSetValueEx(m_hKey, pszKey, 0, REG_BINARY,
file.Detach(), dwLen);
m_Info.lMessage = lReturn;
m_Info.dwSize = dwLen;
m_Info.dwType = REG_BINARY;
if(byData)
{
free(byData);
byData = NULL;
}
if(lReturn == ERROR_SUCCESS)
return TRUE;
return FA