DelphiFAQ Home Search:

Pitfalls reading from the Registry (ProxyEnable)

 

comments1 comments. Current rating: 2 stars (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.

// 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;
You don't like the formatting? Check out SourceCoder then!

Comments:

2006-05-10, 06:21:00
bebo from United States  
rating

 

 

Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option.
 
It seems that you are
from Los Angeles, US .

Info/ Feedback on this

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:
photo Add a picture:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity. The owner of this web site reserves the right to delete such material.