如何将文件写入物理驱动器(Windows 7)而不会收到ERROR_ACCESS_DENIED? [英] How to WriteFile to a PhysicalDrive (Windows 7) without getting ERROR_ACCESS_DENIED?
问题描述
首先,我做了以下工作:
//从驱动器字母h:我使用
获取物理磁盘号// 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);
//通过IOCTL_DISK_GET_DRIVE_GEOMETRY_EX
获取可用扇区的数量// => ulNumberOfSectors
//现在我尝试写一些扇区,例如2(我想使用更高的值):
WriteFile(hDevice,abBuffer,2 * 512,& byteswritten,NULL);
对 WriteFile
的调用失败, c $ c $ ERROR_ACCESS_DENIED 。
如果我写了一个扇区,它就可以工作。
<当我覆盖第一个扇区并重新插入设备时,Windows想格式化它。在这种情况下,我的代码可以同时运行2048个扇区而不会出现 ERROR_ACCESS_DENIED
。
我也卸载了 CodeProject:WriteFile on物理驱动器与Windows 7 ,但这并没有改变任何东西。很明显,卷被卸载,因为它在Windows资源管理器中不再可见。
由于性能原因,我想要写多个单个扇区。我也担心这个领域的其他问题可能会发生,因为我没有完全理解这个问题。
有什么建议?
不同的 WriteFile()
大小,但是我确实解决了
$ b
WriteFile Access is denied
< ERROR_ACCESS_DENIED / 5>
to
'\.\physicaldriveX
$ b
计算机管理 - >磁盘管理:
设备(通常为USB HDD / SSD)
$ b
- 成交量(H:在您的情况下) - >右键单击 - >删除成交量
- 磁盘(在你的案例中的磁盘2) - >右键单击 - >脱机
- 磁盘(在你的案例中的磁盘2) - >右键单击 - >在线 >
之后,我可以写入'\.\physicaldriveX',没有任何问题。
我认为只要有任何文件系统,Win7就会锁定(与以前的Windows版本不同)物理设备e设备,以避免一致性问题。
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.
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
.
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
.
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.
Any suggestions?
I didn't have problems with different WriteFile()
sizes, but I did solve the
WriteFile(): Access is denied
<ERROR_ACCESS_DENIED/5>
to '\.\physicaldriveX
devices (usually USB HDD/SSD) in Windows 7 running as Administrator (elevated rights) as follows:
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
After that, I'm able to write to '\.\physicaldriveX' with no problem.
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.
这篇关于如何将文件写入物理驱动器(Windows 7)而不会收到ERROR_ACCESS_DENIED?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!