Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

winapi

fat32

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 before 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

like image 473
andy Avatar asked Aug 30 '10 13:08

andy


1 Answers

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

like image 152
Dominik Weber Avatar answered Nov 04 '22 12:11

Dominik Weber