Format a drive with NTFS

Hi friends,

I want to format a drive with NTFS. How we can achieve this using c++.

I have search for the same on net but didn't get success.

Please provide any source link or a way to achieve this

Thanks.
I am aware of a SHFormatDrive function - but I have never tried it.
Use native Format() apis
Use native Format() apis
What Format APIs?
http://msdn.microsoft.com/en-us/library/aa390432(VS.85).aspx (server 2003 or higher)

Or you could use cmd.exe format option: (this is delphi code, but it is easy to port to c++)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
procedure FormatFloppy;
var
  sa: TSecurityAttributes;
  si: TStartupInfo;
  pi: TProcessInformation;
  BytesWritten: LongWord;
  hInRead, hInWrite: THandle;
begin
  // Initialize security information
  sa.nLength := SizeOf(sa);
  sa.lpSecurityDescriptor := nil;
  sa.bInheritHandle := True;
  CreatePipe(hInRead, hInWrite, @sa, 0);
  // Initialize startup info
  ZeroMemory(@si, SizeOf(si));
  si.cb := SizeOf(si);
  si.dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
  si.wShowWindow := SW_HIDE;
  si.hStdInput := hInRead;
  si.hStdOutput := GetStdHandle(STD_OUTPUT_HANDLE);
  si.hStdError := GetStdHandle(STD_ERROR_HANDLE);
  // Start process
  ZeroMemory(@pi, SizeOf(pi));
  CreateProcess(nil, 'cmd /c format a: /fs:FAT /F:1.44 /V:', nil, nil, True,
    CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, nil, nil, si, pi);
  CloseHandle(pi.hThread);
  CloseHandle(hInRead);
  // Write '<enter>' to start processing, and 'n<enter>' to respond to question at end
  WriteFile(hInWrite, #13#10'N'#13#10, 5, BytesWritten, nil);
  CloseHandle(hInWrite);
  // Wait for process to exit
  WaitForSingleObject(pi.hProcess, INFINITE);
  CloseHandle(pi.hProcess);
end;


Last edited on
launching cmd is horrible, instead of using native win 32 apis !
@ moodoran: I don't know Delphi, but that looks like it would just Zap the MBR. Would it really nuke the disk?

@ george135: Please contribute in a helpful way, I'm as interested as kbw to hear what part of the Win32 API will allow you to format the disk drive. And if you stick to you're traditional mentioning COM objects without giving a reference I will report you for trolling.
Last edited on
I answered
FormatEx() is used for 20 years
Hardly an answer, and if it's new with Server 2003 it's hardly 20 years old.
http://msdn.microsoft.com/en-us/library/aa382964%28v=VS.85%29.aspx

Mmm, let's remind ourselves what was around 20 years ago.
http://en.wikipedia.org/wiki/Timeline_of_Microsoft_Windows
Last edited on
Topic archived. No new replies allowed.