Thema: Delphi External Exception

Einzelnen Beitrag anzeigen

Benutzerbild von Luckie
Luckie

Registriert seit: 29. Mai 2002
37.621 Beiträge
 
Delphi 2006 Professional
 
#1

External Exception

  Alt 18. Dez 2004, 15:19
Ich hatte folgenden Code:
Delphi-Quellcode:
procedure LoadLogicalDrives(Strings: TStrings; ReadyOnly: Boolean = True;
  WithLabels: Boolean = True);

  function DriveIsReady(Drive: string): Boolean;
  var
    i : Integer;
    sRec : TSearchRec;
  begin
    SetErrorMode(SEM_FAILCRITICALERRORS);
    i := FindFirst(Drive + '*.*', faAnyFile, sRec);
    if i = 0 then
    begin
      Result := True;
    end
    else
    begin
      Result := False;
    end;
    FindClose(sRec);
    SetErrorMode(0);
  end;

var
  s : DWORD;
  AllStrings : PChar;
  CurrentString: PChar;
begin
  s := 255;
  GetMem(AllStrings, Succ(s));
  GetLogicalDriveStrings(S, AllStrings);
  try
    if AllStrings <> nil then
    begin
      CurrentString := AllStrings;
      while CurrentString[0] <> #0 do
      begin
        if (DriveIsReady(StrPas(CurrentString))) and ReadyOnly then
        begin
          if WithLabels then
            Strings.Add(StrPas(CurrentString)+' ['+GetVolumelabel(CurrentString)+']')
          else
            Strings.Add(StrPas(CurrentString));
        end;
        Inc(CurrentString, Succ(StrLen(CurrentString)));
      end;
    end;
  finally
    FreeMem(AllStrings);
  end;
end;
Jetzt wollte ich erst mal das FindFile loswerden und habe entsprechend FindFikles benutzt. dann sieht das so aus:
Delphi-Quellcode:
procedure LoadLogicalDrives(var Drives: TStringArray; ReadyOnly: Boolean = True;
  WithLabels: Boolean = True);

  function DriveIsReady(const Drive: string): Boolean;
  var
    wfd : TWin32FindData;
    hFindData : THandle;
  begin
    SetErrorMode(SEM_FAILCRITICALERRORS);
    hFindData := FindFirstFile(Pointer(Drive + '*.*'), wfd);
    if hFindData <> INVALID_HANDLE_VALUE then
    begin
      Result := True;
    end
    else
    begin
      Result := False;
    end;
    CloseHandle(hFindData);
    SetErrorMode(0);
  end;

var
  s : DWORD;
  AllStrings : PChar;
  CurrentString: PChar;
  cntDrives: Integer;
begin
  s := 255;
  cntDrives := 0;
  SetLength(Drives, 26);
  GetMem(AllStrings, Succ(s));
  GetLogicalDriveStrings(S, AllStrings);
  try
    if AllStrings <> nil then
    begin
      CurrentString := AllStrings;
      while CurrentString[0] <> #0 do
      begin
        if ReadyOnly then
        begin
          if (DriveIsReady(StrPas(CurrentString))) then
          begin
            if WithLabels then
              Drives[cntDrives] := String(CurrentString)+ ' ['+GetVolumeLabel(currentString)+']'
            else
              Drives[cntDrives] := String(StrPas(CurrentString));
            Inc(cntDrives);
          end;
        end
        else
        begin
          if WithLabels then
            Drives[cntDrives] := String(CurrentString) + ' [' +
              GetVolumelabel(CurrentString) + ']'
          else
            Drives[cntDrives] := String(CurrentString);
          Inc(cntDrives);
        end;
        Inc(CurrentString, Succ(StrLen(CurrentString)));
      end;
    end;
  finally
    SetLength(Drives, cntDrives);
    FreeMem(AllStrings);
  end;
end;
Nur passiert jetzt das Dumme, dass er nach dem zweiten Laufwerk, das er findet, bei mir C:\, aus der Schleife rausgeht und nach FreeMem(AllStrings) springt und dann kommt es zu einer External Exception - was auch immer das heißen mag.

Weiß da jemand Rat?
Michael
Ein Teil meines Codes würde euch verunsichern.
  Mit Zitat antworten Zitat