Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My application requires writing directly to physical drive which is a usb removable storage device. It works ok on xp until Win7 is installed. In this case the WriteFile win32 api returns deny access error (5).

While looking through this board I got the idea how to make the write work: basically the drive mapped to the volume must be removed before writing can begin.

(1) call QueryDosDevice to get device name.
(2) call DefineDosDevice with DDD_RAW_TARGET_PATH | DDD_REMOVE_DEFINITION | DDD_EXACT_MATCH_ON_REMOVE

The volume is then gets removed and WriteFile() returns success. So far so good.

The problem now is: How can I restore the drive mapping?

I tried using DefineDosDevice with DDD_RAW_TARGET_PATH which is kind works but it persist the drive letter even the USB is removed. So if I run the application again it will look for next available drive letter until finally the machine has to reboot.

Any suggestion on mount back a removable storage device? Thanks in advance.
Posted
Comments
Sergey Alexandrovich Kryukov 25-Aug-11 23:21pm    
You need to explain. If you simply write the file do you think you write to chemical or biological drive or what? :-)
--SA

1 solution

Found the solution:

Undo calling DefineDosDevice().

Before WriteDisk,

Call DeviceIoControl FSCTL_DISMOUNT_VOLUME.

After writing, call IOCTL_STORAGE_LOAD_MEDIA to restore.
 
Share this answer
 
Comments
noamshan 20-Dec-11 7:53am    
I have the same problem. Tried all the options above, still get ERROR_ACCESS_DENIED after WriteFile. Any ideas?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900