如何在不获取 ERROR_ACCESS_DENIED 的情况下将文件写入物理驱动器 (Windows 7)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6608466/
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
How to WriteFile to a PhysicalDrive (Windows 7) without getting ERROR_ACCESS_DENIED?
提问by ur.
I'm trying to write a test pattern to every sector of a formatted USB drive. There is one logical drive (e.g. h:). This volume is FAT-formatted and contains data to be overwritten. Also, I want to overwrite the whole physical drive. The program is running with elevated user rights.
我正在尝试为格式化的 USB 驱动器的每个扇区编写测试模式。有一个逻辑驱动器(例如 h:)。此卷为 FAT 格式,包含要覆盖的数据。另外,我想覆盖整个物理驱动器。该程序正在以提升的用户权限运行。
First I did the following:
首先我做了以下事情:
// from the drive letter "h:" I get the physical disk number using
// IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS => "\.\PhysicalDrive2"
hDevice = ::CreateFile( "\.\PhysicalDrive2", GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL );
// get the number of available sectors with IOCTL_DISK_GET_DRIVE_GEOMETRY_EX
// => ulNumberOfSectors
// now I try to write some sectors, e.g. 2 (I want to use a higher value):
WriteFile( hDevice, abBuffer, 2*512, &byteswritten, NULL );
The call to WriteFile
fails with ERROR_ACCESS_DENIED
.
调用WriteFile
失败并显示ERROR_ACCESS_DENIED
.
If I write one sector, it works.
如果我写一个扇区,它会起作用。
When I overwrite the first sector and plug the device out and in again, Windows wants to format it. In this situation my code with 2048 sectors at once works without ERROR_ACCESS_DENIED
.
当我覆盖第一个扇区并再次插入设备时,Windows 想要对其进行格式化。在这种情况下,我的 2048 个扇区的代码可以在没有ERROR_ACCESS_DENIED
.
I also unmounted the volume as described in CodeProject: WriteFile on Physical Drives with Windows 7but this didn't change anything. Obviously the volume is unmounted because it's no longer visible in Windows Explorer.
我还按照CodeProject: WriteFile on Physical Drives with Windows 7 中所述卸载了卷,但这并没有改变任何东西。显然,该卷已卸载,因为它在 Windows 资源管理器中不再可见。
I want to write more than a single sector due to perfomance reasons. I'm also afraid that other problems in the field might occur because I don't fully understand ths problem.
由于性能原因,我想写多个扇区。我也担心该领域可能会出现其他问题,因为我不完全了解这个问题。
Any suggestions?
有什么建议?
回答by Radomir Tomis
I didn't have problems with different WriteFile()
sizes, but I did solve the
我没有遇到不同WriteFile()
尺寸的问题,但我确实解决了
WriteFile(): Access is denied
<ERROR_ACCESS_DENIED/5>
to '\.\physicaldriveX
WriteFile(): 拒绝
<ERROR_ACCESS_DENIED/5>
访问 '\.\physicaldriveX
devices (usually USB HDD/SSD) in Windows 7 running as Administrator (elevated rights) as follows:
在 Windows 7 中以管理员身份运行的设备(通常是 USB HDD/SSD)(提升权限)如下:
Computer Management -> Disk Management:
计算机管理 -> 磁盘管理:
- Volume (H: in your case) -> right-click -> Delete Volume
- Disk (Disk 2 in your case) -> right-click -> Off-line
- Disk (Disk 2 in your case) -> right-click -> On-line
- 音量(H:在您的情况下)-> 右键单击-> 删除音量
- 磁盘(在您的情况下为磁盘 2)-> 右键单击-> 脱机
- 磁盘(在您的情况下为磁盘 2)-> 右键单击-> 在线
After that, I'm able to write to '\.\physicaldriveX' with no problem.
之后,我可以毫无问题地写入“\.\physicaldriveX”。
I think the Win7 locks (unlike previous Windows releases) the physical device as long as there is any file system on the device to avoid consistency problems.
我认为只要设备上有任何文件系统,Win7 就会锁定(与以前的 Windows 版本不同)物理设备以避免一致性问题。
回答by Ben Voigt
You cannot directly access sectors of a drive which are owned by a mounted filesystem.
您不能直接访问由已安装文件系统拥有的驱动器扇区。
The documentation for FSCTL_DISMOUNT_VOLUME
describes the following sequence for overwriting a filesystem:
的文档FSCTL_DISMOUNT_VOLUME
描述了以下覆盖文件系统的顺序:
- Open a volume.
- Lock the volume.
- Format the volume.
- Dismount the volume.
- Unlock the volume.
- Close the volume handle.
- 打开一个卷。
- 锁定音量。
- 格式化卷。
- 卸载卷。
- 解锁音量。
- 关闭音量手柄。
Your pattern-writing operation would be in step 3 instead of formatting.
您的模式编写操作将在第 3 步而不是格式化。
回答by user2349550
Another method is to use clean
to delete all the partitions (and ALL DATA)on the disk:
另一种方法是使用clean
来删除所有分区(所有数据)在磁盘上:
C:\> diskpart
Diskpart> list disk
Diskpart> select disk N (where N is your disk number)
Diskpart> clean
Diskpart> exit