这个程序编译没问题,运行的时候出错,哪位大虾告诉我什么原因?
function GetAllNames(rKey:HKEY;sKey:string):TStringList;
var
  reg:TRegistry;
  aList:TStringList;
  i:integer;
begin
  result:=nil;
  reg:=TRegistry.Create;
  aList:=TStringList.Create;
  with reg do
  begin
    RootKey:=rKey;
    if OpenKey(sKey,false) then
    begin
      GetValueNames(aList);
      for i:=0 to aList.Count-1 do
       result.Strings[i]:=ReadString(aList.Strings[i]);
    end;
    CloseKey;
    Free;
  end;
  aList.Free;
end;  

解决方案 »

  1.   

    your result of function is TStringList, but you never allocate memory, so you should change you function declaration, and create a TStringList instance before calling the procedureprocedure GetAllNames(rKey:HKEY;sKey:string, var sList: TStringList);
    var
      reg:TRegistry;
      aList:TStringList;
      i:integer;
    begin
      //result:=nil;
      if not Assigned(sList) then Exit;  //check aList  reg:=TRegistry.Create;
      aList:=TStringList.Create;
      with reg do
      begin
        RootKey:=rKey;
        if OpenKey(sKey,false) then
        begin
          GetValueNames(aList);
          for i:=0 to aList.Count-1 do
           //result.Strings[i]:=ReadString(aList.Strings[i]);
           sList.Strings[i]:=ReadString(aList.Strings[i]);
        end;
        CloseKey;
        Free;
      end;
      aList.Free;
    end;  
      

  2.   

    我用变量参数,当调用这个函数时发生如小错误:List index out of bounds(0),不知道是什么原因?
      

  3.   

    sList.Strings[i]:=ReadString(aList.Strings[i]);
    当alist内没有值时发生的错误?