Author: Tomas Rutkauskas
Pitfalls reading from the Registry (ProxyEnable)
Answer:
The following has been verified for Delphi 3. It may be slightly different for
Delphi 5.
In a project I had to read the Boolean field “ProxyEnable” from the registry. It is
stored in
HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings
On the systems that I work with (NT 4.0/ sp 5, NT 4.0/ sp 6, Win 2000/ sp 1) I
checked and determined that this value was stored as a REG_DWORD. 0 meant false, 1
meant true. So I simply coded as shown in 1)
Pitfall 1): Unexpected Entry Type
On some customer machines, the value ProxyEnable was stored as a REG_BINARY.
Reading a REG_BINARY with ReadInteger() causes an exception – and to make things
worse, exception handling was not yet in place since it was executed during the
initialization of my application.
So I changed my code to 2) and hoped it would work.
Pitfall 2): Read the exact length
This was a surprising one. When you have a registry value of type REG_BINARY that
is 4 bytes long and you try reading it with
ReadBinaryData("MyKey", myBooleanVar, 1);
… then you will experience another exception. The correct code is as shown in part
3.
1 // Version 1:2 // raises an exception because of the unexpected entry type3 4 ProxyEnabled := ReadBool(sProxyEnable);
5 6 // Version 2:7 // detect type with GetDataType(),8 // but not size... raises an exception if the size is wrong9 10 case GetDataType(sProxyEnable) of11 rdInteger:
12 ProxyEnabled := ReadBool(sProxyEnable);
13 rdBinary:
14 ReadBinaryData(sProxyEnable, ProxyEnabled, 1);
15 // other types..16 end;
17 18 // Version 3:19 // detect type and size (GetDataSize())! Works so far.20 21 case GetDataType(sProxyEnable) of22 rdInteger:
23 ProxyEnabled := ReadBool(sProxyEnable);
24 rdBinary:
25 ReadBinaryData(sProxyEnable, ProxyEnabled, GetDataSize(sProxyEnable));
26 // other types..27 end;