Re: How to format disk by application

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



This, I believe, is a clone of originally posted by
Nish(http://www.voidnish.com/) solution which is written in C++:
http://www.codeproject.com/KB/dialog/cformatdrivedialog.aspx

--
Volodymyr
NG tips:
http://msmvps.com/blogs/v_scherbina/pages/microsoft-newsgroups-tips.aspx

"Christian Kaiser" <chk@xxxxxxxxx> wrote in message
news:478533b8$0$25372$9b4e6d93@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
T.Masuda wrote:
I want to format disk from my application. Does anyone know how to do
that? Thanks in advance.

Takashi Masuda

Here's some Delphi code I use. Basically it uses the shell function
SHFormatDrive, but uses a CBT hook to fill out the dialog with the volume
name and disable 'quick format' in the dialog that pops up.

By removing the comment of SendMessage(...BN_CLICKED...), you can start
formatting automatically. But it still requires the user to "close" the
dialog.

Christian

--------------------------

function SHFormatDrive (hWnd : HWND;
Drive,
FmtID,
Options : word) : LongInt; stdcall;
external 'Shell32.dll';

//////////////////////////////////////////////////////////////////////////////

var
hCBTHook: HHOOK;
ThisForm: TFrmDiskCreation;

function WndEnumProc(hWnd: HWND; lParam: LPARAM): boolean; stdcall;
begin
// pass on to dialog: TFrmDiskCreation.EnumProc()
Result := ThisForm.EnumProc(hWnd);
end;

function CBTProc(nCode: integer; wParam: WPARAM; lParam: LPARAM): LRESULT;
stdcall;
begin
// pass on to dialog: call TFrmDiskCreation.CBT()
Result := ThisForm.CBT(nCode,wParam,lParam);
end;

function TFrmDiskCreation.EnumProc(hWnd: HWND): boolean;
var
szClass: array[0..30] of char;
szText: array[0..30] of char;
begin
GetClassName(hWnd,szClass,sizeof(szClass));
if stricomp(szClass,'edit') = 0 then
SetWindowText(hWnd,pchar(EdtName.Text));
GetWindowText(hWnd,szText,sizeof(szText));
if (stricomp(szClass,'button') = 0) and
(((GetWindowLong(hWnd,GWL_STYLE) and $0000000f) = BS_CHECKBOX) or
((GetWindowLong(hWnd,GWL_STYLE) and $0000000f) = BS_AUTOCHECKBOX)) and
(strpos(szText,'Quick') <> NIL) then
SendMessage(hWnd,BM_SETCHECK,0,0);
Result := true;
end;

function TFrmDiskCreation.CBT(nCode: integer; wParam: WPARAM; lParam:
LPARAM): LRESULT;
begin
case nCode of
HCBT_ACTIVATE:
begin
EnumChildWindows(HWND(wParam),@WndEnumProc,0);
UnhookWindowsHookEx(hCBTHook);

// SendMessage(HWND(wParam), WM_COMMAND, MAKELONG(1,BN_CLICKED),
hWndStart);
end;
end;
Result := 0;
end;

function TFrmDiskCreation.FormatDisk(hWndParent: HWND): integer;
begin
ThisForm := self;
hCBTHook := SetWindowsHookEx(WH_CBT,@CBTProc,0,GetCurrentThreadId);

Result := SHFormatDrive(hWndParent,
ord(Config._sDataDrive[1]) - ord('A'),
0,
1);

ThisForm := NIL;
end;



.



Relevant Pages