Solved

Reading registry error

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to fill array with TArray.Create? 14 90
Delphi - replicating a form 8 74
Create a path if not exists 7 77
Twebbrowser in Fire monkey android scrolling to bottom 1 30
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

773 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