簡體   English   中英

通過c ++格式化驅動器

[英]Format drive by c++

我想用c ++格式化一個驅動器,但是當我嘗試使用windows.h的Format函數時,我找不到樣本或使用它的方法。 我也不想讓用戶進行交互以獲得正常或取消,因此我無法使用SHFormat

有誰知道我該怎么做?

您可以使用SHFormatDrive函數在Windows中顯示“格式化驅動器”對話框。

您可以使用CreateProcess啟動cmd.exe格式命令的隱藏副本,並為其提供字符以處理提示。 這是在Pascal中,但它是所有API調用,所以它應該很容易翻譯。 您還需要添加一些錯誤處理,並確保對其進行廣泛測試。

Win32_Volume :: Format僅在Windows 2003中添加,因此如果您需要WinXP或Win2K支持,它將無法運行。

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;

正確的方法是使用虛擬磁盤服務FormatPartition方法。

C ++不提供這種低級API。

您使用的操作系統/平台是什么?

在Windows上,有一個WMI API可以實現: Win32_Volume格式

或者您可以嘗試使用“system”(或在Windows上,“ShellExecute”?);

祝好運。

最大。

你可以調用system("format C: /FS:NTFS /X /Q /y"); 其中“C:”是要格式化的磁盤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM