WriteFile 错误 #5“拒绝访问”在win Vista/7下

发布于 2024-09-17 09:55:54 字数 862 浏览 6 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别闹i 2024-09-24 09:55:55

我认为这是由于路径字符串“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

萌逼全场 2024-09-24 09:55:55

您必须在写入原始数据之前卸载卷。

来自 MSDN:

如果卷没有安装的文件系统,或者满足以下条件之一,则对卷句柄的写入将会成功:

  • 要写入的扇区是引导扇区。
  • 要写入的扇区位于文件系统空间之外。
  • 您已使用 FSCTL_LOCK_VOLUME 或 FSCTL_DISMOUNT_VOLUME 显式锁定或卸载该卷。
  • 该卷没有实际的文件系统。 (换句话说,它安装了 RAW 文件系统。)

如果满足以下条件之一,则磁盘句柄上的写入将会成功:

  • 要写入的扇区不在卷的范围内。
  • 要写入的扇区属于已装载卷,但您已使用 FSCTL_LOCK_VOLUME 或 FSCTL_DISMOUNT_VOLUME 显式锁定或卸载该卷。
  • 要写入的扇区属于未安装除 RAW 之外的文件系统的卷。

示例代码

BOOL bResult = DeviceIoControl(hDevice,                // device to be queried
                               FSCTL_DISMOUNT_VOLUME,  // operation to perform
                               NULL, 0,                // no input buffer
                               pdg, sizeof(*pdg),      // output buffer
                               &junk,                  // # of bytes returned
                               (LPOVERLAPPED)NULL);    // synchronous I/O

You must dismount volume before writing raw data.

From MSDN:

A write on a volume handle will succeed if the volume does not have a mounted file system, or if one of the following conditions is true:

  • The sectors to be written to are boot sectors.
  • The sectors to be written to reside outside of file system space.
  • You have explicitly locked or dismounted the volume by using FSCTL_LOCK_VOLUME or FSCTL_DISMOUNT_VOLUME.
  • The volume has no actual file system. (In other words, it has a RAW file system mounted.)

A write on a disk handle will succeed if one of the following conditions is true:

  • The sectors to be written to do not fall within a volume's extents.
  • The sectors to be written to fall within a mounted volume, but you have explicitly locked or dismounted the volume by using FSCTL_LOCK_VOLUME or FSCTL_DISMOUNT_VOLUME.
  • The sectors to be written to fall within a volume that has no mounted file system other than RAW.

Sample code:

BOOL bResult = DeviceIoControl(hDevice,                // device to be queried
                               FSCTL_DISMOUNT_VOLUME,  // operation to perform
                               NULL, 0,                // no input buffer
                               pdg, sizeof(*pdg),      // output buffer
                               &junk,                  // # of bytes returned
                               (LPOVERLAPPED)NULL);    // synchronous I/O
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文