Objective Toolkit : Chapter 16 Utility Classes : Win32 Registry Access
Win32 Registry Access
Objective Toolkit provides an encapsulation of the Win32 registry APIs. Using Objective Toolkit’s registry abstraction, you can modify the registry through a common interface consistent under all supported platforms.
SECRegistry encapsulates the APIs used to access and modify the Windows registry in a single class. The bulk of the SECRegistry methods are wrappers for the Win32 API. One advantage of using the class instead of the Win32 API is that you can recursively delete keys in a 32-bit environment.
The SECRegistry Class
The SECRegistry class encapsulates the Win32 registry API. An SECRegistry instance contains a member handle to a registry key. This same key is then used as a parameter to some of the Windows registry APIs (SECRegistry shields the user from having to keep track of the handle).
Figure 139 – Objective Toolkit’s Registry Class Hierarchy
Using SECRegistry
The following terms are used in the following descriptions:
Keys. Nodes that can contain one or more keys or values. A key is like a folder that can contain other folders (keys) or items (values).
Values. An item that is contained by a key that has two attributes: a name and an associated evaluation. For example:
Name
Value
“Path”
C:\Program Files\WordView\WordView.exe
“Password”
GX325PF
Version
5.1
The following section show you how to use SECRegistry to modify registry settings.
To open the registry
Call the Connect() method. If a computer name is specified, the application attempts to open the registry remotely. There must be sufficient permissions for remote registry connections to work. For example:
 
// connect to the local registry
m_registry.Connect(HKEY_CURRENT_USER);
 
// connect to a remote registry
m_registry.Connect(HKEY_LOCAL_MACHINE,
_T(“\\\\TARZAN”));
To open a subkey
1. Connect() to the registry.
 
m_registry.Connect(HKEY_CURRENT_USER);
2. Open the subkey with the Open() method.
 
m_registry.Open(_T(“Software”));
To enumerate registry keys
1. Connect() to the registry.
 
m_registry.Connect(HKEY_CURRENT_USER);
2. You can also open a subkey.
 
m_registry.Open(_T(“Environment”));
3. Call the EnumerateKeys() method.
 
// Enumerate all the subkeys
// and add them to the listbox
DWORD dwCount = 0;
CString strKey;
CString strClass;
while (m_registry.EnumerateKeys(dwCount++, strKey, strClass))
pListBox->InsertString(-1, (LPCTSTR)strKey);
To enumerate values within a key
1. Connect() to the registry.
 
m_registry.Connect(HKEY_CURRENT_USER);
2. You can also open a subkey.
 
m_registry.Open(_T(“Environment”));
3. Call the EnumerateValues() method.
 
DWORD dwCount = 0;
Cstring strName;
SECRegistry::KeyValueTypes typeCode;
while (m_registry.EnumerateValues(dwCount++, strName, typeCode))
pListBox->InsertString(-1, (LPCTSTR)strName);
To read a value
1. Connect() to the registry.
 
m_registry.Connect(HKEY_CURRENT_USER);
2. You can also open a subkey.
 
m_registry.Open(_T(“Environment”));
3. Call one of the following methods to retrieve the value. These methods assume you know the type of the value. See “To enumerate values within a key”.
 
GetBinaryValue()
GetDoubleWordValue()
GetStringArrayValue()
GetStringValue()
4. You can also call one of the overloaded GetValue() methods or the QueryValue() method.
To create a key
1. Connect() to the registry.
 
m_registry.Connect(HKEY_CURRENT_USER);
2. You can also open a subkey.
 
m_registry.Open(_T(“Software”));
3. Call the Create() method to create the new key.
 
m_registry.Create(_T(“MyKey”));
To delete a key
1. Connect() to the registry and open the parent of the key about to be deleted.
 
m_registry.Connect(HKEY_CURRENT_USER);
m_registry.Open(_T(“Software”));
2. Call the DeleteKey() method to delete a particular subkey.
 
m_registry.DeleteKey(_T(“MyKey”));
NOTE >> DeleteKey() fails if the key being deleted contains subkeys. To delete subkeys, you need to specify a recursive delete operation. See “To recursively delete keys”.
To recursively delete keys
1. Connect() to the registry, and open the parent of the key about to be deleted.
 
m_registry.Connect(HKEY_CURRENT_USER);
m_registry.Open(_T(“Software”));
2. Call the DeleteKey() method to delete a particular subkey and specify a recursive delete.
 
m_registry.DeleteKey(_T(“MyKey”), TRUE); // TRUE = recursive delete
NOTE >> Be careful when recursively deleting registry keys!
To close a registry connection
Call the Close() method.
 
m_registry.Close();
NOTE >> If this method is not called explicitly, it is called implicitly by the SECRegistry destructor.
SECRegistry Sample
The Objective Toolkit regdemo sample (Samples\Toolkit\MFC\Utility\regdemo) illustrates how to use SECRegistry in a 32-bit environment. This sample does not ship with the product. For information on how to obtain this sample, see “Location of Sample Code” in the Getting Started part.