Click here to Skip to main content
15,893,668 members
Articles / Programming Languages / C++

Class to handle the Windows Registry operations

Rate me:
Please Sign up or sign in to vote.
1.00/5 (14 votes)
27 Nov 20072 min read 25.7K   446   15  
A class to handle the Windows registry more smoothly.
/**
 *
 * CRegistry.cpp - To do the registry operations
 *
 * @author :    Ajayraj
 * @version:    1.0            Date:  2007-02-08
 */
#include "StdAfx.h"
#include "CRegistry.h"





/** 
 * 
 * Constructor
 * 
 * @param       Nil
 * @return      Nil
 * @exception   Nil
 * @see         Nil
 * @since       1.0
 */
CRegistry::CRegistry()
{
    
}



/** 
 * 
 * To create the registry node with a default setting.
 * 
 * @param       hKey        - Handle to a currently open key or one of the following predefined reserved handle values:  eg, HKEY_CLASSES_ROOT 
 * @param       csRootKey_i - Node to be created
 * @param       hReg        - Output, handle to the opened or created key.
 * @param       dwDisp      - ouputs whether its a new key or existing eg:- REG_CREATED_NEW_KEY
 * @return      bool        - true on success
 * @exception   Nil
 * @see         Nil
 * @since       1.0
 */
bool CRegistry::CreateRegistryKey(HKEY hKey, const CString& csRootKey_i, HKEY hReg, DWORD dwDisp)
{
    try
    {
    
        if( ERROR_SUCCESS  != RegCreateKeyEx( hKey, TEXT(csRootKey_i), 0, NULL, REG_OPTION_NON_VOLATILE,
					                          KEY_ALL_ACCESS, NULL, (PHKEY)&hReg,  &dwDisp))
        {
            LPTSTR lptszErrorMessage;
            FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
                              NULL, GetLastError(),0, (LPTSTR) &lptszErrorMessage,0, NULL );
            return false;
        }
        return true;
    }
    catch ( ... ) 
    {
        return false;
    }
}


/** 
 * 
 * To create the registry node with your own security as well as other settings
 * 
 * @param       All paramter are same as given in MSDN for RegCreateKeyEx
 * @return      bool - 
 * @exception   Nil
 * @see         Nil
 * @since       1.0
 */
bool CRegistry::CreateRegistryKey( HKEY hKey, const CString& csRootKey_i, DWORD dwReserved_i, 
                                   LPSTR lpClassName_i,REGSAM samDesired_i, LPSECURITY_ATTRIBUTES lpSecurityAttributes,
                                   DWORD dwOption_i,PHKEY hReg_o, DWORD dwDisp_o )
{

    try
    {
    

	  if( ERROR_SUCCESS  != RegCreateKeyEx( hKey, TEXT(csRootKey_i), dwReserved_i,lpClassName_i,dwOption_i,
					    samDesired_i, lpSecurityAttributes, hReg_o,  &dwDisp_o ))
      {
           LPTSTR lptszErrorMessage;
           FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
                          NULL, GetLastError(),0, (LPTSTR) &lptszErrorMessage,0,NULL );
           return false;
           
      }
      return true;
    }
    catch ( ... ) 
    {
        return false;
    }
}



/** 
 * 
 * To create an key and set the value for that key is possible with this API.
 * 
 * @param       hkey - Handle to a currently open key or one of the following predefined reserved handle values:  eg, HKEY_CLASSES_ROOT  
 * @param       csRootKey_i - Main node i.e say //Software//microsoft/DevStudio
 * @param       csChildkey_i - The Root keys lastly specified nodes Attributes and values.eg:- say DevStdio, size and Name. 
 * @param       csChildKeyValue_i - THe value of the Child .
 * @return      bool - true on success
 * @exception   Nil
 * @see         Nil
 * @since       1.0
 */
bool CRegistry::CreateAndSetValue( HKEY hkey, const CString& csRootKey_i, 
							       const CString& csChildkey_i, const CString& csChildKeyValue_i )
{
    
    try
    {
    
        HKEY hReg;
	    DWORD dwDisp;
  
	    if( ERROR_SUCCESS  != RegCreateKeyEx( hkey, TEXT(csRootKey_i), 0, NULL, REG_OPTION_NON_VOLATILE,
					    KEY_ALL_ACCESS, NULL, (PHKEY)&hReg,  &dwDisp))
        {
            LPTSTR lptszErrorMessage;
            FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
                          NULL, GetLastError(),0, (LPTSTR) &lptszErrorMessage,0,NULL );
            return false;
        }
	    HKEY hOpenedKey = NULL;
	    LONG nResult = RegOpenKeyEx(hkey, csRootKey_i, 0, KEY_WRITE, &hOpenedKey);
	    if ( nResult != ERROR_SUCCESS )
	    return false;
	    // Default value insert
	    char szValue[150];
	    strcpy( szValue,csChildKeyValue_i );
	    CString csSubKey="";
	    if( ERROR_SUCCESS  != RegSetValueEx(hReg, TEXT(csChildkey_i), 0, REG_SZ, 
				                            (BYTE* const)&szValue, strlen(szValue)+1))
        {
            LPTSTR lptszErrorMessage;
            FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
                          NULL, GetLastError(),0, (LPTSTR) &lptszErrorMessage,0,NULL );
            return false;
        }
        RegCloseKey(hOpenedKey);
        return true;
    }
    catch ( ... ) 
    {
        return true;
    }
}


/** 
 * 
 * To create an key and set the value for that key is possible with this API.
 * 
 * @param       hkey - Handle to a currently open key or one of the following predefined reserved handle values:  eg, HKEY_CLASSES_ROOT  
 * @param       csRootKey_i - Main node i.e say //Software//microsoft/DevStudio
 * @param       csChildkey_i - The Root keys lastly specified nodes Attributes and values.eg:- say DevStdio, size and Name. 
 * @param       csChildKeyValue_i - THe value of the Child
 * @param       dwChildKeySize - Size of the child key value
 * @param       dwChildType    - Type say REG_SZ..
 * @return      bool -  true on success
 * @exception   Nil
 * @see         Nil
 * @since       1.0
 */
bool CRegistry::CreateAndSetValue( HKEY hkey, const CString& csRootKey_i, const CString& csChildkey_i, 
                                   const BYTE* lpChildKeyValue, DWORD dwChildKeySize, DWORD dwChildType )
{

    try
    {
    
        HKEY hReg;
	    DWORD dwDisp;
  
	    if( ERROR_SUCCESS  != RegCreateKeyEx( hkey, TEXT(csRootKey_i), 0, NULL, REG_OPTION_NON_VOLATILE,
					    KEY_ALL_ACCESS, NULL, (PHKEY)&hReg,  &dwDisp))
        {
            LPTSTR lptszErrorMessage;
            FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
                          NULL, GetLastError(),0, (LPTSTR) &lptszErrorMessage,0,NULL );
            return false;
        }
	    HKEY hOpenedKey = NULL;
	    LONG nResult = RegOpenKeyEx(hkey, csRootKey_i, 0, KEY_WRITE, &hOpenedKey);
	    if ( nResult != ERROR_SUCCESS )
	    return false;
	    // Default value insert
	   
	    if( ERROR_SUCCESS  != RegSetValueEx( hReg, TEXT(csChildkey_i), 0, dwChildType, 
				                             lpChildKeyValue,dwChildKeySize ))
        {
            LPTSTR lptszErrorMessage;
            FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
                          NULL, GetLastError(),0, (LPTSTR) &lptszErrorMessage,0,NULL );
            return false;
        }
        RegCloseKey(hOpenedKey);
        return true;
    }
    catch ( ... ) 
    {
        return true;
    }
}



/** 
 * 
 * Delete the key
 * 
 * @param       hkey    - Handle to a currently open key or one of the following predefined reserved handle values:  eg, HKEY_CLASSES_ROOT  
 * @param       csKey_i - Main node i.e say //Software//microsoft/DevStudio, it will delete if it have the previlege.
 * @return      bool    - 
 * @exception   Nil
 * @see         Nil
 * @since       1.0
 */
bool CRegistry::DeleteKey( HKEY hkey, const CString& csKey_i )
{
    try
    {
      if( ERROR_SUCCESS  != RegDeleteKey( hkey, csKey_i ))
      { 
          LPTSTR lptszErrorMessage;
          FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
                         NULL, GetLastError(),0, (LPTSTR) &lptszErrorMessage,0,NULL );
          return false;

      }
      return true;
    }
    catch ( ... ) 
    {
        return false;
    }
}


/** 
 * 
 * destructor
 * 
 * @param       Nil
 * @return      Nil
 * @exception   Nil
 * @see         Nil
 * @since       1.0
 */
CRegistry::~CRegistry()
{

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer
India India
If there is no way, make a way.
Believe me, you can do that.

In the software history, you can never find a software without bug, It is made the best after continuos attempts.

Comments and Discussions