Back to the WFC main page

CRegistry

$Revision: 40 $

Description

This class handles the registry API. The registry is a database that allows applications to store stuff. Microsoft has weird terms for dealing with the registry:
Hive
Kinda like a virtual disk drive
Key
Kinda like a directory
Value
Kinda like a file

Data Members

None

Methods

BOOL Close( void )
Closes the connection to the registry.
BOOL Connect( HKEY key_to_open = HKEY_CURRENT_USER, LPCTSTR computer_name = NULL )
Connects to a registry on a computer. The default is to connect to the local computer. If computer_name is not NULL, it will connect to that machine on the network.
BOOL Create( LPCTSTR               name_of_subkey,
             LPCTSTR               name_of_class         = NULL,
             CreateOptions         options               = optionsNonVolatile,
             CreatePermissions     permissions           = permissionAllAccess,
             LPSECURITY_ATTRIBUTES security_attributes_p = NULL,
             CreationDisposition * disposition_p         = NULL )
Creates a subkey in a registry hive. options can be one of:
  • optionsNonVolatile
  • optionsVolatile
permissions can be a combination of:
  • permissionAllAccess
  • permissionCreateLink
  • permissionCreateSubKey
  • permissionEnumerateSubKeys
  • permissionExecute
  • permissionNotify
  • permissionQueryValue
  • permissionRead
  • permissionSetValue
  • permissionWrite
disposition_p if not NULL, will be filled with either:
  • dispositionCreatedNewKey
  • dispositionOpenedExistingKey
It returns TRUE on success or FALSE on failure. The reason for the failure can be retrieved via GetErrorCode().
BOOL DeleteKey( LPCTSTR name_of_subkey_to_delete )
Deletes a key and all subkeys (even in NT) of that key. It returns TRUE on success or FALSE on failure. The reason for the failure can be retrieved via GetErrorCode().
BOOL DeleteValue( LPCTSTR name_of_value_to_delete )
Deletes a specific value. It returns TRUE on success or FALSE on failure. The reason for the failure can be retrieved via GetErrorCode().
BOOL EnumerateKeys( const DWORD subkey_index,
                    CString&    subkey_name,
                    CString&    class_name )
Let's you list the names of the keys. It returns TRUE on success or FALSE on failure. The reason for the failure can be retrieved via GetErrorCode().
BOOL EnumerateValues( const DWORD    value_index,
                      CString&       name_of_value,
                      KeyValueTypes& type_code,
                      LPBYTE         data_buffer,
                      DWORD&         size_of_data_buffer )
Let's you list the names of the values within a key. If you do not wish to retrieve the data, set data_buffer to NULL. size_of_data_buffer will be filled with the number of bytes put into data_buffer. It returns TRUE on success or FALSE on failure. The reason for the failure can be retrieved via GetErrorCode(). type_code will be one of:
  • typeBinary
  • typeDoubleWord
  • typeDoubleWordLittleEndian
  • typeDoubleWordBigEndian
  • typeUnexpandedString
  • typeSymbolicLink
  • typeMultipleString
  • typeNone
  • typeResourceList
  • typeString
NOTE: Remember that size_of_data_buffer must be reset within your enumerating loop because it is set by the function call. If you do not, you will probably get an error code of 234 (ERROR_MORE_DATA). Here's what a proper loop should look like:
BYTE buffer[ 4096 ];

DWORD size_of_buffer = 4096;

while( registry.EnumerateValues( enumerator, name, type, buffer, size_of_buffer ) == TRUE )
{
   _tprintf( TEXT( "Found %s, length is %lu\n" ), (LPCTSTR) name, size_of_buffer );
   size_of_buffer = 4096;
   enumerator++;
}
BOOL Flush( void )
Writes all cached changes to disk. It returns TRUE on success or FALSE on failure. The reason for the failure can be retrieved via GetErrorCode().
BOOL GetBinaryValue( LPCTSTR name_of_value, CByteArray& bytes )
Retrieves the binary data and puts it into bytes. It returns TRUE on success or FALSE on failure. The reason for the failure can be retrieved via GetErrorCode().
void GetClassName( CString& class_name ) const
Retrieves the name of the class of key you have opened.
void GetComputerName( CString& computer_name ) const
Retrieves the name of the computer where the hive is located.
BOOL GetDoubleWordValue( LPCTSTR name_of_value, DWORD& value )
Retrieves the DWORD data value. It returns TRUE on success or FALSE on failure. The reason for the failure can be retrieved via GetErrorCode().
DWORD GetErrorCode( void ) const
Retrieves the error code. Call this function if any other class method returns FALSE.
void GetKeyName( CString& key_name ) const
Retrieves the name of the current key.
DWORD GetNumberOfSubKeys( void ) const
Retrieves the number of keys contained within the current key.
DWORD GetNumberOfValues( void ) const
Retrieves the number of values contained within the current key.
void GetRegistryName( CString& registry_name ) const
Retrieves the name of the registry you are playing with.
BOOL GetSecurity( const SECURITY_INFORMATION what_you_want_to_know,
                  PSECURITY_DESCRIPTOR       data_buffer,
                  DWORD&                     size_of_data_buffer )
Retrieves security information. It returns TRUE on success or FALSE on failure. The reason for the failure can be retrieved via GetErrorCode().
BOOL GetStringValue( LPCTSTR name_of_value, CString& return_string )
Retrieves a string value (REG_SZ). It returns TRUE on success or FALSE on failure. The reason for the failure can be retrieved via GetErrorCode().
BOOL GetStringArrayValue( LPCTSTR name_of_value, CStringArray& return_array )
Retrieves a value containing multiple strings (REG_MULTI_SZ) and puts it into a CStringArray. It returns TRUE on success or FALSE on failure. The reason for the failure can be retrieved via GetErrorCode().
BOOL GetValue( LPCTSTR name_of_value, CByteArray& return_array )
BOOL GetValue( LPCTSTR name_of_value, DWORD& return_value )
BOOL GetValue( LPCTSTR name_of_value, CString& return_string )
BOOL GetValue( LPCTSTR name_of_value, CStringArray& return_array )
Overloaded function that calls the proper GetXValue function. It returns TRUE on success or FALSE on failure. The reason for the failure can be retrieved via GetErrorCode().
BOOL Load( LPCTSTR name_of_subkey, LPCTSTR name_of_file_containg_information )
Loads a subkey from a file. It returns TRUE on success or FALSE on failure. The reason for the failure can be retrieved via GetErrorCode().
BOOL NotifyChange( const HANDLE event_handle                       = NULL,
                   const NotifyChangeFilter changes_to_be_reported = notifyLastSet,
                   const BOOL this_subkey_or_all_subkeys           = changeSpecifiedKeyOnly,
                   const BOOL wait_for_change_or_signal_event      = WAIT_FOR_CHANGE )
Lets you watch a key (with or without subkeys) to see if anything changes. It returns TRUE on success or FALSE on failure. The reason for the failure can be retrieved via GetErrorCode(). changes_to_be_reported can be one of:
  • notifyName
  • notifyAttributes
  • notifyLastSet
  • notifySecurity
BOOL Open( LPCTSTR name_of_subkey_to_open,
           const CreatePermissions security_access_mask = permissionAllAccess )
Opens a subkey. It returns TRUE on success or FALSE on failure. The reason for the failure can be retrieved via GetErrorCode(). security_access_mask can be a combination of:
  • permissionAllAccess
  • permissionCreateLink
  • permissionCreateSubKey
  • permissionEnumerateSubKeys
  • permissionExecute
  • permissionNotify
  • permissionQueryValue
  • permissionRead
  • permissionSetValue
  • permissionWrite
BOOL QueryInfo( void )
Tells CRegistry to load information because you're about to call QueryValue(). This loads information that can be retrieved via GetNumberOfSubkeys() or GetNumberOfValues(). It returns TRUE on success or FALSE on failure. The reason for the failure can be retrieved via GetErrorCode().
BOOL QueryValue( LPCTSTR        name_of_value,
                 KeyValueTypes& value_type,
                 LPBYTE         address_of_buffer,
                 DWORD&         size_of_buffer )
Allows you to retrieve a value without knowing it's type. It returns TRUE on success or FALSE on failure. The reason for the failure can be retrieved via GetErrorCode(). value_type will be filled with one of the following values:
  • typeBinary
  • typeDoubleWord
  • typeDoubleWordLittleEndian
  • typeDoubleWordBigEndian
  • typeUnexpandedString
  • typeSymbolicLink
  • typeMultipleString
  • typeNone
  • typeResourceList
  • typeString
BOOL Replace( LPCTSTR name_of_subkey,
              LPCTSTR name_of_file_with_new_data,
              LPCTSTR name_of_backup_file )
Allows you to replace a subkey with data from a file. It returns TRUE on success or FALSE on failure. The reason for the failure can be retrieved via GetErrorCode().
BOOL Restore( LPCTSTR name_of_file_holding_saved_tree, const DWORD volitility_flags = NULL )
Restores a subkey that was backed up to a file. It returns TRUE on success or FALSE on failure. The reason for the failure can be retrieved via GetErrorCode().
BOOL Save( LPCTSTR name_of_file_to_hold_tree, LPSECURITY_ATTRIBUTES security_attributes_p = NULL )
Allows you to save a subtree to a file (so you can call Restore()). It returns TRUE on success or FALSE on failure. The reason for the failure can be retrieved via GetErrorCode().
BOOL SetBinaryValue( LPCTSTR name_of_value, const CByteArray& bytes_to_write )
Allows you to write a binary (raw bytes) to a value in a key. It returns TRUE on success or FALSE on failure. The reason for the failure can be retrieved via GetErrorCode().
BOOL SetDoubleWordValue( LPCTSTR name_of_value, DWORD value_to_write )
Allows you to write a DWORD value to a key. It returns TRUE on success or FALSE on failure. The reason for the failure can be retrieved via GetErrorCode().
BOOL SetSecurity( const SECURITY_INFORMATION& security_information,
                  const PSECURITY_DESCRIPTOR security_descriptor_p )
Allows you to put security onto a subkey. It returns TRUE on success or FALSE on failure. The reason for the failure can be retrieved via GetErrorCode().
BOOL SetStringValue( LPCTSTR name_of_value, const CString& string_value )
Allows you to write a string value to a key. It returns TRUE on success or FALSE on failure. The reason for the failure can be retrieved via GetErrorCode().
BOOL SetStringArrayValue( LPCTSTR name_of_value, const CStringArray& string_array )
Allows you to write a bunch of strings to a value in a key. It returns TRUE on success or FALSE on failure. The reason for the failure can be retrieved via GetErrorCode().
BOOL SetValue( LPCTSTR name_of_value, const CByteArray& bytes_to_write );
BOOL SetValue( LPCTSTR name_of_value, DWORD value );
BOOL SetValue( LPCTSTR name_of_value, const CStringArray& strings_to_write );
BOOL SetValue( LPCTSTR name_of_value, const CString& string_to_write );
BOOL SetValue( LPCTSTR             name_of_subkey,
               const KeyValueTypes type_of_value_to_set,
               CONST PBYTE         address_of_value_data,
               const DWORD         size_of_data )
Overloaded method that figures out what type of value you want to set. It returns TRUE on success or FALSE on failure. The reason for the failure can be retrieved via GetErrorCode().
BOOL UnLoad( LPCTSTR name_of_subkey_to_unload )
Unloads a previously Load()'ed key. It returns TRUE on success or FALSE on failure. The reason for the failure can be retrieved via GetErrorCode().

Example

#include <wfc.h>
#pragma hdrstop

void test_CRegistry( void )
{
   WFCTRACEINIT( TEXT( "test_CRegistry()" ) );

   CRegistry registry;

   if ( registry.Connect( CRegistry::keyLocalMachine ) == FALSE )
   {
      TRACE( TEXT( "Can't connect to registry\n" ) );
      return;
   }

   if ( registry.Open( TEXT( "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application" ),
                      (CRegistry::CreatePermissions)(CRegistry::permissionRead | CRegistry::permissionSetValue)
                     ) == FALSE )
   {
      TRACE( TEXT( "Can't open key\n" ) );
      
      ReportError( registry.GetErrorCode() );
      return;
   }

   CStringArray string_array;

   string_array.RemoveAll();

   if ( registry.GetStringArrayValue( TEXT( "Sources" ), string_array ) != FALSE )
   {
      int index = 0;
      int number_of_strings = string_array.GetSize();

      TRACE1( TEXT( "There are %d strings\n" ), number_of_strings );

      CString temp_string;

      while( index < number_of_strings )
      {
         temp_string = string_array[ index ];

         TRACE2( TEXT( "%d - \"%s\"\n" ), index + 1, (LPCTSTR) temp_string );
         index++;
      }

      if ( registry.SetStringArrayValue( TEXT( "TempSources" ), string_array ) == FALSE )
      {
         ReportError( registry.GetErrorCode() );
      }
   }

   if ( registry.SetValue( TEXT( "QFile" ), "Sammy" ) == FALSE )
   {
      TRACE( TEXT( "registry.SetValue failed\n" ) );
      ReportError( registry.GetErrorCode() );
   }
   else
   {
      TRACE( TEXT( "Set QFile to Sammy\n" ) );
   }

   CString temp_sammy( TEXT( "" ) );

   if ( registry.GetValue( TEXT( "QFile" ), temp_sammy ) == FALSE )
   {
      TRACE( TEXT( "registry.GetValue failed\n" ) );
      ReportError( registry.GetErrorCode() );
   }
   else
   {
      if ( temp_sammy.Compare( TEXT( "Sammy" ) ) == 0 )
      {
         TRACE( TEXT( "GetValue OK\n" ) );
      }
      else
      {
         TRACE1( TEXT( "FAIL! QFile is \"%s\"\n" ), (LPCTSTR) temp_sammy );
      }
   }
}

API's Used

CRegistry encapsulates the following API's:
Copyright, 2000, Samuel R. Blackburn
$Workfile: registry.cpp $
$Modtime: 1/17/00 9:28a $