Registry Operations

CoderSource.net
Registry Operations
Rating:

Date: 5/1/2004 12:00:00 AM

   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.
 

You Can Rate this Article, if you are Logged In      
 

More Links from CoderSource.net:

 
Refer to a Friend:

Your Details:

Name:     e-mail:

Friend Details:

Name:    e-mail:    


MENU
Home
MFC 
C++
.Net
WIN32
Programming
Forum
My Articles
Add to Google
Add to My Yahoo!
Welcome to Codersource.Net Login | Register | Faq  

SEARCH
Google
 

NOTES:


Thanks for visiting our CoderSource.net. This site will be improved with more articles. Interested visitors can also submit their articles through the Submit Article link.Your article will also be published after due consideration by the editor. 

© Copyright 2003. All rights on content reserved by CoderSource.net. Contact    About Us