2006. július 23., vasárnap

Format a disk


Problem/Question/Abstract:

How to format a disk

Answer:

Use the SHFormatDrive- function in then Shell32.dll. You will get (Windows NT, Win95 ???) the standard format-disk- dialog. Put the following in your interface-part:

const
  SHFMT_ID_DEFAULT = $FFFF; {Default for physical format}
  SHFMT_OPT_QUICKFORMAT = $0000; {do quick formatting}
  SHFMT_OPT_FULL = $0001; {complete formatting}
  SHFMT_OPT_SYSONLY = $0002; {copy system files only}
  SHFMT_ERROR = $FFFFFFFF; {formatting error}
  SHFMT_CANCEL = $FFFFFFFE; {formatting aborted}
  SHFMT_NOFORMAT = $FFFFFFFD; {unable to format}

function SHFormatDrive(hWnd: HWND; Drive, fmtID, Options: Word): Longint; stdcall;

implementation

const
  Shell32 = 'Shell32.dll';

function SHFormatDrive; external Shell32 name 'SHFormatDrive';

And a little example for executing the function and formatting drive A:

procedure TMainFrm.DiskFormatClick(Sender: TObject);
var
  FmtRes: longint;
  DriveNo: word;
begin
  DriveNo := 0; {Drive A}
  try
    FmtRes := ShFormatDrive(Handle, DriveNo, SHFMT_ID_DEFAULT, SHFMT_OPT_QUICKFORMAT);
    if FmtRes < 0 then
      ShowMessage('Error or Abort Formatting');
  except
  end;
end;


Solve 2:

procedure FormatFloppy(Drive: byte);
{Procedure to bring up the standard Windows format dialog to format a floppy drive.
Pass 0 for A:\ or 1 for B:\ }
type
  TSHFormatDrive = function(hWnd: HWND; Drive: Word; fmtID: Word;
    Options: Word): Longint stdcall;
var
  SHFormatDrive: TSHFormatDrive;
  LibHandle: THandle;
begin
  LibHandle := LoadLibrary(PChar('Shell32.dll'));
  if LibHandle <> 0 then
    @SHFormatDrive := GetProcAddress(LibHandle, 'SHFormatDrive')
  else
  begin
    MessageDlg('Failed to load Shell32.dll.', mtError, [mbOK], 0);
    Exit;
  end;
  if @SHFormatDrive <> nil then
    SHFormatDrive(Application.Handle, Drive, { 0 = A:\, 1 = B:\ } $FFFF, 0);
  FreeLibrary(LibHandle);
  @SHFormatDrive := nil;
end;

Nincsenek megjegyzések:

Megjegyzés küldése