Solved

How To Format A Floppy Disk Through Delphi 3

Posted on 1998-06-15
15
331 Views
Last Modified: 2010-04-03
Any Ideas How To Format a floppy Disk without showing
any dialog ?
0
Comment
Question by:k6__
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 2
  • +3
15 Comments
 
LVL 5

Expert Comment

by:ronit051397
ID: 1352930
0
 
LVL 2

Expert Comment

by:brainware
ID: 1352931
There is an API hidden away in Shell32.dll called SHFormatDrive,
 this brings up the standard format removable drive dialog.

{implementation section}


const
        SHFMT_ID_DEFAULT      = $FFFF;  // Formating options
        SHFMT_OPT_QUICKFORMAT = $0000;
        SHFMT_OPT_FULL        = $0001;
        SHFMT_OPT_SYSONLY     = $0002;  // Error codes
        SHFMT_ERROR           = $FFFFFFFF;
        SHFMT_CANCEL          = $FFFFFFFE;
        SHFMT_NOFORMAT        = $FFFFFFFD;

function SHFormatDrive(Handle: HWND; Drive, ID, Options: Word): LongInt;
        stdcall; external 'shell32.dll' name 'SHFormatDrive'

procedure TForm1.btnFormatDiskClick(Sender: TObject);
var
  retCode: LongInt;

begin
 retCode:= SHFormatDrive(Handle, 0, SHFMT_ID_DEFAULT,SHFMT_OPT_QUICKFORMAT);
  if retCode < 0 then
      ShowMessage('Could not format drive');
end;

end.

You will need to contact the Dialog somehow, evt. via WinAPI, if you dont get it working or get another answer here, mail me and ill try code it for you..  brainware@software.dk


0
 
LVL 4

Expert Comment

by:BoRiS
ID: 1352932
k6__

Why not use the DeviceIOControl API call and pass the parameter IOCTL_DISK_FORMAT_TRACKS etc. There are a lot more options as well like segtting the volume label etc...

The IOCTL_DISK_FORMAT_TRACKS DeviceIoControl operation formats a specified, contiguous set of tracks on a disk.

Later
BoRiS
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:k6__
ID: 1352933
i said i don't want to show any dialog.. so IOCTL_DISK_FORMAT_TRACKS function can do it ? an example ? =)

thanx =)

0
 
LVL 2

Author Comment

by:k6__
ID: 1352934


0
 
LVL 4

Expert Comment

by:BoRiS
ID: 1352935
k6__

If you wanted code example all you had to do is ask(not reject the answer), for future
reference ask for source code examples in the first question...

Anyway enough of that now (GEEZ)

I'll give you an example, let me just check it out again...

Later
BoRIS
0
 
LVL 5

Expert Comment

by:ronit051397
ID: 1352936
0
 
LVL 2

Author Comment

by:k6__
ID: 1352937
ermmh =| sorry ... i accidentally rejected it... =(
i thouht that i choose Acceptable .. =(
sorry again Boris
0
 

Expert Comment

by:ms99y
ID: 1352938

Why not use the CreateProcess or WinExec to use the format program under c:\dos.
And by giving the SW_HIDE as a parameter to hide the window, and a text file to redirect to FORMAT program to respond to the prompts from the FORMAT command(like insert diskette, enter volume label...)


0
 
LVL 2

Author Comment

by:k6__
ID: 1352939
Nope ... i though of that but i don't want to call DOS through
my app =)

Thanx...

0
 
LVL 4

Accepted Solution

by:
jeurk earned 50 total points
ID: 1352940
Hi,
this is a source for a component that can format your floppy
if you want you can format drives too.
past the following code in a .pas
then use add component
---------------------------------
unit formatdisk;

(* TFormatDrive by Markus Stephany (c) 97

this component is used to format a floppy (or a hd) under windows 95/delphi 2.01
it wraps the explorer's SHFormatDrive-dialog

freeware, no limitations (except of the copyright), no guaranties, no fee... just enjoy.

contact:
mirbir.st@t-online.de
mirbir.st@saargate.de
http://home.t-online.de/home/mirbir.st/

revision 0.1 - initial release 08/23/97*)




interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type TFormatOptions = (foQuick,foNormal,foSystem);

type
  TFormatDrive = class(TComponent)
  private
    { Private-Deklarationen }
    fopts : tformatoptions;
    fdrive : char;
    fallowhd : boolean;
    ferror : integer;
    fwnd : hwnd;
  protected
    { Protected-Deklarationen }
  public
    { Public-Deklarationen }
    constructor create(aowner:tcomponent) ; override;
    function Execute : boolean;
             // starts the formatting-dialog
    property Error : integer read ferror;
  published
    { Published-Deklarationen }
    property DefaultAction : TFormatOptions read fopts write fopts;
             // indicates the default-choosen action to perfomr
             // quick-format,normal format or copying of the sys-files
    property Drive : char read fdrive write fdrive;
             // the drive [a..z] to be formatted
    property AllowHD : boolean read fallowhd write fallowhd;
             // switches whether hard-drives can be formatted or not (for security)
  end;

const
     FDE_Invalid_Drive_Type = -100;
     FDE_No_Disk_In_Drive = -101;
     FDE_HD_Not_Allowed = -102;

procedure Register;

function SHFormatDrive (wnd:hwnd;drive:integer;size:uint;action:integer):integer; stdcall; external
         'shell32' name 'SHFormatDrive';

// wnd : the handle of the owner-window for the format-dialog
// drive : 0=a, 1=b ...
// size : ? seems not to be implemented
{  action :
            0 : quick - format (destroys only the fat)
            1 : normal formatting
            2 : "sys x:"}
// result : the result should be 0 if the function was successfull, but it seems that
//          error # 6 (= error_invalid_handle) is returned on success




implementation

procedure Register;
begin
  RegisterComponents('System', [TFormatDrive]);
end;

// constructor
constructor tformatdrive.create(aowner:tcomponent) ;
begin
     inherited;
     fopts := foquick;
     drive := 'a';
     allowhd := false;
     ferror := 0;
     fwnd := 0;
     if aowner is twincontrol then fwnd := (aowner as twincontrol).handle;
end;

// execute
function tformatdrive.execute : boolean;
var i : integer;
    sr : tsearchrec;
begin
     ferror := 0;
     result := false;
     // check drivetype (only floppy (and  maybe hd) are allowed)
     i:= getdrivetype(pchar(drive+':\'));
     case i of
          0,1,drive_remote,drive_cdrom,drive_ramdisk : begin
                                                            ferror := FDE_Invalid_Drive_Type;
                                                            exit;
                                                       end;
     end;
     // check whether hd is allowed
     if i = drive_fixed then if not fallowhd then begin
        ferror := fde_hd_not_allowed;
        exit;
     end;
     // check if disk is inserted into drive
     if i = drive_removable then begin
        fillchar(sr,sizeof(tsearchrec),0);
        i := findfirst(drive+':\*.*',faanyfile,sr);
        findclose(sr);
        if (i = 3) or (i=21) then begin
           ferror := fde_no_disk_in_drive;
           exit;
        end;
     end;

     //now let us start the shformatdrive-api
     ferror:=SHFormatDrive (fwnd,ord(upcase(drive))-ord('A'),0,integer(fopts));
     if ferror = 6 then ferror := 0; // what happens here with error_invalid_handle ?
     result := (ferror = 0);

end;

end.

0
 
LVL 4

Expert Comment

by:jeurk
ID: 1352941
Drop a component to the form, and call yourcomponent.execute
you will have no dialog opening.
I hope it helps.
Jeurk
0
 
LVL 2

Author Comment

by:k6__
ID: 1352942
jeurk thanx but i didn't want that.. i knew the SHFormatDrive
function of Shell32.dll but anyway somebody posted the answer
that i was looking through a win32s Function =)


0
 
LVL 4

Expert Comment

by:jeurk
ID: 1352943
Hello,
So why did I earned the points ;) ?

0
 
LVL 2

Author Comment

by:k6__
ID: 1352944
your ean points cause i accidentally REJECTED =( the right
answer =(

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Printing problem 2 110
LAN or WAN ? 11 106
Why  'Host Connection unavailable/Socket Error #113 No route to host' 6 75
How to save the image in the .cds File ClientDataSet? 1 30
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question