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

I googled a lot and did not find an answer to this problem ...

I have a C ++ console application that reads a 1 GB SD card that fixes improperly closed files and writes a FAT table accordingly. The SD card is initially recorded by firmware in a custom device. It worked fine until Xp and stopped working in Win Vista / seven. I tried to elevate privileges: in the administrator account type, I launched the cmd window using the "run as administrator" method, but no luck. I also tried with a manifest asking for highAvailable privileges, but no luck.

In some article, I read that โ€œWindows Vista does not allow you to access disks from user-mode processes at all. Does anyone know of any way around this behavior?

Im 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 the problem of linking ... But I am not attached to any spam at all ... just asking on a site related to the community :)

The code looks like

hDevice = CreateFile(buffer,GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,NULL, OPEN_EXISTING,0,NULL); 

Then I read BTB information from SD and searched and closed the file incorrectly.

Finally, when trying to write to SD

 WriteFile(hDevice,buffer,SD_SECTOR_SIZE, &temp, 0) 

I get access denied (error # 5)

The line in CreateFile () is \. \ G: since the letter g corresponds to the SD card on my machine. Everything that works fine, and, as I said, this happens on XP. I also tried using: DeviceIoControl with FSCTL_LOCK_VOLUME, but this gives a memory error error.

Hope this helps to understand and thank for any help.

+7
c ++ winapi fat32
source share
2 answers

I think this is due to the "buffer" of the path string; I came across the same question. The path you use to access the device should look like this: \\. \ PhysicalDrive% d "% d is the decimal number of the drive.

From Vista, this line is CASE SENSITIVE. Check spelling. You also need administrator rights, as before in XP.

For volumes. the letter must be CAPITALIZED for example "\\\ G:".

Also note that it is much better to access the SD card as a device, and not because, if Windows mounts it, a file system with a write cache may be installed.

In addition: I forgot to mention that the buffer in which you read / write data to / from should be aligned on the page, and reading should be a multiple of the sector size. VirtualAlloc () does it

+2
source share

You must turn off the volume before recording raw data.

From MSDN:

Writing to a volume descriptor 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 recorded are boot sectors.
  • Sectors that must be written to be placed outside the file system space.
  • You explicitly locked or unmounted the volume using FSCTL_LOCK_VOLUME or FSCTL_DISMOUNT_VOLUME.
  • The volume does not have a real file system. (In other words, it has the RAW file system installed.)

Writing to a disk descriptor will succeed if one of the following conditions is true:

  • The sectors to be recorded do not fall within the scope.
  • The sectors to be recorded fall into the installed volume, but you explicitly blocked or unmounted the volume using FSCTL_LOCK_VOLUME or FSCTL_DISMOUNT_VOLUME.
  • The sectors to be recorded fall into one that does not have a mounted file system other than RAW.

Code example :

 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
source share

All Articles