Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Reading registry error

Posted on 2006-07-13
7
Medium Priority
?
897 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 500 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

670 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