Registry Operations with ATL class:                                        Notes   Code

#include <atlbase.h>  // for CRegKey

class CRegistry
{
    public:
	CRegistry()
	{
	    m_RegKey.m_hKey = HKEY_CURRENT_USER;
	}
    
	CRegKey m_RegKey;
	
	BOOL OpenKey( LPCTSTR lpszKeyName );
	BOOL CreateKey( LPCTSTR lpszKeyName );
	
	BOOL ReadBoolean( BOOL& bValue, LPCTSTR lpszValueName );
	BOOL ReadString( LPTSTR szValue, LPCTSTR lpszValueName );
	BOOL ReadInteger( DWORD& dwValue, LPCTSTR lpszValueName );
	BOOL ReadFloat( FLOAT& fValue, LPCTSTR lpszValueName );

	BOOL WriteBoolean( BOOL bValue, LPCTSTR lpszValueName );
	BOOL WriteString( LPCTSTR lpszValue, LPCTSTR lpszValueName );
	BOOL WriteInteger( DWORD dwValue, LPCTSTR lpszValueName );
	BOOL WriteFloat( FLOAT fValue, LPCTSTR lpszValueName );

	BOOL DeleteThisKey( LPCTSTR lpszSubKey  );
	BOOL DeleteKeyAndAllSubKeys( LPCTSTR lpszKey  );

	BOOL CloseKey();
};

BOOL CRegistry::OpenKey( LPCTSTR lpszKeyName )
{
    HKEY hKeyParent = m_RegKey.m_hKey;

    LONG lResult =  m_RegKey.Open( hKeyParent,
                                   lpszKeyName,
                                   KEY_ALL_ACCESS  );

    if ( lResult == ERROR_SUCCESS )
    {
        return TRUE;
    }
    return FALSE;
}

BOOL CRegistry::CreateKey( LPCTSTR lpszKeyName )
{
     HKEY hKeyParent = m_RegKey.m_hKey;

     LONG lResult = m_RegKey.Create( hKeyParent,
	                             lpszKeyName,
	                             REG_NONE,
	                             REG_OPTION_NON_VOLATILE,
	                             KEY_ALL_ACCESS,
	                             NULL,
	                             NULL );

     if ( lResult == ERROR_SUCCESS )
     {
         return TRUE;
     }
     return FALSE;
}

BOOL CRegistry::DeleteThisKey( LPCTSTR lpszSubKey  )
{
    LONG lResult =  m_RegKey.DeleteSubKey( lpszSubKey );

    if ( lResult == ERROR_SUCCESS )
    {
        return TRUE;
    }
    return FALSE;
}

BOOL CRegistry::DeleteKeyAndAllSubKeys( LPCTSTR lpszKey  )
{
    LONG lResult =  m_RegKey.RecurseDeleteKey( lpszKey );

    if ( lResult == ERROR_SUCCESS )
    {
        return TRUE;
    }
    return FALSE;
}

BOOL CRegistry::ReadBoolean( BOOL& bValue, LPCTSTR lpszValueName )
{
     DWORD dwValue = bValue;

     LONG lResult =  m_RegKey.QueryValue( dwValue,  lpszValueName );

     if ( lResult == ERROR_SUCCESS )
     {
         if ( dwValue == 1 )
	 {
	      bValue = TRUE;
	 }
	 else
	 {
	     bValue = FALSE;
	 }
	 return TRUE;
     }
     return FALSE;
}

BOOL CRegistry::ReadString( LPTSTR szValue, LPCTSTR lpszValueName )
{
    DWORD dwCount = 20; //sizeof(DWORD );

    LONG lResult =  m_RegKey.QueryValue( szValue, 
                                         lpszValueName, 
                                         &dwCount );

    if ( lResult == ERROR_SUCCESS )
    {
        return TRUE;
    }
    return FALSE;
}

BOOL CRegistry::ReadInteger( DWORD& dwValue, LPCTSTR lpszValueName )
{
    LONG lResult =  m_RegKey.QueryValue( dwValue,  lpszValueName );

    if ( lResult == ERROR_SUCCESS )
    {
        return TRUE;
    }
    return FALSE;
}

BOOL CRegistry::ReadFloat( FLOAT& fValue, LPCTSTR lpszValueName )
{
    char szValueData[20];

    DWORD dwCount = 20; //sizeof(DWORD );

    LONG lResult =  m_RegKey.QueryValue( szValueData, lpszValueName, &dwCount );

    fValue = (float) atof ( szValueData );

    if ( lResult == ERROR_SUCCESS )
    {
        return TRUE;
    }
    return FALSE;
}


BOOL CRegistry::WriteBoolean( BOOL bValue, LPCTSTR lpszValueName )
{
    DWORD dwValue;

    if( bValue )
    {
        dwValue = 1;
    }
    else
    {
        dwValue = 0;
    }

    LONG lResult =  m_RegKey.SetValue( dwValue, lpszValueName );;

    if ( lResult == ERROR_SUCCESS )
    {
        return TRUE;
    }
    return FALSE;
}

BOOL CRegistry::WriteString( LPCTSTR lpszValue, LPCTSTR lpszValueName )
{
    DWORD dwCount = 20; //sizeof(DWORD );

    LONG lResult =  m_RegKey.SetValue( lpszValue, lpszValueName );

    if ( lResult == ERROR_SUCCESS )
    {
        return TRUE;
    }
    return FALSE;
}

BOOL CRegistry::WriteInteger( DWORD dwValue, LPCTSTR lpszValueName )
{
    LONG lResult =  m_RegKey.SetValue( dwValue, lpszValueName );;

    if ( lResult == ERROR_SUCCESS )
    {
        return TRUE;
    }
    return FALSE;
}

BOOL CRegistry::WriteFloat( FLOAT fValue, LPCTSTR lpszValueName )
{
    char szValueData[20];
    sprintf( szValueData,"%.3f", fValue );

    LONG lResult =  m_RegKey.SetValue( szValueData, lpszValueName );

    if ( lResult == ERROR_SUCCESS )
    {
        return TRUE;
    }
    return FALSE;
}

BOOL CRegistry::CloseKey()
{
    LONG lResult =  m_RegKey.Close();

    if ( lResult == ERROR_SUCCESS )
    {
        return TRUE;
    }
    return FALSE;
}
(c) 2001, Stan Malevanny