Search code examples
delphidelphi-xe6

Error DiskSize Delphi


I have a function that checks if the drive (CD / DVD, USB key, floppy are OK) ...

It worked perfectly ... If it is less than 0 ... It returns that there is no drive connected ...

The big problem, I removed my pendrive, without ejecting;;; I pulled the pendrive ... Hence the DiskSize function returns the following error:

there is the disk in the drive. please insert a disk into drive device

How do I fix this error ... because the function is working properly ... Just DiskSize that is generating this error, the fact that I was puzado the stick without Eject ...

// Check if drive is OK
function DriveOK (Drive: Char): boolean;
var
I: byte;
space: integer;
begin
   Drive: = upcase (Drive);
   not if (Drive in ['A' .. 'Z']) then Begin
     raise Exception.Create ('incorrect Unit');
   end;

   I: = Ord (Drive) - 64;
   if (DiskSize(I) >= 0) then Begin
      Result: = false;
   End Else Begin
      Result: = true;
   end;
end;

My system is Windows 7 64bit ... I found, the error is generated since removed the Printer Memory Card ... But the problem is that this card compatilhado Network ... When I remove the network share ... it does not generate the error ...

screenshot


Solution

  • You need to turn off Windows' internal error reporting to disable the popup error dialog:

    function DriveOK(Drive: Char): Boolean;
    var
      I: byte;
      mode: UINT;
    begin
      Drive := UpCase(Drive);
      if not (Drive in ['A' .. 'Z']) then begin
        raise Exception.Create('incorrect Unit');
      end;
    
      I := Ord(Drive) - 64;
    
      mode := SetErrorMode(SEM_FAILCRITICALERRORS);
      mode := SetErrorMode(mode or SEM_FAILCRITICALERRORS);
      try
        if (DiskSize(I) >= 0) then begin
          Result := False;
        end else begin
          Result := True;
        end;
      finally
        SetErrorMode(mode);
      end;
    end;
    

    Alternatively:

    function DriveOK(Drive: Char): Boolean;
    var
      I: byte;
      mode: DWORD;
    begin
      Drive := UpCase(Drive);
      if not (Drive in ['A' .. 'Z']) then begin
        raise Exception.Create ('incorrect Unit');
      end;
    
      I := Ord(Drive) - 64;
    
      SetThreadErrorMode(GetThreadErrorMode() or SEM_FAILCRITICALERRORS, @mode);
      try
        if (DiskSize(I) >= 0) then begin
          Result := False;
        end else begin
          Result := True;
        end;
      finally
        SetThreadErrorMode(mode, nil);
      end;
    end;