WriteFile error #5 “denied access” under win Vista/seven

末鹿安然 提交于 2019-12-05 04:21:44

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

Pierre

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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!