2010. április 7., szerda

How to disable the CD autorun feature through code


Problem/Question/Abstract:

How to disable the CD autorun feature through code

Answer:

Solve 1:

uses
  Registry;

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;

procedure SetCdAutoRun(bOn: bool);
var
  reg: TRegistry;
  AutoRunSetting: integer;
begin
  reg := TRegistry.Create;
  reg.RootKey := HKEY_CURRENT_USER;
  reg.LazyWrite := false;
  reg.OpenKey('Software\Microsoft\Windows\' + 'CurrentVersion\Policies\Explorer',
    false);
  reg.ReadBinaryData('NoDriveTypeAutoRun', +AutoRunSetting, sizeof(AutoRunSetting));
  if bOn then
    AutoRunSetting := AutoRunSetting and not (1 shl 5)
  else
    AutoRunSetting := AutoRunSetting or (1 shl 5);
  reg.WriteBinaryData('NoDriveTypeAutoRun', +AutoRunSetting, sizeof(AutoRunSetting));
  reg.CloseKey;
  reg.free;
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


{...}

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    procedure SetCDAutoRun(AAutoRun: Boolean);
  public
    { Public declarations }
  end;

  {...}

procedure TForm1.Button1Click(Sender: TObject);
begin
  SetCDAutoRun(FALSE);
end;

procedure TForm1.SetCDAutoRun(AAutoRun: Boolean);
const
  DoAutoRun: array[Boolean] of Integer = (0, 1);
var
  Reg: TRegistry;
begin
  try
    { create our registry object }
    Reg := TRegistry.Create;
    { set our registry root key }
    Reg.RootKey := HKEY_LOCAL_MACHINE;
    { verify that our CDROM class key exists }
    if Reg.KeyExists('System\CurrentControlSet\Services\Class\CDROM') then
      { try to open our CDROM class key }
      if Reg.OpenKey('System\CurrentControlSet\Services\Class\CDROM', FALSE) then
        { add AutoRun to our CDROM class key }
        Reg.WriteBinaryData('AutoRun', DoAutoRun[AAutoRun], 1);
  finally
    { free our registry object }
    Reg.Free;
  end;
  { showmessage that the changes will happen on reboot }
  ShowMessage('Your settings will take effect on the next reboot of Windows.');
end;

{...}

Nincsenek megjegyzések:

Megjegyzés küldése