[英]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.