开发者

WriteFile error #5 "denied access" under win Vista/seven

I googled a lot and I couldn't find any answer to this problem...

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 befo开发者_如何学编程re it woks on XP. I also tried using: DeviceIoControl with FSCTL_LOCK_VOLUME but that gives a mem fault error.

Hope this helps to understand and thanks for any help


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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜