Author: Tomas Rutkauskas
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:
1 const
2 SHFMT_ID_DEFAULT = $FFFF; {Default for physical format}
3 SHFMT_OPT_QUICKFORMAT = $0000; {do quick formatting}
4 SHFMT_OPT_FULL = $0001; {complete formatting}
5 SHFMT_OPT_SYSONLY = $0002; {copy system files only}
6 SHFMT_ERROR = $FFFFFFFF; {formatting error}
7 SHFMT_CANCEL = $FFFFFFFE; {formatting aborted}
8 SHFMT_NOFORMAT = $FFFFFFFD; {unable to format}
9
10 function SHFormatDrive(hWnd: HWND; Drive, fmtID, Options: Word): Longint; stdcall;
11
12 implementation
13
14 const
15 Shell32 = 'Shell32.dll';
16
17 function SHFormatDrive; external Shell32 name 'SHFormatDrive';
18
19 //And a little example for executing the function and formatting drive A:
20
21 procedure TMainFrm.DiskFormatClick(Sender: TObject);
22 var
23 FmtRes: longint;
24 DriveNo: word;
25 begin
26 DriveNo := 0; {Drive A}
27 try
28 FmtRes := ShFormatDrive(Handle, DriveNo, SHFMT_ID_DEFAULT,
29 SHFMT_OPT_QUICKFORMAT);
30 if FmtRes < 0 then
31 ShowMessage('Error or Abort Formatting');
32 except
33 end;
34 end;
Solve 2:
35 procedure FormatFloppy(Drive: byte);
36 {Procedure to bring up the standard Windows format dialog to format a floppy drive.
37 Pass 0 for A:\ or 1 for B:\ }
38 type
39 TSHFormatDrive = function(hWnd: HWND; Drive: Word; fmtID: Word;
40 Options: Word): Longint stdcall;
41 var
42 SHFormatDrive: TSHFormatDrive;
43 LibHandle: THandle;
44 begin
45 LibHandle := LoadLibrary(PChar('Shell32.dll'));
46 if LibHandle <> 0 then
47 @SHFormatDrive := GetProcAddress(LibHandle, 'SHFormatDrive')
48 else
49 begin
50 MessageDlg('Failed to load Shell32.dll.', mtError, [mbOK], 0);
51 Exit;
52 end;
53 if @SHFormatDrive <> nil then
54 SHFormatDrive(Application.Handle, Drive, { 0 = A:\, 1 = B:\ } $FFFF, 0);
55 FreeLibrary(LibHandle);
56 @SHFormatDrive := nil;
57 end;
|