将文件写入Win7上的物理驱动器 [英] WriteFile to physicaldrive on Win7
问题描述
我的应用程序需要直接写入物理驱动器,这是一个USB可移动存储设备.在安装Win7之前,它可以在xp上正常运行.在这种情况下,WriteFile win32 api返回拒绝访问错误(5).
在浏览此板时,我想到了如何进行写入的工作:基本上,必须先删除映射到该卷的驱动器,然后才能开始写入.
(1)调用QueryDosDevice以获取设备名称.
(2)使用DDD_RAW_TARGET_PATH调用DefineDosDevice | DDD_REMOVE_DEFINITION | DDD_EXACT_MATCH_ON_REMOVE
然后删除该卷,并且WriteFile()返回成功.到目前为止一切都很好.
现在的问题是:如何还原驱动器映射?
我尝试将DefineDosDevice与DDD_RAW_TARGET_PATH一起使用,这是可行的,但是即使移除USB,它也可以保留驱动器号.因此,如果我再次运行该应用程序,它将寻找下一个可用的驱动器号,直到最后机器必须重新启动.
在挂载可移动存储设备方面有什么建议吗?在此先感谢.
My application requires writing directly to physical drive which is a usb removable storage device. It works ok on xp until Win7 is installed. In this case the WriteFile win32 api returns deny access error (5).
While looking through this board I got the idea how to make the write work: basically the drive mapped to the volume must be removed before writing can begin.
(1) call QueryDosDevice to get device name.
(2) call DefineDosDevice with DDD_RAW_TARGET_PATH | DDD_REMOVE_DEFINITION | DDD_EXACT_MATCH_ON_REMOVE
The volume is then gets removed and WriteFile() returns success. So far so good.
The problem now is: How can I restore the drive mapping?
I tried using DefineDosDevice with DDD_RAW_TARGET_PATH which is kind works but it persist the drive letter even the USB is removed. So if I run the application again it will look for next available drive letter until finally the machine has to reboot.
Any suggestion on mount back a removable storage device? Thanks in advance.
推荐答案
找到了解决方法:
撤消调用DefineDosDevice().
在WriteDisk之前,
致电DeviceIoControl FSCTL_DISMOUNT_VOLUME.
写入后,调用IOCTL_STORAGE_LOAD_MEDIA进行还原.
Found the solution:
Undo calling DefineDosDevice().
Before WriteDisk,
Call DeviceIoControl FSCTL_DISMOUNT_VOLUME.
After writing, call IOCTL_STORAGE_LOAD_MEDIA to restore.
这篇关于将文件写入Win7上的物理驱动器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!