Windows with Delphi Windows API (94) Windows Filesystem (41) Windows Forms (69) Windows Graphics (38)
Exchange Links About this site Links to us 
New related comments Number of comments in the last 48 hoursPlay WAV files 1 new comments
|
Pitfalls reading from the Registry (ProxyEnable)
1 comments. Current rating: (1 votes). Leave comments and/ or rate it.
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.  | |  | |
ProxyEnabled := ReadBool(sProxyEnable);
case GetDataType(sProxyEnable) of
rdInteger:
ProxyEnabled := ReadBool(sProxyEnable);
rdBinary:
ReadBinaryData(sProxyEnable, ProxyEnabled, 1);
end;
case GetDataType(sProxyEnable) of
rdInteger:
ProxyEnabled := ReadBool(sProxyEnable);
rdBinary:
ReadBinaryData(sProxyEnable, ProxyEnabled, GetDataSize(sProxyEnable));
end;
| |  | |  | You don't like the formatting? Check out SourceCoder then!
Comments:
|