Registry Operations

   Registry is organized as a hierarchical tree. The Registry keys are folder like entries and the keys can contain string values ( REG_SZ), dword (REG_DWORD) values etc under them. This article discusses, how to Create Registry Keys, String entries, DWORD entries and set their values.

Registry Key Creation using RegCreateKey:

   The Registry keys can be created under any other keys except at the root level. One more point to be noted is applications cannot create entries directly under HKEY_USERS and HKEY_LOCAL_MACHINE. The sample code for creating a registry key is as follows.

       HKEY hKey;
       DWORD dwDisp = 0;
       LPDWORD lpdwDisp = &dwDisp;
       CString l_strExampleKey = "SOFTWARE\\TestKey";

       LONG iSuccess = RegCreateKeyEx( HKEY_CURRENT_USER, l_strExampleKey, 0L,NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey,lpdwDisp);

       if(iSuccess == ERROR_SUCCESS)
       {
            RegCloseKey(hKey);
       }

   The above piece of code will create an entry under HKEY_CURRENT_USER\Software\TestKey.

Registry Values - Creating and Modifying values:

   There are two types of values which might need to be modified. One is creating and setting the value for a REG_DWORD entry and the next is creating and setting the value of REG_SZ entry.

Registry Values - REG_DWORD entries using RegSetValueEx:

   This is achieved by using the function RegSetValueEx. This function takes parameters of the Handle to the Registry Key, DWORD Variable name, DWORD value etc., A sample implementation for RegSetValueEx is as follows.

       HKEY hKey;
       DWORD dwDisp = 0;
       LPDWORD lpdwDisp = &dwDisp;
       CString l_strExampleKey = "SOFTWARE\\Microsoft\\Exchange\\MSExchangeAdminCommon";
       CString l_strDWordSample = "DWORDSample";
       DWORD dwVal = 100;

       LONG iSuccess = RegCreateKeyEx( HKEY_CURRENT_USER, l_strExampleKey, 0L,NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey,lpdwDisp);

       if(iSuccess == ERROR_SUCCESS)
       {
            RegSetValueEx (hKey, l_strDWordSample, 0L, REG_DWORD,(CONST BYTE*) &dwVal, sizeof(DWORD));
       }

   Now the REG_DWORD variable "SOFTWARE\\Microsoft\\Exchange\\MSExchangeAdminCommon\\ DWORDSample" will be set with value of 100.

   In the examples of this article, we have created a new key and used it. If the key is already available, we can use RegKeyOpenEx to get the HKEY handle. This handle can be used for RegSetValueEx.

Registry Values - REG_SZ entries using RegSetValueEx:

   The following is the sample code for setting a REG_SZ entry. We use the same key entry for this also.

       HKEY hKey;
       DWORD dwDisp = 0;
       LPDWORD lpdwDisp = &dwDisp;
       CString l_strExampleKey = "SOFTWARE\\Microsoft\\Exchange\\MSExchangeAdminCommon";
       CString l_strStringSample = "StringSample", l_strStringSampleVal = "String Value";;

       LONG iSuccess = RegCreateKeyEx( HKEY_CURRENT_USER, l_strExampleKey, 0L,NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey,lpdwDisp);

       if(iSuccess == ERROR_SUCCESS)
       {
            RegSetValueEx(hKey, l_strStringSample, 0, REG_SZ, (LPBYTE) l_strStringSampleVal.GetBuffer(l_strStringSampleVal.GetLength()), l_strStringSampleVal.GetLength()+ 1);
       }

Registry Key - Important:

   These functions are declared in Winreg.h. Windows.h to be included for accessing these functions. The library to be used is Advapi32.lib.
 

Close    To Top
  • Prev Article-Programming:
  • Next Article-Programming:
  • Now: Tutorial for Web and Software Design > Programming > win32 > Programming Content
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Geek Tutorial
     

    Blogging Tutorial

      RSS Tutorial
      Podcasting Tutorial
    Graphic Design Tutorial
      Coreldraw Tutorial
      Illustrator Tutorial
      3D Tutorials
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial/ Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial/ Articles
     

    XML Style

      AJAX Tutorial
      XML Mobile
    Flash Tutorial/ Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial/ Articles
      Linux Tutorial
      Symbian Tutorial
      MacOS Tutorial
    Personal Tech
      Hardware Tutorial
      Software Tutorial
      Online Auction