WriteFile 错误 #5“拒绝访问”在win Vista/7下
我有一个 C++ 控制台应用程序,可以读取 1GB SD 卡,修复不正确关闭的文件并相应地写入 FAT 表。 SD 卡一开始是由定制设备中的固件写入的。 它在 Xp 之前工作正常,在 Win Vista/7 中停止工作。 我尝试提升权限:在管理员帐户类型中,我使用“以管理员身份运行”方法启动了一个cmd窗口,但没有成功。 我还尝试使用清单请求最高可用权限,但没有成功。
我在一些帖子中读到“Windows Vista 根本不允许您从用户模式进程访问磁盘。有人知道绕过这种行为的任何方法吗?
我正在研究解决方法,但我想知道这是否不可能
编辑:
这是我在这里发表的第一篇文章,所以我不太了解链接问题......但我根本不涉及任何垃圾邮件...只是在社区驱动的网站中询问:)
代码看起来像
hDevice = CreateFile(buffer,GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,NULL, OPEN_EXISTING,0,NULL);
我然后从 SD 读取 BTB 信息并查找未正确关闭的文件。
最后,当尝试写入 SD 时,
WriteFile(hDevice,buffer,SD_SECTOR_SIZE, &temp, 0)
我收到拒绝访问(错误 #5)
CreateFile() 上的字符串是 \.\g: 因为 g 字母对应于我机器上的 SD 卡。所有这些都可以正常工作,正如我之前所说,它在 XP 上也可以工作。我还尝试使用:DeviceIoControl 与 FSCTL_LOCK_VOLUME,但这会产生内存故障错误。
I have a C++ console application that reads a 1GB SD card that fixes improperly closed files and writes the FAT table accordingly. The SD card is written at the beginning by a firmware in a custom made device.
It worked OK up to Xp and stopped working in Win Vista/seven.
I tried elevating privileges: within an administrator account type, I launched a cmd window using the "run as administrator" method but no luck.
I also tried with a manifest asking for highestAvailable privileges but no luck.
I read in some post that “Windows Vista doesn't allow you to access the disks from user-mode processes at all. Does anybody know about any way of bypassing this behavior?
I’m working in a workaround however I would like to know if this is impossible or not
Edit:
This is my first post here so I don't quite understand about the linking issue... But I'm not reated to any spam at all... just asking in a comunity driven site :)
The code looks like
hDevice = CreateFile(buffer,GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,NULL, OPEN_EXISTING,0,NULL);
I then read the BTB information from the SD and look for and improperly closed file.
Finally when trying to write to the SD
WriteFile(hDevice,buffer,SD_SECTOR_SIZE, &temp, 0)
I get an Access denied (error #5)
The string on CreateFile() is \.\g: as the g letter correspond to the SD card on my machine. All that works ok and as I said before it woks on XP. I also tried using: DeviceIoControl with FSCTL_LOCK_VOLUME but that gives a mem fault error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这是由于路径字符串“buffer”造成的;我遇到了同样的问题。
您用于获取设备访问权限的路径需要类似于“\\.\PhysicalDrive%d”
%d 是驱动器的十进制数。
从 Vista 开始,此字符串区分大小写。
检查拼写。您还需要管理员权限,就像以前在 XP 中一样。
对于卷,。这封信需要大写
例如“\\.\G:”
另请注意,将 SD 卡作为设备而不是卷进行访问要好得多,因为如果 Windows 挂载它,则可能存在挂载有写入缓存的文件系统。
此外:我忘了提到,您读取/写入数据的缓冲区应该是页面对齐的,并且读取的数据是扇区大小的倍数。
VirtualAlloc() 执行此操作
I think this is due to the path string "buffer"; I ran into the same issue.
The path you are using to get device access needs to look lik this "\\.\PhysicalDrive%d"
%d is the decimal number of the drive.
From Vista on this string is CASE SENSITIVE.
Check the spelling. You also need admin rights, just as before in XP.
For Volumes,. the letter needs to CAPITALIZED
e.g. "\\.\G:"
Also note that it is much better to access the SD card as a device rathern than the volume, since if Windows mounts it, there might be a file system mounted with a write cache.
Furthermore: I forgot to mention that the buffer your read/write the data to/from should be page aligned and the read a multiple of the sector size.
VirtualAlloc() does this
您必须在写入原始数据之前卸载卷。
来自 MSDN:
示例代码:
You must dismount volume before writing raw data.
From MSDN:
Sample code: