Recent

Author Topic: How to format drive using Lazarus?  (Read 4022 times)

DarkNem

  • New Member
  • *
  • Posts: 11
How to format drive using Lazarus?
« on: May 06, 2015, 01:10:30 pm »
Hey guys, i'm making a simple util to format sd memory drives and renew info on them, but i cannot figure out how to format drive so i simply use
Code: [Select]
ExecuteProcess('cmd','/c format '+Drive_Short+': /Q /V:'+Form1.Edit1.Text+'/y /X'); This works, and i can set drive name, but i would've like to make as inner part of the tool, not executing a command line.
Could you please give me a little help with that? :c
._.

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: How to format drive using Lazarus?
« Reply #1 on: May 06, 2015, 01:17:42 pm »
This works, and i can set drive name
I'll take your word for it and won't try it on my C-drive :)

Quote
but i would've like to make as inner part of the tool, not executing a command line.
You could look at the SHFormatDrive-api for Windows.
On these pages are some examples:
http://www.swissdelphicenter.ch/en/showcode.php?id=226
http://www.efg2.com/Lab/Library/UseNet/1999/0323c.txt

DarkNem

  • New Member
  • *
  • Posts: 11
Re: How to format drive using Lazarus?
« Reply #2 on: May 06, 2015, 01:45:47 pm »
Thanks for a fast reply, but this (www.swissdelphicenter.ch/en/showcode.php?id=226) asks for admin permission (we dont have these at work) and i'm too stupid to figure out how second (http://www.efg2.com/Lab/Library/UseNet/1999/0323c.txt) works, convetrer from delphi aborts without error, and if i just compile:
Code: [Select]
unit1.pas(644,55) Error: Identifier not found "TFormatOption"
unit1.pas(648,1) Error: Illegal type conversion: "<erroneous type>" to "Word"
unit1.pas(650,25) Error: Identifier not found "SHFMT_ERROR"
unit1.pas(682) Fatal: There were 3 errors compiling module, stopping
:C

I just need to read/write drivename and format it, and i guess cmd is the best i can have for now ._.
._.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: How to format drive using Lazarus?
« Reply #3 on: May 06, 2015, 02:12:18 pm »
shfmt_error is in unit shellapi/shlobj.  (afaik also in Delphi verrsions after somewhere near D4..D6, those links are really old)

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: How to format drive using Lazarus?
« Reply #4 on: May 06, 2015, 02:19:31 pm »
This should work (I used DRV_M in my example):
You say you need admin permission with this? Is this also true when right-clicking the drive and choosing format?

This method (ShFormatDrive) always gives you the Shell-dialog for format. If you really want it without confirmation you need another way (more low-level).
Code: [Select]
uses Windows, shlobj;

{$R *.lfm}

{ TForm1 }

const
  SHFMT_DRV_M = ord('M') - ord('A');
  //SHFMT_ID_DEFAULT = $FFFF;
  SHFMT_OPT_QUICKFORMAT = 0;
  SHFMT_OPT_FULLFORMAT = 1;
  SHFMT_OPT_SYSONLY = 2;
  //SHFMT_ERROR = -1;
  //SHFMT_CANCEL = -2;
  //SHFMT_NOFORMAT = -3;

function SHFormatDrive(hWnd: HWND;
  Drive: Word;
  fmtID: Word;
  Options: Word): Longint
  stdcall; external 'Shell32.dll' Name 'SHFormatDrive';

procedure TForm1.Button1Click(Sender: TObject);
var
  FmtRes: Longint;
begin
  try
    FmtRes := ShFormatDrive(Handle,
      SHFMT_DRV_M,
      SHFMT_ID_DEFAULT,
      SHFMT_OPT_QUICKFORMAT);
    case FmtRes of
      SHFMT_ERROR: ShowMessage('Error formatting the drive');
      SHFMT_CANCEL: ShowMessage('User canceled formatting the drive');
      SHFMT_NOFORMAT: ShowMessage('No Format')
        else
          ShowMessage('Disk has been formatted!');
    end;
  except
    ShowMessage('Error Occured!');
  end;
end;

DarkNem

  • New Member
  • *
  • Posts: 11
Re: How to format drive using Lazarus?
« Reply #5 on: May 06, 2015, 02:57:53 pm »
Yes, i need it. I can't formad local drives (C or D) but can format removable media storage (usb/sd etc) with right click menu.
So i guess this is the reason for ShFormatDrive to popup admin login window :c

And yeah, as you said i need it w/o confirmation, just like command line one. Is there a way?
If this is really hard to do, i might stick with command line solution, it is not that critical. I thought that would've been quite easier. Thanks again :)
._.

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: How to format drive using Lazarus?
« Reply #6 on: May 06, 2015, 06:18:34 pm »
If SHFormatDrive doesn't suite your needs (because it's a wrapper for the shell and displays a dialog) you would need to use FormatEx Api provided by  fmifs.dll.

I didn't find any clear (small) examples but here is some source for Delphi (maybe you can convert it).
Quote
The Check Disk and Format Disk component is a Delphi wrapper around the Windows APIs exported by fmifs.dll, which are used to format fixed or removable disk drives and perform disk checks on Windows NT4 and later. It supports any local disk drive that has been assigned a drive letter by Windows, just like the normal format and check disk tools in Windows. 
http://www.magsys.co.uk/delphi/magdskfmt.asp

 

TinyPortal © 2005-2018