Using Registry

Several examples below shows how to use registry from your Delphi programs. While reading and writing simple data can be obvious, some other tasks can be difficult, especially for beginners. Here are some examples, which may help.
don't forget to put REGISTRY into your USES section.

Reading Binary Data

The example below shows how to read binary data, which is usially shown in RegEdit as 20 00 00 00 from the registry. Later, you'll see how to write such data back to registry.
 procedure TForm1.Button1Click(Sender: TObject);
 const
  Key  : String = '\Enum\Network\Family\0000\';
  Val  : String = 'Capabilities';
 var
  Data : Array of Byte;	// Dynamic Array - Delphi4 Only !!
  S    : String;
  sz   : Word;
  I    : Word;
 begin
  R := TRegistry.Create;
  with R do
   begin
 //
 // Set root key
 //
    RootKey := HKEY_LOCAL_MACHINE;
 //
 // Open Subkey
 //
    OpenKey(Key, False);
 //
 // If this is binary data
 //
    if GetDataType(Val) = rdBinary
     then
      begin
 //
 // Get the size
 //
       sz := GetDataSize(Val);
       if sz > 0 then
        begin
 //
 // Set the size of the dynamic array
 //
         SetLength(Data, sz);
 //
 // Read data into the array
 //
         ReadBinaryData(Val, Data[0], sz);
         S := Val + ' = ';
         for I := 0 to sz - 1 do
          begin
 //
 // Format 'em
 //
           S := S + Format('%2x',[Data[I]]);
          end;
 //
 // Show
 //
         Edit1.Text := S;
        end;
      end;
    Free;
   end;
 end;
 

Reading Entire Section

This example shows how to read the entire section without previous knowledge of the number of elements in it and its data types.
 procedure TForm1.Button1Click(Sender: TObject);
 const
  Key  : String = '\Enum\Network\Family\0000\';
 var
  Data : Array of Byte;
  L    : TStringList;
  S    : String;
  sz   : Word;
  I,J  : Word;
 begin
  R := TRegistry.Create;
  L := TStringList.Create;
  with R do
   begin
//
// Set root key
//
    RootKey := HKEY_LOCAL_MACHINE;
//
// Open subkey
//
    OpenKey(Key, False);
//
// Get the list of keys
//
    GetValueNames(L);
    If L.Count > 0 Then
     begin
      for I := 0 to L.Count - 1 do
       begin
//
// Get the data of the keys
//
        case GetDataType(L[I]) of
// String?
         rdString,
         rdExpandString :
           S := '"' + ReadString(L[I]) + '"';
// Integer?
         rdInteger :
           S := IntToStr(ReadInteger(L[I]));
// Binary?
         rdBinary :
          begin
           sz := GetDataSize(L[I]);
           SetLength(Data, sz);
           ReadBinaryData(L[I], Data[0], sz);
           S := '';
           for J := 0 to sz - 1 do
            begin
             S := S + Format('%2x',[Data[I]]);
            end;
          end;
 // Unknown?
         rdUnknown :
          S := 'Unknown';
        end;
        Memo1.Lines.Add(L[I] + #9'' + S);
       end;
     end;
    Free;
   end;
  L.Free;
 end;
 

Writing Binary Data

This example shows how to write binary data to the registry. Here we use static array, but it can be dynamic also.
 procedure TForm1.Button1Click(Sender: TObject);
 var
  B : Array [0..15] of Byte;
  I : Byte;
 Const
  SubKey  : String = 'Software\RegDemo';
 begin
  R := TRegistry.Create;
  with R do
   begin
 //
 // Set random data
 //
    for I := 0 to 15 do
     B[I] := Random(255);
 //
 // Create subkey
 //
    OpenKey(SubKey + '\BinKey', True);
 //
 // write binary data
 //
    WriteBinaryData('Value', B, SizeOf(B));
    Free;
   end;
 end;
 

Getting Subsection Information

The last example in this series. Here we get the information about the entire subkey - number of values, maximum length of the data and so on. This can be useful when you read the entire section into memory.
 var
  R  : TRegistry;
  KI : TRegKeyInfo;
 const
  Key  : String = '\Enum\Network\Family\0000\';

 procedure TForm1.Button1Click(Sender: TObject);
 begin
  R := TRegistry.Create;
   with R do
   begin
 //
 // Set Root Key
 //
    RootKey := HKEY_LOCAL_MACHINE;
 //
 // Open Subkey
 //
    OpenKey(Key, False);
 //
    if GetKeyInfo(KI) then
     begin
      with KI do
       begin
        Memo1.Lines.Add(CurrentPath + #13#10);
        Memo1.Lines.Add('MaxSubKeyLen' + #9   +
         IntToStr(MaxSubKeyLen));
        Memo1.Lines.Add('NumValues'    + #9   +
         IntToStr(NumValues));
        Memo1.Lines.Add('MaxValueLen'  + #9   +
         IntToStr(MaxValueLen));
        Memo1.Lines.Add('MaxDataLen'   + #9   +
         IntToStr(MaxDataLen));
        Memo1.Lines.Add('FileTime'     + #9#9 +
         DateTimeToStr(FileTime.dwLowDateTime));
       end;
     end;
   end;
 end;