windows 直接访问硬盘?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2702853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:20:34  来源:igfitidea点击:

Direct access to harddrive?

c++cwindows

提问by jmasterx

I was wondering how hard disk access works. Ex, how could I view/modify sectors? Im targeting Windows if that helps. Thanks

我想知道硬盘访问是如何工作的。例如,我如何查看/修改扇区?如果有帮助,我的目标是 Windows。谢谢

回答by Eli Bendersky

This pageseems to have some relevant information on the subject:

这个页面似乎有一些关于这个主题的相关信息:

You can open a physical or logical drive using the CreateFile() application programming interface (API) with these device names provided that you have the appropriate access rights to the drive (that is, you must be an administrator). You must use both the CreateFile() FILE_SHARE_READ and FILE_SHARE_WRITE flags to gain access to the drive.

Once the logical or physical drive has been opened, you can then perform direct I/O to the data on the entire drive. When performing direct disk I/O, you must seek, read, and write in multiples of sector sizes of the device and on sector boundaries. Call DeviceIoControl() using IOCTL_DISK_GET_DRIVE_GEOMETRY to get the bytes per sector, number of sectors, sectors per track, and so forth, so that you can compute the size of the buffer that you will need.

您可以使用 CreateFile() 应用程序编程接口 (API) 和这些设备名称打开物理或逻辑驱动器,前提是您对驱动器具有适当的访问权限(即您必须是管理员)。您必须同时使用 CreateFile() FILE_SHARE_READ 和 FILE_SHARE_WRITE 标志才能访问驱动器。

打开逻辑或物理驱动器后,您就可以对整个驱动器上的数据执行直接 I/O。执行直接磁盘 I/O 时,您必须以设备扇区大小的倍数和扇区边界进行查找、读取和写入。使用 IOCTL_DISK_GET_DRIVE_GEOMETRY 调用 DeviceIoControl() 以获取每个扇区的字节数、扇区数、每个磁道的扇区等,以便您可以计算所需的缓冲区大小。

The documentation of CreateFilealso offers some clues:

CreateFile文档也提供了一些线索:

You can use the CreateFile function to open a physical disk drive or a volume, which returns a direct access storage device (DASD) handle that can be used with the DeviceIoControl function. This enables you to access the disk or volume directly, for example such disk metadata as the partition table. However, this type of access also exposes the disk drive or volume to potential data loss, because an incorrect write to a disk using this mechanism could make its contents inaccessible to the operating system. To ensure data integrity, be sure to become familiar with DeviceIoControl and how other APIs behave differently with a direct access handle as opposed to a file system handle.

您可以使用 CreateFile 函数打开物理磁盘驱动器或卷,这会返回可与 DeviceIoControl 函数一起使用的直接访问存储设备 (DASD) 句柄。这使您可以直接访问磁盘或卷,例如分区表等磁盘元数据。但是,这种类型的访问也会使磁盘驱动器或卷面临潜在的数据丢失,因为使用这种机制错误地写入磁盘可能会导致操作系统无法访问其内容。为确保数据完整性,请务必熟悉 DeviceIoControl 以及其他 API 的直接访问句柄与文件系统句柄的不同行为方式。

回答by Jacob O'Reilly

You can open a logical volume (e.g. c: drive) or a physical drive using win32's CreateFile() function. With the returned handle you can read and write sectors as needed. This page at MSDN should get you started: CreateFile Function

您可以使用 win32 的 CreateFile() 函数打开逻辑卷(例如 c: 驱动器)或物理驱动器。使用返回的句柄,您可以根据需要读取和写入扇区。MSDN 上的这个页面应该让你开始:CreateFile 函数

I take no responsibility for damaged caused :-)

我对造成的损坏不承担任何责任:-)