错在哪儿??????(20分)

H

hami

Unregistered / Unconfirmed
GUEST, unregistred user!
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;

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);
AutoRunSetting:=reg.ReadInteger('NoDriveTypeAutoRun');
if bOn then

AutoRunSetting := AutoRunSetting and not (1 shl 5)
else

AutoRunSetting := AutoRunSetting or (1 shl 5);
reg.WriteInteger('NoDriveTypeAutoRun',AutoRunSetting);
reg.CloseKey;
reg.free;
end;




procedure TForm1.Button1Click(Sender: TObject);
begin

if IsCdAutoRunOn then
RadioGroup1.ItemIndex:=0
else

RadioGroup1.ItemIndex:=1;
end;


procedure TForm1.Button2Click(Sender: TObject);
begin

if RadioGroup1.ItemIndex=0 then

SetCdAutoRun(True)
else

SetCdAutoRun(False);
end;
 
reg.OpenKey(
'Software/Microsoft/Windows/CurrentVersion/Policies/Explorer',
false);
~~~~~~

这里,改成True,否则他不会给你建立主键的。
 
问题是说NoDriveTypeAutoRun数据类型不对
 
function IsCdAutoRunOn:bool;
中首先读NoDriveTypeAutoRun的值,但第一次访问该键值时,
该键值不存在,当然会出错,为了避免此类现象,可以加上一句。
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);
//改为TRUE
//判断NoDriveTypeAutoRun是否存在,不存在,赋初始值,比如初始值为0时。
if not reg.ExistValues('NoDriveTypeAutoRun') then

reg.WriteInteger('NoDriveTypeAutoRun',0);

AutoRunSetting:=reg.ReadInteger('NoDriveTypeAutoRun');
reg.CloseKey;
reg.free;
result := not ((AutoRunSetting and (1 shl 5)) <> 0);
end;

 
顶部