Solved

How To Format A Floppy Disk Through Delphi 3

Posted on 1998-06-15
15
317 Views
Last Modified: 2010-04-03
Any Ideas How To Format a floppy Disk without showing
any dialog ?
0
Comment
Question by:k6__
  • 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
 
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

705 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now