Solved

Reading registry error

Posted on 2006-07-13
7
872 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

895 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