Mega Code Archive

 
Categories / Delphi / Examples
 

Pitfalls reading from the registry [proxyenable]

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. // Version 1: // raises an exception because of the unexpected entry type ProxyEnabled := ReadBool(sProxyEnable); // Version 2: // detect type with GetDataType(), // but not size... raises an exception if the size is wrong case GetDataType(sProxyEnable) of rdInteger: ProxyEnabled := ReadBool(sProxyEnable); rdBinary: ReadBinaryData(sProxyEnable, ProxyEnabled, 1); // other types.. end; // Version 3: // detect type and size (GetDataSize())! Works so far. case GetDataType(sProxyEnable) of rdInteger: ProxyEnabled := ReadBool(sProxyEnable); rdBinary: ReadBinaryData(sProxyEnable, ProxyEnabled, GetDataSize(sProxyEnable)); // other types.. end;