Solved

Reading registry error

Posted on 2006-07-13
7
887 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses
Course of the Month4 days, 11 hours left to enroll

635 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