1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
| {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.
Reading Binary Data
Reading Entire Section
Writing Binary Data
Getting Subsection Information }
//don't forget to put REGISTRY into your USES section.
//Reading Binary Data
//The example below shows how to read binary data, which is usually 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[J]]);
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; |
Partager