Solved

Reading registry error

Posted on 2006-07-13
7
870 Views
Last Modified: 2008-01-09
Hi,

I tried to read the Autorun CD setting and found a function like this :

function IsCdAutoRunOn : bool;
var
  reg: TRegistry;
  AutoRunSetting : integer;
begin
  reg := TRegistry.Create;
  reg.RootKey := HKEY_CURRENT_USER;
  reg.OpenKey(
    'Software\Microsoft\Windows\CurrentVersion\Policies\Explorer',
              false);
  reg.ReadBinaryData('NoDriveTypeAutoRun',
                     AutoRunSetting ,
                     sizeof(AutoRunSetting));
  reg.CloseKey;
  reg.free;
  result :=  not ((AutoRunSetting and (1 shl 5)) <> 0);
end;

I got it from :

http://bdn.borland.com/article/0,1410,16175,00.html

When I run it, I got error :

... raised exception class ERegistryException with message 'Invalid data type for 'NoDriveTypeAutoRun'' ...

How to fix that ?

And why the code in the Borland code example has a bug like that ??? Or do I miss something here ???

Please help.

Thanks,

0
Comment
Question by:db2trade
  • 4
7 Comments
 
LVL 26

Accepted Solution

by:
Russell Libby earned 125 total points
ID: 17103844
Its stored as a DWORD value (integer). Try the following:

function IsCdAutoRunOn : bool;
var
  reg: TRegistry;
  AutoRunSetting : integer;
begin
  reg := TRegistry.Create;
  reg.RootKey := HKEY_CURRENT_USER;
  reg.OpenKey(
    'Software\Microsoft\Windows\CurrentVersion\Policies\Explorer',
              false);
  AutoRunSetting:=reg.ReadInteger('NoDriveTypeAutoRun');
  reg.CloseKey;
  reg.free;
  result :=  not ((AutoRunSetting and (1 shl 5)) <> 0);
end;

Regards,
Russell
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 17103881

And I have no idea why they would they would post it as such. The MS Technet lists this value as a DWORD, and the delphi code for ReadBinaryData performs the following checks:

  if ((RegData = rdBinary) or (RegData = rdUnknown)) and (Result <= BufSize) then
      GetData(Name, @Buffer, Result, RegData)
    else ReadError(Name);

which is going to fail if the data is a DWORD type, which it is.

Russell

0
 
LVL 1

Author Comment

by:db2trade
ID: 17103933
Yeah, I changed it to use ReadInteger and got it working. But the strange thing is, it does not work to set/unset Autorun CD !

I just really dont understand.
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 17104030

You never mentioned attempting to set the new state (a whole different question). Please show the code that you are using (specifically the value) for enabling/disabling the autorun for CD class drives.

Russell
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 17104114
Never mind, all documentation indicates that the system needs to be restarted for the setting to take effect. Running a registry monitor check also confirms that this value is not read in during the drive inserted event.

Russell
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now