Format C ++ Drive

I want to format the disk in C ++, but when I tried to use the Format function for windows.h, I could not find a sample or how to use it. I also do not want to interact with the user to get approval or cancel, so I can not use SHFormat

Does anyone know how I can do this?

+7
c ++ winapi
source share
4 answers

You can use CreateProcess to launch a hidden copy of the cmd.exe command and pass it to the characters to process the tooltip. This is in Pascal, but all API requests are APIs, so it should be easy to translate. You will also need to add some errors, and make sure you have tested it thoroughly.

Win32_Volume :: The format was added only in Windows 2003, so it will not work if you need WinXP or Win2K support.

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; 
+5
source share

You can use the SHFormatDrive function to display the Format Drive dialog box in Windows.

+7
source share

The correct way to do this is to use the Virtual Disk Service FormatPartition method .

+2
source share

C ++ does not offer such a low-level API.

What OS / platform are you using?

On Windows, there is a WMI API that does this: Win32_Volume format

Or you can try using the "system" (or on Windows, "ShellExecute"?);

Good luck.

Max.

+1
source share

All Articles