如何在没有得到 ERROR_ACCESS_DENIED 的情况下将文件写入 PhysicalDrive (Windows 7)? [英] How to WriteFile to a PhysicalDrive (Windows 7) without getting ERROR_ACCESS_DENIED?

查看:22
本文介绍了如何在没有得到 ERROR_ACCESS_DENIED 的情况下将文件写入 PhysicalDrive (Windows 7)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将测试模式写入已格式化 USB 驱动器的每个扇区.有一个逻辑驱动器(例如 h:).此卷为 FAT 格式并包含要覆盖的数据.另外,我想覆盖整个物理驱动器.该程序以提升的用户权限运行.

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.

首先我做了以下事情:

// 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 );

WriteFile 的调用失败并显示 ERROR_ACCESS_DENIED.

The call to WriteFile fails with ERROR_ACCESS_DENIED.

如果我写一个扇区,它就可以工作.

If I write one sector, it works.

当我覆盖第一个扇区并重新插入设备时,Windows 想要对其进行格式化.在这种情况下,我的代码同时包含 2048 个扇区,而无需 ERROR_ACCESS_DENIED.

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.

我还按照 CodeProject: WriteFile on Physical Drives with Windows 7 但这并没有改变任何东西.显然该卷已卸载,因为它在 Windows 资源管理器中不再可见.

I also unmounted the volume as described in CodeProject: WriteFile on Physical Drives with Windows 7 but this didn't change anything. Obviously the volume is unmounted because it's no longer visible in Windows Explorer.

由于性能原因,我想写入多个扇区.我也担心可能会出现该领域的其他问题,因为我不完全理解这个问题.

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.

有什么建议吗?

推荐答案

我没有遇到不同 WriteFile() 大小的问题,但我确实解决了

I didn't have problems with different WriteFile() sizes, but I did solve the

WriteFile(): 访问被拒绝'.physicaldriveX

WriteFile(): Access is denied <ERROR_ACCESS_DENIED/5> to '.physicaldriveX

在 Windows 7 中以管理员身份运行的设备(通常是 USB HDD/SSD)(提升权限)如下:

devices (usually USB HDD/SSD) in Windows 7 running as Administrator (elevated rights) as follows:

计算机管理 -> 磁盘管理:

Computer Management -> Disk Management:

  • 卷(H:在你的情况下)->右键单击->删除卷
  • 磁盘(在您的情况下为磁盘 2)-> 右键单击​​ -> 离线
  • 磁盘(在您的情况下为磁盘 2)-> 右键单击​​ -> 在线

之后,我可以毫无问题地写入.physicaldriveX".

After that, I'm able to write to '.physicaldriveX' with no problem.

我认为只要设备上有任何文件系统,Win7 就会锁定(与以前的 Windows 版本不同)物理设备以避免一致性问题.

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.

这篇关于如何在没有得到 ERROR_ACCESS_DENIED 的情况下将文件写入 PhysicalDrive (Windows 7)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆