Solved

How To Format A Floppy Disk Through Delphi 3

Posted on 1998-06-15
15
325 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

773 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