Windows Registry I/O

Visual Basic | Delphi | C++ Builder | Java | Windows API

Visual Basic 6.0

The following is based on information in the VB 6.0 help under registry, functions for manipulating settings

The following commands Read, Write, and Delete registry information stored under

     HKEY_CURRENT_USER\Software\
        VB and VBA Program Settings\appname\section\key

The following example is from VB 6.0 help on SaveSetting

Basically, this code is worthless. There is no way to specify actual registry entries. You can only read and write values in the special VB Applications section of the registry. (Notice the Pascal/Delphi assignment syntax (var := "str") when named parameters are used.)

If you want the freedom to access any key in the registry, then you must use the Windows API calls.


Delphi

In order to use TRegistry, add Registry to the Uses clause.
from Windows.pas because they are not in the help

const
{ Reserved Key Handles. }

  HKEY_CLASSES_ROOT     = $80000000;
  HKEY_CURRENT_USER     = $80000001;
  HKEY_LOCAL_MACHINE    = $80000002;
  HKEY_USERS            = $80000003;
  HKEY_PERFORMANCE_DATA = $80000004;
  HKEY_CURRENT_CONFIG   = $80000005;
  HKEY_DYN_DATA         = $80000006;


from Registry.pas

constructor TRegistry.Create;
begin
  RootKey := HKEY_CURRENT_USER;
  LazyWrite := True;
end;

My Examples

    // How to test for HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft
    // The following test returns true
var
  a: string;
  b:boolean;
  reg1: TRegistry;
begin
  a:='SOFTWARE\Microsoft';
  reg1 := TRegistry.create;  
  reg1.RootKey := HKEY_LOCAL_MACHINE;
  b:=reg1.KeyExists(a);
  if b then
   UIEdit.text:='True'
  else
   UIEdit.text:='False';
  reg1.free;
end;

The following code produces an error when the application closes.
  reg1.create;
Use
  reg1 := TRegistry.create;  
     ......
  reg1.free;
instead.
var
  reg: TRegistry;
begin
  reg := TRegistry.create;
  try
     // Your code here
  finally
    reg.free;
  end;
end;
In order to access the (Default) value, use a null string ('').
  reg1.RootKey := HKEY_CLASSES_ROOT;
  if reg1.openKey('.txt', false) then
    begin
      reg1.WriteString('', 'your string'); // null means "default"
      reg1.closeKey;
    end;
An at-sign (@) is used in the .reg files to refer to the default value. However, in Delphi, the following creates a string value called '@'
      reg1.WriteString('@', 'your string');

The reg1.GetValueNames method returns the names of the available parameters.


ZiffDavis Example

The following is from a ZiffDavis article (Delphi Developers Journal - Jan 98)

There appears to be a coding error - MyRegistry is changed to MRURegistry.

MyRegistry := TRegistry.Create;  

  ' full key = HKEY_CURRENT_USER\Software\MyApps\Settings
MyRegistry.OpenKey('\Software\Myapps\Settings', True);
MRURegistry.WriteString('LastFile','myfile.txt');
MRURegistry.WriteString('LastDirectory','C:\');
MRURegistry.WriteString('Data',1);
MyRegistry.Free;

*****

var
  FileName: String;

MyRegistry := TRegistry.Create;
MyRegistry.OpenKey('\Software\Myapps\Settings', True);
FileName := MyRegistry.ReadString("LastFile");
MyRegistry.Free;
Because the TRegistry destructor closes the current key, there is no reason to call CloseKey explicitly.

*****

ZDNet provides several utilities written by Neil J. Rubenking which include Delphi source code and text descriptions of the design technique.


C++ Builder

From Mircosoft help, search for RegOpenKey and click on Registry Functions (at bottom of screen). Simply searching for Registry Functions produces a completely different and unrelated page.
LONG RegOpenKey( HKEY hKey,        // handle to open key
                 LPCTSTR lpSubKey, // address of name of subkey to open
                 PHKEY phkResult   // address of handle to open key);

Java

The RegKey class in the com.ms.lang package provides the necessary member functions.

Windows API

The registry functions are implemented via advapi32.dll. The following is from Win32API.txt (edited to make it somewhat readable).
' -----------------
' ADVAPI32
' -----------------

' function prototypes, constants, and type definitions
' for Windows 32-bit Registry API

Const HKEY_CLASSES_ROOT     = &H80000000
Const HKEY_CURRENT_USER     = &H80000001
Const HKEY_LOCAL_MACHINE    = &H80000002
Const HKEY_USERS            = &H80000003
Const HKEY_PERFORMANCE_DATA = &H80000004   // Windows NT
Const HKEY_CURRENT_CONFIG   = &H80000005
Const HKEY_DYN_DATA         = &H80000006   // Windows 95 & 98

' Registry API prototypes

RegCloseKey        (ByVal hKey As Long) As Long
RegConnectRegistry (ByVal lpMachineName As String, ByVal hKey As Long, phkResult As Long) As Long
RegCreateKey       (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
RegCreateKeyEx     (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES, phkResult As Long, lpdwDisposition As Long) As Long
RegDeleteKey       (ByVal hKey As Long, ByVal lpSubKey As String) As Long
RegDeleteValue     (ByVal hKey As Long, ByVal lpValueName As String) As Long
RegEnumKey         (ByVal hKey As Long, ByVal dwIndex  As Long, ByVal lpName As String, ByVal cbName As Long) As Long
RegEnumKeyEx       (ByVal hKey As Long, ByVal dwIndex  As Long, ByVal lpName As String, lpcbName As Long, ByVal lpReserved As Long, ByVal lpClass As String, lpcbClass As Long, lpftLastWriteTime As FILETIME) As Long
RegEnumValue       (ByVal hKey As Long, ByVal dwIndex  As Long, ByVal lpValueName As String, lpcbValueName As Long, ByVal lpReserved As Long, lpType As Long, lpData As Byte, lpcbData As Long) As Long
RegFlushKey        (ByVal hKey As Long) As Long
RegGetKeySecurity  (ByVal hKey As Long, ByVal SecurityInformation As Long, pSecurityDescriptor As SECURITY_DESCRIPTOR, lpcbSecurityDescriptor As Long) As Long
RegLoadKey         (ByVal hKey As Long, ByVal lpSubKey As String, ByVal lpFile As String) As Long
RegNotifyChangeKeyValue
                   (ByVal hKey As Long, ByVal bWatchSubtree As Long, ByVal dwNotifyFilter As Long, ByVal hEvent As Long, ByVal fAsynchronus As Long) As Long
RegOpenKey         (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
RegOpenKeyEx       (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
RegQueryInfoKey    (ByVal hKey As Long, ByVal lpClass  As String, lpcbClass As Long, ByVal lpReserved As Long, lpcSubKeys As Long, lpcbMaxSubKeyLen As Long, lpcbMaxClassLen As Long, lpcValues As Long, lpcbMaxValueNameLen As Long, lpcbMaxValueLen As Long, lpcbSecurityDescriptor As Long, lpftLastWriteTime As FILETIME) As Long
RegQueryValue      (ByVal hKey As Long, ByVal lpSubKey As String, ByVal lpValue As String, lpcbValue As Long) As Long
RegQueryValueEx    (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long         ' Note that if you declare the lpData parameter as String, you must pass it By Value. 
RegReplaceKey      (ByVal hKey As Long, ByVal lpSubKey As String, ByVal lpNewFile As String, ByVal lpOldFile As String) As Long
RegRestoreKey      (ByVal hKey As Long, ByVal lpFile   As String, ByVal dwFlags As Long) As Long
RegSaveKey         (ByVal hKey As Long, ByVal lpFile   As String, lpSecurityAttributes As SECURITY_ATTRIBUTES) As Long
RegSetKeySecurity  (ByVal hKey As Long, ByVal SecInfo  As   Long, pSecurityDescriptor  As SECURITY_DESCRIPTOR) As Long
RegSetValue        (ByVal hKey As Long, ByVal lpSubKey As String, ByVal dwType As Long, ByVal lpData As String, ByVal cbData As Long) As Long
RegSetValueEx      (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long         ' Note that if you declare the lpData parameter as String, you must pass it By Value.
RegUnLoadKey       (ByVal hKey As Long, ByVal lpSubKey As String) As Long

Author: Robert Clemenzi - clemenzi@cpcug.org
URL: http:// cpcug.org / user / clemenzi / technical / Languages / RegistryIO.htm