unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    ProgressBar1: TProgressBar;
    rgDrive: TRadioGroup;
    Label1: TLabel;
    Label2: TLabel;
    cbVerify: TCheckBox;
    rgCapacity: TRadioGroup;
    Edit1: TEdit;
    Label3: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation
{$R *.DFM}

const
  cLibId = 163402;
  DllName = 'fformatx.dll';

function DiskOpInit(Drive: Integer; MediaType: Integer; VolumeLab: string; NeedVerify: BOOL; var LibId: Integer): Integer; stdcall; external DllName;
function FormatCylinder(Cyl: Integer): Integer; export; stdcall; external DllName;
function DiskCompletion(NeedFatsAndRoot: WordBool; LibId: Integer): Integer; export; stdcall; external DllName;
function GetFloppyType(Drive: Integer; var ErrNo: Integer): Integer; export; stdcall; external DllName;


procedure TForm1.Button1Click(Sender: TObject);
var
  LibId: Integer;
  i, Res: Integer;
  ErrNo: Integer;
  FloppyType: Integer;
  FloppyTypeStr, FormatErrStr: string;
  DriveByte: Byte;
begin
  LibId:=cLibId;
  DriveByte:=rgDrive.ItemIndex;
  Label1.Caption:='';
  Label2.Caption:='';

  FloppyType:=GetFloppyType(DriveByte, ErrNo);
  case FloppyType of
    1: FloppyTypeStr:='360KB';
    2: FloppyTypeStr:='1.2MB';
    3: FloppyTypeStr:='720KB';
    4: FloppyTypeStr:='1.44MB';
    5: FloppyTypeStr:='1.72MB';
    6: FloppyTypeStr:='DMF1';
    7: FloppyTypeStr:='DMF2';
  else
       FloppyTypeStr:='Unknown';
  end;

  case ErrNo of
    0: Label1.Caption:='Old capacity: '+FloppyTypeStr+'.  Disk is empty.';
    1: Label1.Caption:='Old capacity: '+FloppyTypeStr+'.  Disk is NOT empty.';
    2: Label1.Caption:='No disk in the drive OR disk is not formatted.';
    3: Label1.Caption:='No disk in the drive.';
  end;

  if ErrNo<>3 then begin
    ProgressBar1.Max:=80;
    ProgressBar1.Position:=0;
    //   FloppyType = (FUnknown,NotSupp,F1200,F720,F1440,NotSupp,FDMF1,FDMF2);
    //                 0        1       2     3    4     5       6     7
    if rgCapacity.ItemIndex=0 then FloppyType:=3 else //720KB
    if rgCapacity.ItemIndex=2 then FloppyType:=6 else //DMF1
    if rgCapacity.ItemIndex=3 then FloppyType:=7 else //DMF2
      FloppyType:=4; //1.44MB

    case FloppyType of
      2: FloppyTypeStr:='1.2MB';
      3: FloppyTypeStr:='720KB';
      4: FloppyTypeStr:='1.44MB';
      6: FloppyTypeStr:='DMF1';
      7: FloppyTypeStr:='DMF2';
    end;
    Label2.Caption:='New capacity: '+FloppyTypeStr+'.  Formatting...';

    Res:=DiskOpInit(DriveByte,
      FloppyType,
      Edit1.Text,
      cbVerify.Checked,
      LibId);

    if Res=0 then begin
      Res:=FormatCylinder(0);
      Application.ProcessMessages;

      case Res of
        1: FormatErrStr:='The drive is not ready.';
        2: FormatErrStr:='No disk in the drive.';
        3: FormatErrStr:='The disk is write protected.';
        0:
          begin
            for i:=1 to 79 do begin
              Res:=FormatCylinder(i);
              if Res<>0 then Break;
              ProgressBar1.Position:=i+1;
              Application.ProcessMessages;
            end;
            if Res=0 then FormatErrStr:='Result: Ok'
            else FormatErrStr:='Result:  Unknown error';
          end;
      end;
      DiskCompletion(True, LibId);

      Label2.Caption:=FormatErrStr;
    end; //if Res=0 (FormatInit)
  end; //if ErrNo<>3
end;

end.
