Author: William Gerbert
How to disable the CD autorun feature through code
Answer:
Solve 1:
1 uses
2 Registry;
3
4 function IsCdAutoRunOn: bool;
5 var
6 reg: TRegistry;
7 AutoRunSetting: integer;
8 begin
9 reg := TRegistry.Create;
10 reg.RootKey := HKEY_CURRENT_USER;
11 reg.OpenKey('Software\Microsoft\Windows\' + 'CurrentVersion\Policies\Explorer',
12 false);
13 reg.ReadBinaryData('NoDriveTypeAutoRun', +AutoRunSetting, sizeof(AutoRunSetting));
14 reg.CloseKey;
15 reg.free;
16 result := not ((AutoRunSetting and (1 shl 5)) <> 0);
17 end;
18
19 procedure SetCdAutoRun(bOn: bool);
20 var
21 reg: TRegistry;
22 AutoRunSetting: integer;
23 begin
24 reg := TRegistry.Create;
25 reg.RootKey := HKEY_CURRENT_USER;
26 reg.LazyWrite := false;
27 reg.OpenKey('Software\Microsoft\Windows\' + 'CurrentVersion\Policies\Explorer',
28 false);
29 reg.ReadBinaryData('NoDriveTypeAutoRun', +AutoRunSetting, sizeof(AutoRunSetting));
30 if bOn then
31 AutoRunSetting := AutoRunSetting and not (1 shl 5)
32 else
33 AutoRunSetting := AutoRunSetting or (1 shl 5);
34 reg.WriteBinaryData('NoDriveTypeAutoRun', +AutoRunSetting,
35 sizeof(AutoRunSetting));
36 reg.CloseKey;
37 reg.free;
38 end;
Solve 2:
After an hour of tinkering with some samples on the Internet I came up with the
proper registry setting to turn on/off the CDROM AutoRun feature. The first thing
wrong with all the samples was the type of value we need to store in the registry,
I found that binary is what works. The next thing most samples neglected to say,
besides being just wrong, was that you need to reboot your system after making the
change. This tip was tested on Windows98, so this registry setting may differ a
little for other operating systems. We will be using the TRegistry object using
TRegistry.KeyExists, TRegistry.OpenKey, and TRegistry.WriteBinaryData
First we will need to add Registry to our unit's uses clause
Next we will need a TButton and a TButton.OnClick event. You can easily create this
event through the Object Inspector Events tab
We will then declare a new procedure that will make the settings according to the
boolean parameter sent in
In our routine we will first create our TRegistry object, then we will set the
TRegistry.RootKey. Next we will check to see if the CDROM registry key exists
before trying to open it. After opening the CDROM registry key we will add AutoRun
to it according to the boolean parameter sent into the procedure
TRegistry.KeyExists returns TRUE is the specified registry key exists. The only
parameter is a string to the key in the registry. If we set TRegistry.RootKey then
we do not have to specify it within the string parameter
TRegistry.OpenKey returns TRUE if the specified registry key opens successfully.
The first parameter is a string to the key in the registry. As mentioned above, if
we set TRegistry.RootKey then we do not have to specify it within the string
parameter. The second parameter is a boolean value telling Windows whether or not
to create the key if it does not exist, we will set this to FALSE
TRegistry.WriteBinaryData stores binary data to the registry. The first parameter
is the name to stoe under the open registry key. The second parameter is the value
to store. The last parameter is the size of the binary value we are storing
39 {...}
40
41 type
42 TForm1 = class(TForm)
43 Button1: TButton;
44 procedure Button1Click(Sender: TObject);
45 private
46 { Private declarations }
47 procedure SetCDAutoRun(AAutoRun: Boolean);
48 public
49 { Public declarations }
50 end;
51
52 {...}
53
54 procedure TForm1.Button1Click(Sender: TObject);
55 begin
56 SetCDAutoRun(FALSE);
57 end;
58
59 procedure TForm1.SetCDAutoRun(AAutoRun: Boolean);
60 const
61 DoAutoRun: array[Boolean] of Integer = (0, 1);
62 var
63 Reg: TRegistry;
64 begin
65 try
66 { create our registry object }
67 Reg := TRegistry.Create;
68 { set our registry root key }
69 Reg.RootKey := HKEY_LOCAL_MACHINE;
70 { verify that our CDROM class key exists }
71 if Reg.KeyExists('System\CurrentControlSet\Services\Class\CDROM') then
72 { try to open our CDROM class key }
73 if Reg.OpenKey('System\CurrentControlSet\Services\Class\CDROM', FALSE) then
74 { add AutoRun to our CDROM class key }
75 Reg.WriteBinaryData('AutoRun', DoAutoRun[AAutoRun], 1);
76 finally
77 { free our registry object }
78 Reg.Free;
79 end;
80 { showmessage that the changes will happen on reboot }
81 ShowMessage('Your settings will take effect on the next reboot of Windows.');
82 end;
83
84 {...}
|